Unity 導入項目tag與layer的方法


 

UnityEditor》AssetPostprocessor 控制導入的腳本

 

類中的方法字段

public string assetPath { get; set; }

public AssetImporter assetImporter { get; }

public Texture2D preview { get; set; }

 

public virtual int GetPostprocessOrder();

public virtual uint GetVersion();

public void LogError(string warning);

public void LogError(string warning, [DefaultValue("null")] UnityEngine.Object context);

public void LogWarning(string warning);

public void LogWarning(string warning, [DefaultValue("null")] UnityEngine.Object context);

 

可以注意下projectsetting 下的assets 文件的數據

以下時TagManger的數據

 

這些內容手動配置是無法更改引擎內的數據的

 

//Mono掛在網上的腳本新版本存在兩個問題

>layer 層 在index8之后才能設置

>tag 默認size0

可能時mono 16年的帖子針對5x版本可行但是針對17版本不可執行

 

 

 

代碼:

using UnityEditor;

 

public class M_LayerExtent : AssetPostprocessor {

 

    static string TagsAdd = "momo&yang";

    static string TagsLayer = "ruoruo&hang";

    private const string ScriptsPath = "Assets/Editor/M_LayerExtent.cs";

 

    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)

    {

        foreach (string s in importedAssets)

        {

            //此腳本需要存在的路徑

            if (s.Equals(ScriptsPath))

            {

                string[] tags = TagsAdd.Split('&');

                string[] Layers = TagsLayer.Split('&');

                foreach (var tag in tags) AddTag(tag);

                foreach (var layer in Layers) AddLayer(layer);

                //Debug.Log("loading....");

                return;

            }

        }

    }

 

    static void AddTag(string tag)

    {

        if (!isHasTag(tag))

        {           

            SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);

            SerializedProperty it = tagManager.GetIterator();

            //Debug.Log("prop is:"+it);

            while (it.NextVisible(true))

            {

                if (it.name == "tags")

                {

                    //Debug.Log("NextVisible is:" + it.name+" size is:"+it.arraySize);

 

                    it.arraySize++;

 

                    for (int i = 0; i < it.arraySize; i++)

                    {

                       

                        SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);

                        //Debug.Log("loading.... " + dataPoint.stringValue);

                        if (string.IsNullOrEmpty(dataPoint.stringValue))

                        {

                            dataPoint.stringValue = tag;

                            tagManager.ApplyModifiedProperties();

                            //Debug.Log("is loading tag");

                            return;

                        }

                    }

                }

            }

        }

    }

    

    static void AddLayer(string layer)

    {

        if (!isHasLayer(layer))

        {

            SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);

            SerializedProperty it = tagManager.GetIterator();

            while (it.NextVisible(true))

            {

                //Debug.Log("prop is:"+ it.name);

                if (it.name == "layers")                                         

                {

                    for (int i = 0; i < it.arraySize; i++)

                    {

                        SerializedProperty dataPoint = it.GetArrayElementAtIndex(i+1);

                        //Debug.Log("loading.... " + dataPoint.stringValue+"  i:"+i);

                        //默認前把層無法設置

                        if (string.IsNullOrEmpty(dataPoint.stringValue)&&i>=8)

                        {

                           

                            dataPoint.stringValue = layer;

                            tagManager.ApplyModifiedProperties();

                            //Debug.Log("is loading layer");

                            return;

                        }

                    }

                }

                

            }

        }

    }

 

    static bool isHasTag(string tag)

    {

        for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++)

        {

            if (UnityEditorInternal.InternalEditorUtility.tags[i].Contains(tag))

                return true;

        }

        return false;

    }

 

    static bool isHasLayer(string layer)

    {

        for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.layers.Length; i++)

        {

            if (UnityEditorInternal.InternalEditorUtility.layers[i].Contains(layer))

                return true;

        }

        return false;

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM