Version: 2021.1

AssetDatabase.CanOpenAssetInEditor

切换到手册
public static bool CanOpenAssetInEditor (int instanceID);

参数

instanceID 资源的实例 ID。

返回

bool Returns true if Unity can successfully open the asset in the Editor, otherwise returns false.

描述

Checks if Unity can open an asset in the Editor.

Checks if Unity can open a given asset instance in the Editor if AssetDatabase.OpenAsset is executed. Note: The asset must be written to disk, or this function always returns false.

using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

[CreateAssetMenu(fileName = "MyObject", menuName = "ScriptableObjects/MyObject", order = 1)] public class MyObject : ScriptableObject { public string myData;

[OnOpenAsset] public static bool Open(int instanceID) { if (AssetDatabase.GetMainAssetTypeAtPath(AssetDatabase.GetAssetPath(instanceID)) == typeof(MyObject)) { Debug.Log("Opening asset"); return true; } else return false; // This method doesn't handle opening of different asset types }

[OnOpenAsset(OnOpenAssetAttributeMode.Validate)] public static bool WillOpen(int instanceID) { if (AssetDatabase.GetMainAssetTypeAtPath(AssetDatabase.GetAssetPath(instanceID)) == typeof(MyObject)) { // We can open MyObject asset using MyObject opening method Debug.Log("This asset can be opened by OnOpenAsset marked method"); return true; } else return false; // The passed instance doesn't belong to MyObject type asset so we won't be able to open it using opening method inside MyObject }

[MenuItem("Test/WillOpenInUnity")] public static void WillSelectionBeOpenedInUnity() { AssetDatabase.CanOpenAssetInEditor(Selection.activeObject.GetInstanceID()); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961