Unity 导入触发

作者: 皿卜土 | 来源:发表于2016-07-28 09:46 被阅读0次

资源加载时触发


public class test:AssetPostprocessor  {
    //所有的资源的导入,删除,移动,都会调用此方法,注意,这个方法是static的 
    public static void OnPostprocessAllAssets(string[] importedAsset, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        Debug.Log("OnPostprocessAllAssets");
        foreach (string str in importedAsset)
        {
            Debug.Log("importedAsset = " + str);
        }
        foreach (string str in deletedAssets)
        {
            Debug.Log("deletedAssets = " + str);
        }
        foreach (string str in movedAssets)
        {
            Debug.Log("movedAssets = " + str);
        }
        foreach (string str in movedFromAssetPaths)
        {
            Debug.Log("movedFromAssetPaths = " + str);
        }
    }

    

AddTag Scripts

using UnityEditor;

public static class TagHelper
{
    public static void AddTag(string tag)
    {
        UnityEngine.Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
        if ((asset != null) && (asset.Length > 0))
        {
            SerializedObject so = new SerializedObject(asset[0]);
            SerializedProperty tags = so.FindProperty("tags");

            for (int i = 0; i < tags.arraySize; ++i)
            {
                if (tags.GetArrayElementAtIndex(i).stringValue == tag)
                {
                    return;     // Tag already present, nothing to do.
                }
            }

            tags.InsertArrayElementAtIndex(tags.arraySize);
            tags.GetArrayElementAtIndex(tags.arraySize - 1).stringValue = tag;
            so.ApplyModifiedProperties();
            so.Update();
        }
    }
}

taskManager 全方法

using System.Reflection;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine;

public class test01 :AssetPostprocessor {

    // will check if the specified layer names are present and add any missing ones
    // it will simply add them from the first open slots so do not depend on any
    // specific order but rather grab layers from the layer names at runtime
    public static void CheckLayers(string[] layerNames)
    {
        SerializedObject manager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
#if !UNITY_4
        SerializedProperty layersProp = manager.FindProperty("layers");
#endif

        foreach (string name in layerNames)
        {
            // check if layer is present
            bool found = false;
            for (int i = 0; i <= 31; i++)
            {
#if UNITY_4
                    string nm = "User Layer " + i;
                    SerializedProperty sp = manager.FindProperty(nm);
#else
                SerializedProperty sp = layersProp.GetArrayElementAtIndex(i);
#endif
                if (sp != null && name.Equals(sp.stringValue))
                {
                    found = true;
                    break;
                }
            }

            // not found, add into 1st open slot
            if (!found)
            {
                SerializedProperty slot = null;
                for (int i = 8; i <= 31; i++)
                {
#if UNITY_4
                        string nm = "User Layer " + i;
                        SerializedProperty sp = manager.FindProperty(nm);
#else
                    SerializedProperty sp = layersProp.GetArrayElementAtIndex(i);
#endif
                    if (sp != null && string.IsNullOrEmpty(sp.stringValue))
                    {
                        slot = sp;
                        break;
                    }
                }

                if (slot != null)
                {
                    slot.stringValue = name;
                }
                else
                {
                    Debug.LogError("Could not find an open Layer Slot for: " + name);
                }
            }
        }

        // save
        manager.ApplyModifiedProperties();
    }

    public static void CheckTags(string[] tagNames)
    {
        SerializedObject manager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
        SerializedProperty tagsProp = manager.FindProperty("tags");

        List<string> DefaultTags = new List<string>() { "Untagged", "Respawn", "Finish", "EditorOnly", "MainCamera", "Player", "GameController" };

        foreach (string name in tagNames)
        {
            if (DefaultTags.Contains(name)) continue;

            // check if tag is present
            bool found = false;
            for (int i = 0; i < tagsProp.arraySize; i++)
            {
                SerializedProperty t = tagsProp.GetArrayElementAtIndex(i);
                if (t.stringValue.Equals(name)) { found = true; break; }
            }

            // if not found, add it
            if (!found)
            {
                tagsProp.InsertArrayElementAtIndex(0);
                SerializedProperty n = tagsProp.GetArrayElementAtIndex(0);
                n.stringValue = name;
            }
        }

        // save
        manager.ApplyModifiedProperties();
    }

