GameObjectActivatorEditor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace TheraBytes.BetterUi.Editor
  9. {
  10. [CustomEditor(typeof(GameObjectActivator)), CanEditMultipleObjects]
  11. public class GameObjectActivatorrEditor : UnityEditor.Editor
  12. {
  13. GameObjectActivator activator;
  14. SerializedProperty settingsFallback, settingsList;
  15. void OnEnable()
  16. {
  17. activator = base.target as GameObjectActivator;
  18. this.settingsFallback = serializedObject.FindProperty("settingsFallback");
  19. this.settingsList = serializedObject.FindProperty("customSettings");
  20. }
  21. public override void OnInspectorGUI()
  22. {
  23. bool tmp = activator.EditorPreview;
  24. activator.EditorPreview = EditorGUILayout.ToggleLeft("Editor Preview", activator.EditorPreview);
  25. if(activator.EditorPreview && tmp != activator.EditorPreview)
  26. {
  27. activator.Apply();
  28. }
  29. ScreenConfigConnectionHelper.DrawGui("Settings", settingsList, ref settingsFallback, DrawSettings);
  30. }
  31. private void DrawSettings(string configName, SerializedProperty settings)
  32. {
  33. SerializedProperty active = settings.FindPropertyRelative("ActiveObjects");
  34. SerializedProperty inactive = settings.FindPropertyRelative("InactiveObjects");
  35. EditorGUILayout.BeginVertical("box");
  36. DrawList("Active Objects", active, inactive);
  37. DrawList("Inactive Objects", inactive, active);
  38. EditorGUILayout.EndVertical();
  39. serializedObject.ApplyModifiedProperties();
  40. }
  41. void DrawList(string label, SerializedProperty list, SerializedProperty otherList)
  42. {
  43. EditorGUILayout.LabelField(label, EditorStyles.boldLabel);
  44. EditorGUI.indentLevel++;
  45. int i;
  46. for(i = 0; i < list.arraySize; i++)
  47. {
  48. SerializedProperty prop = list.GetArrayElementAtIndex(i);
  49. EditorGUILayout.BeginHorizontal();
  50. EditorGUI.BeginDisabledGroup(true);
  51. EditorGUILayout.PropertyField(prop, GUIContent.none);
  52. //EditorGUILayout.ObjectField(prop.objectReferenceValue, typeof(GameObject), true);
  53. EditorGUI.EndDisabledGroup();
  54. if (GUILayout.Button("x", GUILayout.Width(18)))
  55. {
  56. list.DeleteArrayElementAtIndex(i); // nulls the object
  57. list.DeleteArrayElementAtIndex(i); // deletes the entry
  58. i--;
  59. }
  60. EditorGUILayout.EndHorizontal();
  61. }
  62. EditorGUILayout.Space();
  63. GameObject obj = EditorGUILayout.ObjectField("Add Object", null, typeof(GameObject), true) as GameObject;
  64. if(obj != null)
  65. {
  66. if(CheckObject(obj, list, otherList))
  67. {
  68. list.InsertArrayElementAtIndex(i);
  69. SerializedProperty prop = list.GetArrayElementAtIndex(i);
  70. prop.objectReferenceValue = obj;
  71. }
  72. }
  73. EditorGUILayout.Space();
  74. EditorGUI.indentLevel--;
  75. }
  76. bool CheckObject(GameObject go, SerializedProperty list, SerializedProperty otherList)
  77. {
  78. #if UNITY_2018_2_OR_NEWER
  79. if(PrefabUtility.GetCorrespondingObjectFromSource(go) == null && PrefabUtility.GetCorrespondingObjectFromSource(go) != null)
  80. #else
  81. if(PrefabUtility.GetPrefabParent(go) == null && PrefabUtility.GetPrefabObject(go) != null)
  82. #endif
  83. {
  84. Debug.LogError("Object must be part of the scene.");
  85. return false;
  86. }
  87. for (int i = 0; i < list.arraySize; i++)
  88. {
  89. if (list.GetArrayElementAtIndex(i).objectReferenceValue == go)
  90. {
  91. Debug.Log("Object already added.");
  92. return false;
  93. }
  94. }
  95. for (int i = 0; i < otherList.arraySize; i++)
  96. {
  97. if (otherList.GetArrayElementAtIndex(i).objectReferenceValue == go)
  98. {
  99. Debug.LogError("Object already added to the other list.");
  100. return false;
  101. }
  102. }
  103. Transform parentOrSelf = activator.transform;
  104. while(parentOrSelf != null)
  105. {
  106. if(parentOrSelf.gameObject == go)
  107. {
  108. Debug.LogError("Object to add must not be this object or a parent of this object.");
  109. return false;
  110. }
  111. parentOrSelf = parentOrSelf.parent;
  112. }
  113. return true;
  114. }
  115. }
  116. }