ZEDLayersManager.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. /// <summary>
  5. /// This class creates automaticaly layers on load
  6. /// </summary>
  7. public struct ZEDLayers
  8. {
  9. public static int tagInvisibleToZED = 16;
  10. public static string ID_tagInvisibleToZED = "tagInvisibleToZED";
  11. public static int arlayer = 30;
  12. public static string ID_arlayer = "arlayer";
  13. }
  14. #if UNITY_EDITOR
  15. [InitializeOnLoad]
  16. public static class ZEDLayersManager
  17. {
  18. static ZEDLayersManager()
  19. {
  20. CreateLayer(ZEDLayers.ID_tagInvisibleToZED, ZEDLayers.tagInvisibleToZED);
  21. CreateLayer(ZEDLayers.ID_arlayer, ZEDLayers.arlayer);
  22. }
  23. public static void CreateLayer(string layerName, int layerIndex)
  24. {
  25. UnityEngine.Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
  26. if (layerIndex < 7 || layerIndex > 31) return; //Invalid ID.
  27. if ((asset != null) && (asset.Length > 0))
  28. {
  29. SerializedObject serializedObject = new SerializedObject(asset[0]);
  30. SerializedProperty layers = serializedObject.FindProperty("layers");
  31. for (int i = 0; i < layers.arraySize; ++i)
  32. {
  33. if (layers.GetArrayElementAtIndex(i).stringValue == layerName)
  34. {
  35. layerIndex = i;
  36. return; // Layer already present, update layerindex value.
  37. }
  38. }
  39. if (layers.GetArrayElementAtIndex(layerIndex).stringValue == "")
  40. {
  41. layers.GetArrayElementAtIndex(layerIndex).stringValue = layerName;
  42. serializedObject.ApplyModifiedProperties();
  43. serializedObject.Update();
  44. if (layers.GetArrayElementAtIndex(layerIndex).stringValue == layerName)
  45. {
  46. return; // to avoid unity locked layer
  47. }
  48. }
  49. }
  50. }
  51. public static void ClearLayer(string layerName)
  52. {
  53. UnityEngine.Object[] asset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/TagManager.asset");
  54. if ((asset != null) && (asset.Length > 0))
  55. {
  56. SerializedObject serializedObject = new SerializedObject(asset[0]);
  57. SerializedProperty layers = serializedObject.FindProperty("layers");
  58. for (int i = 0; i < layers.arraySize; ++i)
  59. {
  60. if (layers.GetArrayElementAtIndex(i).stringValue == layerName)
  61. {
  62. layers.GetArrayElementAtIndex(i).stringValue = "";
  63. }
  64. }
  65. serializedObject.ApplyModifiedProperties();
  66. serializedObject.Update();
  67. }
  68. }
  69. }
  70. #endif