    public static void CheckSortLayers(string[] tagNames)
    {
        SerializedObject manager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
        SerializedProperty sortLayersProp = manager.FindProperty("m_SortingLayers");

        //for (int i = 0; i < sortLayersProp.arraySize; i++)
        //{ // used to figure out how all of this works and what properties values look like
        //    SerializedProperty entry = sortLayersProp.GetArrayElementAtIndex(i);
        //    SerializedProperty name = entry.FindPropertyRelative("name");
        //    SerializedProperty unique = entry.FindPropertyRelative("uniqueID");
        //    SerializedProperty locked = entry.FindPropertyRelative("locked");
        //    Debug.Log(name.stringValue + " => " + unique.intValue + " => " + locked.boolValue);
        //}

        foreach (string name in tagNames)
        {
            // check if tag is present
            bool found = false;
            for (int i = 0; i < sortLayersProp.arraySize; i++)
            {
                SerializedProperty entry = sortLayersProp.GetArrayElementAtIndex(i);
                SerializedProperty t = entry.FindPropertyRelative("name");
                if (t.stringValue.Equals(name)) { found = true; break; }
            }

            // if not found, add it
            if (!found)
            {
                manager.ApplyModifiedProperties();
                AddSortingLayer();
                manager.Update();

                int idx = sortLayersProp.arraySize - 1;
                SerializedProperty entry = sortLayersProp.GetArrayElementAtIndex(idx);
                SerializedProperty t = entry.FindPropertyRelative("name");
                t.stringValue = name;
            }
        }

        // save
        manager.ApplyModifiedProperties();
    }

    // you need 'using System.Reflection;' for these
    private static Assembly editorAsm;
    private static MethodInfo AddSortingLayer_Method;

    /// <summary> add a new sorting layer with default name </summary>
    public static void AddSortingLayer()
    {
        if (AddSortingLayer_Method == null)
        {
            if (editorAsm == null) editorAsm = Assembly.GetAssembly(typeof(Editor));
            System.Type t = editorAsm.GetType("UnityEditorInternal.InternalEditorUtility");
            AddSortingLayer_Method = t.GetMethod("AddSortingLayer", (BindingFlags.Static | BindingFlags.NonPublic), null, new System.Type[0], null);
        }
        AddSortingLayer_Method.Invoke(null, null);
    }
 
}

最后用到的类

using System;
using System.Reflection;
using Assets.roguelike2d.game;
using UnityEditor;
using UnityEngine;


[InitializeOnLoad]
public class AddTagsAndLayers
{
    static AddTagsAndLayers()
    {
        SerializedObject tagManager =
                    new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);
        //增加所有的Tags
        foreach (string str in Enum.GetNames(typeof(GameTags)))
        {
            AddTag(str, tagManager);
        }
        //从10层做起始层
        int startIndex = 10;
        int layerCount = 0;
        //增加所有的Layers
        foreach (var str in Enum.GetValues(typeof(GameLayers)))
        {
            AddLayer(str.ToString(), tagManager, startIndex + layerCount);
            layerCount++;
        }
        //增加所有的SortingLayers
        foreach (GameLayers str in Enum.GetValues(typeof(GameLayers)))
        {
            addSortingLayer(str, tagManager);
        }
    }


    static void addSortingLayer(GameLayers sortLayer, SerializedObject tagManager)
    {
        SerializedProperty sortingLayers = tagManager.FindProperty("m_SortingLayers");
        bool found = false;
        for (int i = 0; i < sortingLayers.arraySize; i++)
        {
            SerializedProperty t = sortingLayers.GetArrayElementAtIndex(i);
            if (t.FindPropertyRelative("name").stringValue.Equals(sortLayer.ToString()))
            {
                found = true;
                t.FindPropertyRelative("uniqueID").longValue = (long)sortLayer;
                break;
            }
        }
        if (!found)
        {
            sortingLayers.InsertArrayElementAtIndex(0);
            SerializedProperty sp = sortingLayers.GetArrayElementAtIndex(0);
            if (sp != null)
            {
                sp.FindPropertyRelative("name").stringValue = sortLayer.ToString();
                sp.FindPropertyRelative("uniqueID").longValue = (long)sortLayer;
            }
        }
        tagManager.ApplyModifiedProperties();
    }

    static void AddTag(string tag, SerializedObject tagManager)
    {
        SerializedProperty tagsProp = tagManager.FindProperty("tags");

        bool found = false;
        for (int i = 0; i < tagsProp.arraySize; i++)
        {
            SerializedProperty t = tagsProp.GetArrayElementAtIndex(i);
            if (t.stringValue.Equals(tag)) { found = true; break; }
        }
        if (!found)
        {
            tagsProp.InsertArrayElementAtIndex(0);
            SerializedProperty n = tagsProp.GetArrayElementAtIndex(0);
            n.stringValue = tag;
        }
        tagManager.ApplyModifiedProperties();
    }

    static void AddLayer(string layer, SerializedObject tagManager,int index)
    {
        SerializedProperty layersProp = tagManager.FindProperty("layers");

        bool found = false;
        for (int i = 0; i < layersProp.arraySize; i++)
        {
            SerializedProperty t = layersProp.GetArrayElementAtIndex(i);
            if (t.stringValue.Equals(layer)) { found = true; break; }
        }
        if (!found)
        {
            Debug.Log(layersProp.arraySize);
            SerializedProperty sp = layersProp.GetArrayElementAtIndex(index);
            if (sp != null) sp.stringValue = layer;
        }
        tagManager.ApplyModifiedProperties();
    }


}



枚举的层

public enum GameLayers:long
    {
        Role = 1,
        Item = 2,
        BackGround = 3,
    }

相关文章

网友评论

    本文标题:Unity 导入触发

    本文链接:https://www.haomeiwen.com/subject/lwkajttx.html