编辑器扩展AssetModificationProcessor

版权声明:转载 请说明 出处 https://blog.csdn.net/qq2512667/article/details/87096754

AssetModificationProcessor

在编辑模式下可以让你的Unity资源 保存序列化 前 勾住它,

防止Unity 在写入资源

提前把它锁住,对它进行处理

Unity Editor下对资源进行操作时调用AssetModificationProcessor

.

Leave feedback

Description

AssetModificationProcessor lets you hook into saving of serialized assets and scenes which are edited inside Unity.

This lets you prevent writing of assets by Unity for integration with VCS solutions like Perforce which require locking of files.

This can be used as a callback to know when Assets are saved. You can then make actions such as run code generator.

Messages

IsOpenForEdit This is called by Unity when inspecting an asset to determine if an editor should be disabled.
OnWillCreateAsset Unity calls this method when it is about to create an Asset you haven't imported (for example, .meta files).
OnWillDeleteAsset This is called by Unity when it is about to delete an asset from disk.
OnWillMoveAsset Unity calls this method when it is about to move an Asset on disk.
OnWillSaveAssets This is called by Unity when it is about to write serialized assets or Scene files to disk.

 这几个  第一个  检查一个资源 确定是否应该禁用编辑器时,会调用这个函数

第二个是 在 资源被创建前 会调用

第三个是 资源被删除时前  会调用,

第四个是 资源被移动前,会被调用

第五个是 资源将要保存前 会被调用。

可以 用OnWillCreateAsset 写一个作者和 时间版本是注释。

例子。 

所有继承UnityEditor类 都要放在Editor文件夹下。

using System.IO;
using UnityEngine;
using UnityEditor;
public class ChangeCSStyleAutoSummer : UnityEditor.AssetModificationProcessor
{
    //要添加的注释内容
    private static string annotationStr =
"///<summary>"+
"描述:\r\n"+ 
"///"+"作者: xxx \r\n" +
"///"+"创建时间: #CreateTime# \r\n"+
"///"+"版本:v1.0\r\n"+
"///</summary>";

    
      public static void OnWillCreateAsset(string name)
    {
        string annotationStr =
        "///<summary>" +
        "描述:\r\n" +
        "///" + "作者: xxx \r\n" +
        "///" + "创建时间: #CreateTime# \r\n" +
        "///" + "版本:v1.0\r\n" +
        "///</summary>";

        string path = name.Replace(".meta", "");     
        GenericMenu genericMenu = new GenericMenu();
     
        genericMenu.AddItem(new GUIContent("确定"), false,
        (object o) 
        =>
            {
                if (path.EndsWith(".cs"))
                {
                    annotationStr = annotationStr.Replace("#CreateTime#", System.DateTime.Now.ToString("yyy/MM/dd HH:ss"));
                    annotationStr += File.ReadAllText(path);

                    File.WriteAllText(path, annotationStr);
                    Debug.Log(o);
                }
            } ,"Done");
        genericMenu.AddSeparator("");
        genericMenu.AddItem(new GUIContent("取消"), false, () => { return; });
        genericMenu.ShowAsContext();
    }
}

 https://jingyan.baidu.com/article/7c6fb428d33a6980642c90dc.html

猜你喜欢

转载自blog.csdn.net/qq2512667/article/details/87096754