Unity 自动添加tag,SortingLayers,Layer

(Mark 学习)

momo大神的文章已经写得很详细了,但文章的代码在新版本里已经无法运行了,前几天整理公司SDK,看到自动添加tag的地方,参照momo的做法整理了一版。顺便扩展了一些功能,这里贴出来供大家参考

[csharp]  view plain  copy
  1. <span style="font-size:18px;">using UnityEditor;  
  2. using UnityEngine;  
  3.   
  4. public class TagManagerData  
  5. {  
  6.     public static string[] tags = new string[]  
  7.     {  
  8.         "momo"  
  9.     };  
  10.   
  11.     public static string[] sortingLayers = new string[]  
  12.     {  
  13.   
  14.     };  
  15.   
  16.     public static string[] layers = new string[]  
  17.     {  
  18.        "Blend"  
  19.     };  
  20. }  
  21.   
  22.   
  23. [InitializeOnLoad]  
  24. public class AutoWriteSettings  
  25. {  
  26.     static AutoWriteSettings()  
  27.     {  
  28.         bool hasKey = PlayerPrefs.HasKey("WriteSettings");  
  29.         if (hasKey == false)  
  30.         {  
  31.             PlayerPrefs.SetInt("WriteSettings", 1);  
  32.             OnWrite();  
  33.         }  
  34.     }  
  35.   
  36.     [MenuItem("Tools/TagTools/Write")]  
  37.     static void OnWrite()  
  38.     {  
  39.         foreach (var tag in TagManagerData.tags)  
  40.         {  
  41.             AddTag(tag);  
  42.         }  
  43.   
  44.         foreach (var sortingLayer in TagManagerData.sortingLayers)  
  45.         {  
  46.             AddSortingLayer(sortingLayer);  
  47.         }  
  48.   
  49.         foreach (var layer in TagManagerData.layers)  
  50.         {  
  51.             AddLayer(layer);  
  52.         }  
  53.     }  
  54.   
  55.   
  56.     [MenuItem("Tools/TagTools/ClearTags")]  
  57.     static void ClearTags()  
  58.     {  
  59.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  60.         SerializedProperty it = tagManager.GetIterator();  
  61.         while (it.NextVisible(true))  
  62.         {  
  63.             if (it.name == "tags")  
  64.             {  
  65.                 it.ClearArray();  
  66.                 tagManager.ApplyModifiedProperties();  
  67.                 return;  
  68.             }  
  69.         }  
  70.     }  
  71.   
  72.     [MenuItem("Tools/TagTools/ClearSortingLayers")]  
  73.     static void ClearSortingLayers()  
  74.     {  
  75.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  76.         SerializedProperty it = tagManager.GetIterator();  
  77.         while (it.NextVisible(true))  
  78.         {  
  79.             if (it.name == "m_SortingLayers")  
  80.             {  
  81.                 it.ClearArray();  
  82.                 tagManager.ApplyModifiedProperties();  
  83.                 return;  
  84.             }  
  85.         }  
  86.     }  
  87.   
  88.     [MenuItem("Tools/TagTools/ClearLayers")]  
  89.     static void ClearLayers()  
  90.     {  
  91.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  92.         SerializedProperty it = tagManager.GetIterator();  
  93.         while (it.NextVisible(true))  
  94.         {  
  95.             if (it.name == "layers")  
  96.             {  
  97.                 for (int i = 0; i < it.arraySize; i++)  
  98.                 {  
  99.                     if (i == 3 || i == 6 || i == 7) continue;  
  100.                     SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);  
  101.                     dataPoint.stringValue = string.Empty;  
  102.                 }  
  103.                 tagManager.ApplyModifiedProperties();  
  104.                 return;  
  105.             }  
  106.         }  
  107.     }  
  108.   
  109.     [MenuItem("Tools/TagTools/ClearAll")]  
  110.     static void ClearAll()  
  111.     {  
  112.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  113.         SerializedProperty it = tagManager.GetIterator();  
  114.         while (it.NextVisible(true))  
  115.         {  
  116.             it.ClearArray();  
  117.             tagManager.ApplyModifiedProperties();  
  118.         }  
  119.     }  
  120.   
  121.     static void ReadTag()  
  122.     {  
  123.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  124.         SerializedProperty it = tagManager.GetIterator();  
  125.         while (it.NextVisible(true))  
  126.         {  
  127.             if (it.name == "tags")  
  128.             {  
  129.                 var count = it.arraySize;  
  130.   
  131.                 for (int i = 0; i < count; i++)  
  132.                 {  
  133.                     var dataPoint = it.GetArrayElementAtIndex(i);  
  134.                     //Debug.Log(dataPoint.stringValue);  
  135.                 }  
  136.             }  
  137.         }  
  138.     }  
  139.   
  140.     static void ReadSortingLayer()  
  141.     {  
  142.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  143.         SerializedProperty it = tagManager.GetIterator();  
  144.         while (it.NextVisible(true))  
  145.         {  
  146.             if (it.name == "m_SortingLayers")  
  147.             {  
  148.                 var count = it.arraySize;  
  149.                 for (int i = 0; i < count; i++)  
  150.                 {  
  151.                     var dataPoint = it.GetArrayElementAtIndex(i);  
  152.                     while (dataPoint.NextVisible(true))  
  153.                     {  
  154.                         if (dataPoint.name == "name")  
  155.                         {  
  156.                             //Debug.Log(dataPoint.stringValue);  
  157.                         }  
  158.                     }  
  159.                 }  
  160.   
  161.   
  162.             }  
  163.         }  
  164.     }  
  165.   
  166.     static void ReadLayer()  
  167.     {  
  168.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  169.         SerializedProperty it = tagManager.GetIterator();  
  170.         while (it.NextVisible(true))  
  171.         {  
  172.             if (it.name == "layers")  
  173.             {  
  174.                 for (int i = 0; i < it.arraySize; i++)  
  175.                 {  
  176.                     if (i == 3 || i == 6 || i == 7) continue;  
  177.                     SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);  
  178.                     //Debug.Log(dataPoint.stringValue);  
  179.                 }  
  180.             }  
  181.         }  
  182.     }  
  183.   
  184.   
  185.     static void AddTag(string tag)  
  186.     {          
  187.         if (!isHasTag(tag))  
  188.         {  
  189.             SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  190.             SerializedProperty it = tagManager.GetIterator();  
  191.             while (it.NextVisible(true))  
  192.             {  
  193.                 if (it.name == "tags")  
  194.                 {  
  195.                     it.InsertArrayElementAtIndex(it.arraySize);  
  196.                     SerializedProperty dataPoint = it.GetArrayElementAtIndex(it.arraySize - 1);  
  197.                     dataPoint.stringValue = tag;  
  198.                     tagManager.ApplyModifiedProperties();  
  199.                     return;  
  200.                 }  
  201.             }  
  202.         }  
  203.     }  
  204.   
  205.     static void AddSortingLayer(string sortingLayer)  
  206.     {  
  207.         if (!isHasSortingLayer(sortingLayer))  
  208.         {  
  209.             SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  210.             SerializedProperty it = tagManager.GetIterator();  
  211.             while (it.NextVisible(true))  
  212.             {  
  213.                 if (it.name == "m_SortingLayers")  
  214.                 {  
  215.                     Debug.Log("SortingLayers" + it.arraySize);  
  216.                     it.InsertArrayElementAtIndex(it.arraySize);  
  217.                     SerializedProperty dataPoint = it.GetArrayElementAtIndex(it.arraySize-1);  
  218.                     while (dataPoint.NextVisible(true))  
  219.                     {  
  220.                         if (dataPoint.name == "name")  
  221.                         {  
  222.                             dataPoint.stringValue = sortingLayer;  
  223.                             tagManager.ApplyModifiedProperties();  
  224.                             return;  
  225.                         }  
  226.                     }  
  227.                 }  
  228.             }  
  229.         }  
  230.     }  
  231.   
  232.     static void AddLayer(string layer)  
  233.     {  
  234.         if (!isHasLayer(layer))  
  235.         {  
  236.             SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  237.             SerializedProperty it = tagManager.GetIterator();  
  238.             while (it.NextVisible(true))  
  239.             {  
  240.                 if (it.name== "layers")  
  241.                 {  
  242.                     for (int i = 0; i < it.arraySize; i++)  
  243.                     {  
  244.                         if (i==3||i == 6||i==7) continue;  
  245.                         SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);  
  246.                         if (string.IsNullOrEmpty(dataPoint.stringValue))  
  247.                         {  
  248.                             dataPoint.stringValue = layer;  
  249.                             tagManager.ApplyModifiedProperties();  
  250.                             return;  
  251.                         }  
  252.                     }  
  253.                 }  
  254.             }  
  255.         }  
  256.     }  
  257.   
  258.     static bool isHasTag(string tag)  
  259.     {  
  260.         for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.tags.Length; i++)  
  261.         {  
  262.             if (UnityEditorInternal.InternalEditorUtility.tags[i].Contains(tag))  
  263.                 return true;  
  264.         }  
  265.         return false;  
  266.     }  
  267.   
  268.     static bool isHasSortingLayer(string sortingLayer)  
  269.     {  
  270.         SerializedObject tagManager = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset")[0]);  
  271.         SerializedProperty it = tagManager.GetIterator();  
  272.         while (it.NextVisible(true))  
  273.         {  
  274.             if (it.name == "m_SortingLayers")  
  275.             {  
  276.                 for (int i = 0; i < it.arraySize; i++)  
  277.                 {  
  278.                     SerializedProperty dataPoint = it.GetArrayElementAtIndex(i);  
  279.                     while (dataPoint.NextVisible(true))  
  280.                     {  
  281.                         if (dataPoint.name == "name")  
  282.                         {  
  283.                             if (dataPoint.stringValue == sortingLayer) return true;  
  284.                         }  
  285.                     }  
  286.                 }  
  287.             }  
  288.         }  
  289.   
  290.         return false;  
  291.     }  
  292.   
  293.     static bool isHasLayer(string layer)  
  294.     {  
  295.         for (int i = 0; i < UnityEditorInternal.InternalEditorUtility.layers.Length; i++)  
  296.         {  
  297.             if (UnityEditorInternal.InternalEditorUtility.layers[i].Contains(layer))  
  298.                 return true;  
  299.         }  
  300.         return false;  
  301.     }  
  302. }</span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值