EasyTouchTriggerInspector.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using HedgehogTeam.EasyTouch;
  8. #if UNITY_5_3
  9. using UnityEditor.SceneManagement;
  10. #endif
  11. [CustomEditor(typeof(EasyTouchTrigger))]
  12. public class EasyTouchTriggerInspector : Editor {
  13. public override void OnInspectorGUI(){
  14. EasyTouchTrigger t = (EasyTouchTrigger)target;
  15. string[] eventNames = Enum.GetNames( typeof(EasyTouch.EvtType) ) ;
  16. eventNames[0] = "Add new event";
  17. #region Event properties
  18. GUILayout.Space(5f);
  19. for (int i=1;i<eventNames.Length;i++){
  20. EasyTouch.EvtType ev = (EasyTouch.EvtType)Enum.Parse( typeof(EasyTouch.EvtType), eventNames[i]);
  21. int result = t.receivers.FindIndex(
  22. delegate(EasyTouchTrigger.EasyTouchReceiver e){
  23. return e.eventName == ev;
  24. }
  25. );
  26. if (result>-1){
  27. TriggerInspector( ev,t);
  28. }
  29. }
  30. #endregion
  31. #region Add Event
  32. GUI.backgroundColor = Color.green;
  33. int index = EditorGUILayout.Popup("", 0, eventNames,"Button");
  34. GUI.backgroundColor = Color.white;
  35. if (index!=0){
  36. //AddEvent((EasyTouch.EventName)Enum.Parse( typeof(EasyTouch.EventName), eventNames[index]),t );
  37. t.AddTrigger( (EasyTouch.EvtType)Enum.Parse( typeof(EasyTouch.EvtType), eventNames[index]));
  38. EditorPrefs.SetBool( eventNames[index], true);
  39. }
  40. #endregion
  41. if (GUI.changed){
  42. EditorUtility.SetDirty(t);
  43. #if UNITY_5_3
  44. EditorSceneManager.MarkSceneDirty( EditorSceneManager.GetActiveScene());
  45. #endif
  46. }
  47. }
  48. private void TriggerInspector(EasyTouch.EvtType ev, EasyTouchTrigger t){
  49. bool folding = EditorPrefs.GetBool( ev.ToString() );
  50. folding = HTGuiTools.BeginFoldOut( ev.ToString(),folding,false);
  51. EditorPrefs.SetBool( ev.ToString(), folding);
  52. if (folding){
  53. HTGuiTools.BeginGroup();
  54. int i=0;
  55. while (i<t.receivers.Count){
  56. if (t.receivers[i].eventName == ev){
  57. GUI.color = new Color(0.8f,0.8f,0.8f,1);
  58. HTGuiTools.BeginGroup(5);
  59. GUI.color = Color.white;
  60. EditorGUILayout.BeginHorizontal();
  61. t.receivers[i].enable = HTGuiTools.Toggle("Enable",t.receivers[i].enable,55,true);
  62. t.receivers[i].name = EditorGUILayout.TextField("",t.receivers[i].name, GUILayout.MinWidth(130));
  63. // Delete
  64. GUILayout.FlexibleSpace();
  65. if (HTGuiTools.Button("X",Color.red,19)){
  66. t.receivers[i] = null;
  67. t.receivers.RemoveAt( i );
  68. EditorGUILayout.EndHorizontal();
  69. return;
  70. }
  71. EditorGUILayout.EndHorizontal();
  72. EditorGUILayout.Space();
  73. // Restriced
  74. //t.receivers[i].restricted = HTGuiTools.Toggle("Restricted to gameobject",t.receivers[i].restricted,true);
  75. t.receivers[i].triggerType = (EasyTouchTrigger.ETTType)EditorGUILayout.EnumPopup("Testing on",t.receivers[i].triggerType );
  76. EditorGUILayout.BeginHorizontal();
  77. t.receivers[i].restricted = EditorGUILayout.Toggle("",t.receivers[i].restricted ,(GUIStyle)"Radio" ,GUILayout.Width(15));
  78. EditorGUILayout.LabelField("Only if on me (requiered a collider)");
  79. EditorGUILayout.EndHorizontal();
  80. EditorGUILayout.BeginHorizontal();
  81. t.receivers[i].restricted = !EditorGUILayout.Toggle("",!t.receivers[i].restricted ,(GUIStyle)"Radio",GUILayout.Width(15));
  82. EditorGUILayout.LabelField("All the time, or other object");
  83. EditorGUILayout.EndHorizontal();
  84. if (!t.receivers[i].restricted){
  85. t.receivers[i].gameObject = (GameObject)EditorGUILayout.ObjectField("Other object",t.receivers[i].gameObject,typeof(GameObject),true);
  86. }
  87. EditorGUILayout.Space();
  88. EditorGUILayout.Space();
  89. t.receivers[i].otherReceiver = HTGuiTools.Toggle("Other receiver",t.receivers[i].otherReceiver,true);
  90. if (t.receivers[i].otherReceiver){
  91. t.receivers[i].gameObjectReceiver = (GameObject)EditorGUILayout.ObjectField("Receiver",t.receivers[i].gameObjectReceiver,typeof(GameObject),true);
  92. }
  93. // Method Name
  94. EditorGUILayout.BeginHorizontal();
  95. t.receivers[i].methodName = EditorGUILayout.TextField("Method name",t.receivers[i].methodName);
  96. // Methode helper
  97. string[] mNames = null;
  98. if (!t.receivers[i].otherReceiver || (t.receivers[i].otherReceiver && t.receivers[i].gameObjectReceiver == null) ){
  99. mNames = GetMethod( t.gameObject);
  100. }
  101. else if ( t.receivers[i].otherReceiver && t.receivers[i].gameObjectReceiver != null){
  102. mNames = GetMethod( t.receivers[i].gameObjectReceiver);
  103. }
  104. int index = EditorGUILayout.Popup("", -1, mNames,"Button",GUILayout.Width(20));
  105. if (index>-1){
  106. t.receivers[i].methodName = mNames[index];
  107. }
  108. EditorGUILayout.EndHorizontal();
  109. // Parameter
  110. t.receivers[i].parameter = (EasyTouchTrigger.ETTParameter) EditorGUILayout.EnumPopup("Parameter to send",t.receivers[i].parameter);
  111. HTGuiTools.EndGroup();
  112. }
  113. i++;
  114. }
  115. }
  116. else{
  117. HTGuiTools.BeginGroup();
  118. }
  119. HTGuiTools.EndGroup(false);
  120. if (!GUILayout.Toggle(true,"+","ObjectPickerTab")){
  121. t.AddTrigger( ev);
  122. }
  123. GUILayout.Space(5f);
  124. }
  125. private void AddEvent(EasyTouch.EvtType ev, EasyTouchTrigger t){
  126. EasyTouchTrigger.EasyTouchReceiver r = new EasyTouchTrigger.EasyTouchReceiver();
  127. r.enable = true;
  128. r.restricted = true;
  129. r.eventName = ev;
  130. r.gameObject = t.gameObject;
  131. t.receivers.Add( r );
  132. }
  133. private string[] GetMethod(GameObject obj){
  134. List<string> methodName = new List<string>();
  135. Component[] allComponents = obj.GetComponents<Component>();
  136. if (allComponents.Length>0){
  137. foreach( Component comp in allComponents){
  138. if (comp!=null){
  139. if (comp.GetType().IsSubclassOf( typeof(MonoBehaviour))){
  140. MethodInfo[] methodInfos = comp.GetType().GetMethods();
  141. foreach( MethodInfo methodInfo in methodInfos){
  142. if ((methodInfo.DeclaringType.Namespace == null) || (!methodInfo.DeclaringType.Namespace.Contains("Unity") && !methodInfo.DeclaringType.Namespace.Contains("System"))){
  143. if (methodInfo.IsPublic){
  144. methodName.Add( methodInfo.Name );
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. //
  153. return methodName.ToArray();
  154. }
  155. }