Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

AssetDatabase.CanOpenAssetInEditor

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static bool CanOpenAssetInEditor(EntityId entityId);

Parameters

Parameter Description
entityId The EntityId of the asset to check.

Returns

bool True if AssetDatabase.OpenAsset can open the asset. Otherwise, false.

Description

Checks whether the Unity Editor can open the asset with the specified EntityId.

Use this method to check whether calling AssetDatabase.OpenAsset would open the asset. The asset must be saved to disk. Otherwise, the method 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(EntityId entityId) { if (AssetDatabase.GetMainAssetTypeAtPath(AssetDatabase.GetAssetPath(entityId)) == 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(EntityId entityId) { if (AssetDatabase.GetMainAssetTypeAtPath(AssetDatabase.GetAssetPath(entityId)) == 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.GetEntityId()); } }

Obsolete Please use CanOpenAssetInEditor(EntityId) with the EntityId parameter type instead.

Declaration

public static bool CanOpenAssetInEditor(int instanceID);

Parameters

Parameter Description
instanceID The instance ID of the asset to check.

Returns

bool True if AssetDatabase.OpenAsset can open the asset. Otherwise, false.

Description

Checks whether the Unity Editor can open the asset with the specified instance ID.

This overload is obsolete. Use the overload that takes an EntityId instead.

Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961