[OnOpenAssetAttribute] 使用

Description

  • 官方文档
    Callback attribute for opening an asset in Unity (e.g the callback is fired when double clicking an asset in the Project Browser).
    Adding this attribute to a static method will make the method be called when Unity is about to open an asset. The method should have one of the following signatures:
    static bool OnOpenAsset(int instanceID, int line)
    static bool OnOpenAsset(int instanceID, int line, int column)
    Return true if you handled the opening of the asset or false if an external tool should open it.
using UnityEngine;
using [UnityEditor](https://docs.unity3d.com/ScriptReference/UnityEditor.html);
using UnityEditor.Callbacks;
public class MyAssetHandler{
    [[OnOpenAssetAttribute](https://docs.unity3d.com/ScriptReference/Callbacks.OnOpenAssetAttribute.html)(1)]
    public static bool step1(int instanceID, int line)
    {
        string name = [EditorUtility.InstanceIDToObject](https://docs.unity3d.com/ScriptReference/EditorUtility.InstanceIDToObject.html)(instanceID).name;
        [Debug.Log](https://docs.unity3d.com/ScriptReference/Debug.Log.html)("Open [Asset](https://docs.unity3d.com/ScriptReference/VersionControl.Asset.html) step: 1 (" + name + ")");
        return false; // we did not handle the open
    }

    // step2 has an attribute with index 2, so will be called after step1
    [[OnOpenAssetAttribute](https://docs.unity3d.com/ScriptReference/Callbacks.OnOpenAssetAttribute.html)(2)]
    public static bool step2(int instanceID, int line)
    {
        [Debug.Log](https://docs.unity3d.com/ScriptReference/Debug.Log.html)("Open [Asset](https://docs.unity3d.com/ScriptReference/VersionControl.Asset.html) step: 2 (" + instanceID + ")");
        return false; // we did not handle the open
    }
}

OnOpenAssetAttribute has an option to provide an order index in the callback, starting at 0. This is useful if you have more than one OnOpenAssetAttribute callback, and you would like them to be called in a certain order. Callbacks are called in order, starting at zero.

  • 添加这个attribute之后 双击Asset 就会回调 操作信息 instanceID, line
    [UnityEditor.Callbacks.OnOpenAssetAttribute(2)] 参数可以控制产生回调时调用方法的顺序

应用

  1. 打开指定后缀的文件
   [OnOpenAssetAttribute(2)]
    public static bool step2(int instanceID, int line)
    {
 
        string path = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
        string name = Application.dataPath + "/" + path.Replace("Assets/", "");
 
 
        if (name.EndsWith(".xx"))
        {
            //指定打开方式
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "D:/Program Files/Sublime Text 3/sublime_text.exe";
            startInfo.Arguments = name;
            process.StartInfo = startInfo;
            process.Start();
            return true;
        }
       // Debug.Log("Open Asset step: 2 (" + name + ")");
        return false; // we did not handle the open
    }
  1. consolo 打印重定向
    另一篇文章有写 这里就偷懒了

你可能感兴趣的:([OnOpenAssetAttribute] 使用)