/// <summary> /// Lists the components. /// </summary> static void ListComponents(UnityEngine.Object theTarget, KGFEvent.KGFEventData theEventData) { GameObject aGameObject = theEventData.GetGameObject(); if (aGameObject != null) { // search monobehaviours on game object MonoBehaviour [] aComponentList = aGameObject.GetComponents<MonoBehaviour>(); string [] aComponentNamesList = GetComponentNames(aComponentList); // search for currently selected monobehaviour int anOldIndex = -1; for (int i=0; i<aComponentNamesList.Length; i++) { if (aComponentNamesList[i] == theEventData.itsComponentName) { anOldIndex = i; } } // draw user selection int aNewIndex = EditorGUILayout.Popup("itsScript", anOldIndex, aComponentNamesList); // set target to dirty if changed if (aNewIndex != anOldIndex) { theEventData.itsMethodName = ""; theEventData.SetParameters(new KGFEvent.EventParameter[0]); EditorUtility.SetDirty(theTarget); } // set new value if selection is valid and list methods if (aNewIndex >= 0) { theEventData.itsComponentName = aComponentNamesList[aNewIndex]; if (aComponentList[aNewIndex] != null) { ListMethods(theTarget,theEventData,aComponentList[aNewIndex].GetType()); } } } }
/// <summary> /// Show input fields for all parameters of the given method /// </summary> /// <param name="theMethod">The reflected method info</param> static void ListParameters(UnityEngine.Object theTarget, KGFEvent.KGFEventData theEventData, MethodInfo theMethod) { if (theEventData.GetDirectPassThroughMode()) return; // if (!theEventData.GetDisplayParametersInInspector()) // return; EditorGUILayout.BeginHorizontal(); // EditorGUILayout.Space(); EditorGUILayout.BeginVertical(); { // get parameters from method ParameterInfo [] aMethodParametersList = theMethod.GetParameters(); // get saved parameter values from event_generic object KGFEvent.EventParameter []aParametersList = theEventData.GetParameters(); // reinit parameter values if length counts do not match if (theEventData.itsParameters.Length != aMethodParametersList.Length) { aParametersList = new KGFEvent.EventParameter[aMethodParametersList.Length]; for (int j=0;j<aParametersList.Length;j++) { aParametersList[j] = new KGFEvent.EventParameter(); } } KGFEvent.EventParameterType[] aTypes = theEventData.GetParameterLinkTypes(); string[] aPopUpArray = new string[0]; // draw input field for each parameter for (int i=0;i<aMethodParametersList.Length;i++) { GUILayout.BeginHorizontal(); { // check if there are linked parameters with the current parameter type if (theEventData.GetSupportsRuntimeParameterInfos()) { List<string> aPopupList = new List<string>(); foreach (KGFEvent.EventParameterType aParameterType in aTypes) { if (aParameterType.GetIsMatchingType(aMethodParametersList[i].ParameterType)) { aPopupList.Add(string.Format("{0} ({1})",aParameterType.itsName,aParameterType.itsTypeName)); } } aPopUpArray = aPopupList.ToArray(); } // if (i < aMethodParametersList.Length-1) { GUILayout.Label("├",GUILayout.Width(20)); } // else // { // GUILayout.Label("└",GUILayout.Width(20)); // } if (theEventData.GetIsParameterLinked(i)) { // if the parameter is linked, let the user choose the linked parameter GUILayout.Label(aMethodParametersList[i].Name+"="); int aIndexNew = EditorGUILayout.Popup(theEventData.GetParameterLink(i),aPopUpArray); if (aIndexNew != theEventData.GetParameterLink(i)) { theEventData.SetParameterLink(i,aIndexNew); EditorUtility.SetDirty(theTarget); } } else { // if not linked, enable default input field DrawSingleParameter(theTarget,theEventData,aMethodParametersList[i],aParametersList[i],i); } // let the user enable/disable linked state for this parameter, if it is supported and there are parameters with matching types if (theEventData.GetSupportsRuntimeParameterInfos() && aPopUpArray.Length > 0) { bool aBoolValue = GUILayout.Toggle(theEventData.GetIsParameterLinked(i),"Link",GUILayout.Width(40)); if (aBoolValue != theEventData.GetIsParameterLinked(i)) { theEventData.SetIsParameterLinked(i,aBoolValue); EditorUtility.SetDirty(theTarget); } }else { theEventData.SetIsParameterLinked(i,false); } } GUILayout.EndHorizontal(); } // save parameter values to the event generic object theEventData.SetParameters(aParametersList); } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); }
public static void EventGui(UnityEngine.Object theTarget, KGFEvent.KGFEventData theData, bool theDirectObject,params string[] theRuntimeObjectList) { if (theDirectObject) { bool aValue = EditorGUILayout.Toggle("Runtime Object Search",theData.itsRuntimeObjectSearch); if (aValue != theData.itsRuntimeObjectSearch) { theData.itsRuntimeObjectSearch = aValue; // set target to dirty EditorUtility.SetDirty(theTarget); } }else { theData.itsRuntimeObjectSearch = true; } if (theData.itsRuntimeObjectSearch) { if (theRuntimeObjectList.Length == 0) { string aValueString = EditorGUILayout.TextField("Type",theData.itsRuntimeObjectSearchType); if (aValueString != theData.itsRuntimeObjectSearchType) { theData.itsRuntimeObjectSearchType = aValueString; // set target to dirty EditorUtility.SetDirty(theTarget); } }else { int aSelectedIndex = 0; for (int i=0;i<theRuntimeObjectList.Length;i++) { if (theRuntimeObjectList[i] == theData.itsRuntimeObjectSearchType) { aSelectedIndex = i; break; } } aSelectedIndex = EditorGUILayout.Popup(aSelectedIndex,theRuntimeObjectList); if (theData.itsRuntimeObjectSearchType != theRuntimeObjectList[aSelectedIndex]) { theData.itsRuntimeObjectSearchType = theRuntimeObjectList[aSelectedIndex]; // set target to dirty EditorUtility.SetDirty(theTarget); } } string aValueFilter = EditorGUILayout.TextField("Gameobject Filter",theData.itsRuntimeObjectSearchFilter); if (aValueFilter != theData.itsRuntimeObjectSearchFilter) { theData.itsRuntimeObjectSearchFilter = aValueFilter; EditorUtility.SetDirty(theTarget); } ListMethods(theTarget,theData,theData.GetRuntimeType()); }else { // draw object selection GameObject aNewGameObject = (GameObject)EditorGUILayout.ObjectField("itsObject", theData.itsObject, typeof(GameObject),true); if (aNewGameObject != theData.itsObject) { // clear selection of sub items if game object changed theData.itsObject = aNewGameObject; theData.itsComponentName = ""; theData.itsMethodName = ""; theData.SetParameters(new KGFEvent.EventParameter[0]); // set target to dirty EditorUtility.SetDirty(theTarget); } ListComponents(theTarget,theData); } }
/// <summary> /// Lists the methods. /// </summary> /// <param name='theComponent'> /// The component. /// </param> static void ListMethods(UnityEngine.Object theTarget, KGFEvent.KGFEventData theEventData, Type theType) { if (theType != null) { // Get methods of the Monobehaviour MethodInfo[] aMethodList = KGFEvent.GetMethods(theType,theEventData); string [] aMethodNamesList = GetMethodNames(aMethodList); // search for currently selected method int anOldIndex = -1; for (int i=0; i<aMethodNamesList.Length; i++) { if (aMethodNamesList[i] == theEventData.itsMethodName) { anOldIndex = i; } } // draw user selection int aNewIndex = EditorGUILayout.Popup("Method", anOldIndex, aMethodNamesList); // set target to dirty if value changed if (aNewIndex != anOldIndex) { theEventData.SetParameters(new KGFEvent.EventParameter[0]); EditorUtility.SetDirty(theTarget); } // set new method name if selection is valid if (aNewIndex >= 0) { theEventData.itsMethodName = aMethodNamesList[aNewIndex]; theEventData.itsMethodNameShort = aMethodList[aNewIndex].Name; ListParameters(theTarget,theEventData,aMethodList[aNewIndex]); } } }