instanceID | 资产的实例 ID。 |
bool 如果 Unity 可以在编辑器中成功打开该资产,则返回 true,否则返回 false。
检查 Unity 是否可以在编辑器中打开资产。
如果执行 AssetDatabase.OpenAsset,则检查 Unity 是否可以在编辑器中打开给定的资产实例。注意:必须将资产写入磁盘,否则此函数始终返回 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()); } }