/// <summary>Creates a ClassEventBinder instance to handle binding methods for a specific class.</summary> /// <param name="TargetClassType">The class to bind methods for.</param> public ClassEventBinder(Type TargetClassType) { //Copies information ClassType = TargetClassType; ClassName = TargetClassType.Name; //Gets all method overloads marked as Bindable MethodInfo[] AllOverloads = TargetClassType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where((MethodInfo x) => x.GetCustomAttributes(typeof(BindableMethod), false).Length > 0).ToArray(); //Creates a subset of unique methods, removing extra overloads MethodInfo[] UniqueMethods = AllOverloads.DistinctBy((MethodInfo x) => x.Name).OrderBy((MethodInfo x) => x.Name).ToArray(); //Creates BindableMethods for each method group BindableMethods = new MethodEventBinder[UniqueMethods.Length]; for (int i = 0; i < UniqueMethods.Length; i++) { BindableMethods[i] = new MethodEventBinder(AllOverloads.Where((MethodInfo x) => x.Name == UniqueMethods[i].Name).ToArray()); } }
//Draws Playlist inspector public override void OnInspectorGUI() { if (!CreatingEvent) { CreatingEvent |= GUILayout.Button("Create new Event"); } else { SelectedTrigger = (EventTriggerType)EditorGUILayout.EnumPopup("Event Trigger Type", SelectedTrigger); int NewClassIndex = EditorGUILayout.Popup("Manager", SelectedClassIndex, ClassNames); if (NewClassIndex != SelectedClassIndex) { SelectedMethodIndex = 0; SelectedOverloadIndex = 0; } SelectedClassIndex = NewClassIndex; ClassEventBinder SelectedClass = ClassBinders[SelectedClassIndex]; int NewMethodIndex = EditorGUILayout.Popup("Method", SelectedMethodIndex, SelectedClass.MethodNameList); if (NewMethodIndex != SelectedMethodIndex) { SelectedOverloadIndex = 0; } SelectedMethodIndex = NewMethodIndex; MethodEventBinder SelectedMethod = SelectedClass.BindableMethods[SelectedMethodIndex]; if (SelectedMethod.MethodOverloads.Length > 1) { SelectedOverloadIndex = EditorGUILayout.Popup("Overload", SelectedOverloadIndex, SelectedMethod.OverloadNameList); } else { SelectedOverloadIndex = 0; } OverloadEventBinder SelectedOverload = SelectedMethod.MethodOverloads[SelectedOverloadIndex]; EditorGUILayout.LabelField(SelectedOverload.FullDisplayName, EditorStyles.wordWrappedMiniLabel); if (SelectedOverload != LastSelectedOverload) { LastSelectedOverload = SelectedOverload; CurrentParams = new object[SelectedOverload.MethodParamaters.Length]; for (int i = 0; i < CurrentParams.Length; i++) { ParameterInfo Param = SelectedOverload.MethodParamaters[i]; Type ParamType = Param.ParameterType; if (!DBNull.Value.Equals(Param.DefaultValue)) { CurrentParams[i] = Param.DefaultValue; } else { if (ParamType.IsValueType) { CurrentParams[i] = Activator.CreateInstance(ParamType); } else if (ParamType == typeof(string)) { CurrentParams[i] = ""; } else { CurrentParams[i] = null; } } } } for (int i = 0; i < CurrentParams.Length; i++) { ParameterInfo Param = SelectedOverload.MethodParamaters[i]; if (Param.ParameterType == typeof(int)) { CurrentParams[i] = EditorGUILayout.IntField(Param.Name, (int)CurrentParams[i]); } else if (Param.ParameterType == typeof(float)) { CurrentParams[i] = EditorGUILayout.FloatField(Param.Name, (float)CurrentParams[i]); } else if (Param.ParameterType == typeof(string)) { CurrentParams[i] = EditorGUILayout.TextField(Param.Name, (string)CurrentParams[i]); } else if (Param.ParameterType == typeof(bool)) { CurrentParams[i] = EditorGUILayout.Toggle(Param.Name, (bool)CurrentParams[i]); } else if (Param.ParameterType == typeof(Vector3)) { CurrentParams[i] = EditorGUILayout.Vector3Field(Param.Name, (Vector3)CurrentParams[i]); } else if (typeof(UnityEngine.Object).IsAssignableFrom(Param.ParameterType)) { CurrentParams[i] = EditorGUILayout.ObjectField(Param.Name, (UnityEngine.Object)CurrentParams[i], Param.ParameterType, !(typeof(ScriptableObject).IsAssignableFrom(Param.ParameterType) || EditorUtility.IsPersistent(target))); } else { EditorGUILayout.LabelField(CurrentParams[i] == null ? "null" : CurrentParams[i].ToString()); } } GUILayout.BeginHorizontal(); if (GUILayout.Button("Bind Event")) { BindedEvent NewBindedEvent = SelectedClass.CreateAndBindEvent(SelectedTrigger, SelectedOverload, CurrentParams); //serializedObject.FindProperty("AllEvents").InsertArrayElementAtIndex(0); //serializedObject.FindProperty("AllEvents").GetArrayElementAtIndex(0).objectReferenceValue = NewBindedEvent; ((AMPEventBinder)target).AllEvents.Add(NewBindedEvent); CreatingEvent = false; } if (GUILayout.Button("Cancel")) { CreatingEvent = false; } GUILayout.EndHorizontal(); } for (int i = 0; i < ((AMPEventBinder)target).AllEvents.Count; i++) { EditorGUILayout.LabelField((((AMPEventBinder)target).AllEvents[i].EventCallback != null).ToString()); } serializedObject.ApplyModifiedProperties(); base.DrawDefaultInspector(); }