ToString() public method

Convert the delegate to its string representation.
public ToString ( ) : string
return string
	public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label)
	{
		Undo.RecordObject(prop.serializedObject.targetObject, "Delegate Selection");

		SerializedProperty targetProp = prop.FindPropertyRelative("mTarget");
		SerializedProperty methodProp = prop.FindPropertyRelative("mMethodName");

		MonoBehaviour target = targetProp.objectReferenceValue as MonoBehaviour;
		string methodName = methodProp.stringValue;

		EditorGUI.indentLevel = prop.depth;
		EditorGUI.LabelField(position, label);

		Rect line = position;
		line.yMin = position.yMin + lineHeight;
		line.yMax = line.yMin + lineHeight;

		EditorGUI.indentLevel = targetProp.depth;
		target = EditorGUI.ObjectField(line, "Notify", target, typeof(MonoBehaviour), true) as MonoBehaviour;

		if (target != null && target.gameObject != null)
		{
			GameObject go = target.gameObject;
			List<Entry> list = EventDelegateEditor.GetMethods(go);

			int index = 0;
			int choice = 0;

			EventDelegate del = new EventDelegate();
			del.target = target;
			del.methodName = methodName;
			string[] names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);

			line.yMin += lineHeight;
			line.yMax += lineHeight;
			choice = EditorGUI.Popup(line, "Method", index, names);

			if (choice > 0)
			{
				if (choice != index)
				{
					Entry entry = list[choice - 1];
					target = entry.target as MonoBehaviour;
					methodName = entry.name;
				}
			}
		}

		targetProp.objectReferenceValue = target;
		methodProp.stringValue = methodName;
	}
	/// <summary>
	/// Draw an editor field for the Unity Delegate.
	/// </summary>

	static public bool Field (Object undoObject, EventDelegate del, bool removeButton)
	{
		if (del == null) return false;
		bool prev = GUI.changed;
		GUI.changed = false;
		bool retVal = false;
		MonoBehaviour target = del.target;
		bool remove = false;

		if (removeButton && (del.target != null || del.isValid))
		{
			if (del.target == null && del.isValid)
			{
				EditorGUILayout.LabelField("Notify", del.ToString());
			}
			else
			{
				target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
			}

			GUILayout.Space(-20f);
			GUILayout.BeginHorizontal();
			GUILayout.Space(64f);

#if UNITY_3_5
			if (GUILayout.Button("X", GUILayout.Width(20f)))
#else
			if (GUILayout.Button("", "ToggleMixed", GUILayout.Width(20f)))
#endif
			{
				target = null;
				remove = true;
			}
			GUILayout.EndHorizontal();
		}
		else
		{
			target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
		}

		if (remove)
		{
			NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
			del.Clear();
			EditorUtility.SetDirty(undoObject);
		}
		else if (del.target != target)
		{
			NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
			del.target = target;
			EditorUtility.SetDirty(undoObject);
		}

		if (del.target != null && del.target.gameObject != null)
		{
			GameObject go = del.target.gameObject;
			List<Entry> list = GetMethods(go);

			int index = 0;
			string[] names = GetMethodNames(list, del.ToString(), out index);
			int choice = 0;

			GUILayout.BeginHorizontal();
			choice = EditorGUILayout.Popup("Method", index, names);
			GUILayout.Space(18f);
			GUILayout.EndHorizontal();

			if (choice > 0)
			{
				if (choice != index)
				{
					Entry entry = list[choice - 1];
					NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
					del.target = entry.target;
					del.methodName = entry.method.Name;
					EditorUtility.SetDirty(undoObject);
					GUI.changed = prev;
					return true;
				}
			}
		}

		retVal = GUI.changed;
		GUI.changed = prev;
		return retVal;
	}
Beispiel #3
0
	public override void OnGUI (Rect rect, SerializedProperty prop, GUIContent label)
	{
		Undo.RecordObject(prop.serializedObject.targetObject, "Delegate Selection");

		SerializedProperty targetProp = prop.FindPropertyRelative("mTarget");
		SerializedProperty methodProp = prop.FindPropertyRelative("mMethodName");

		MonoBehaviour target = targetProp.objectReferenceValue as MonoBehaviour;
		string methodName = methodProp.stringValue;

		EditorGUI.indentLevel = prop.depth;
		EditorGUI.LabelField(rect, label);

		Rect lineRect = rect;
		lineRect.yMin = rect.yMin + lineHeight;
		lineRect.yMax = lineRect.yMin + lineHeight;

		EditorGUI.indentLevel = targetProp.depth;
		target = EditorGUI.ObjectField(lineRect, "Notify", target, typeof(MonoBehaviour), true) as MonoBehaviour;
		targetProp.objectReferenceValue = target;

		if (target != null && target.gameObject != null)
		{
			GameObject go = target.gameObject;
			List<Entry> list = EventDelegateEditor.GetMethods(go);

			int index = 0;
			int choice = 0;

			EventDelegate del = new EventDelegate();
			del.target = target;
			del.methodName = methodName;
			string[] names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);

			lineRect.yMin += lineHeight;
			lineRect.yMax += lineHeight;
			choice = EditorGUI.Popup(lineRect, "Method", index, names);

			if (choice > 0 && choice != index)
			{
				Entry entry = list[choice - 1];
				target = entry.target as MonoBehaviour;
				methodName = entry.name;
				targetProp.objectReferenceValue = target;
				methodProp.stringValue = methodName;
			}

			SerializedProperty paramArrayProp = prop.FindPropertyRelative("mParameters");
			EventDelegate.Parameter[] ps = del.parameters;

			if (ps != null)
			{
				paramArrayProp.arraySize = ps.Length;
				for (int i = 0; i < ps.Length; i++)
				{
					EventDelegate.Parameter param = ps[i];
					SerializedProperty paramProp = paramArrayProp.GetArrayElementAtIndex(i);
					SerializedProperty objProp = paramProp.FindPropertyRelative("obj");
					SerializedProperty fieldProp = paramProp.FindPropertyRelative("field");

					param.obj = objProp.objectReferenceValue;
					param.field = fieldProp.stringValue;
					Object obj = param.obj;

					lineRect.yMin += lineHeight;
					lineRect.yMax += lineHeight;

					obj = EditorGUI.ObjectField(lineRect, "   Arg " + i, obj, typeof(Object), true);

					objProp.objectReferenceValue = obj;
					del.parameters[i].obj = obj;
					param.obj = obj;

					if (obj == null) continue;

					GameObject selGO = null;
					System.Type type = param.obj.GetType();
					if (type == typeof(GameObject)) selGO = param.obj as GameObject;
					else if (type.IsSubclassOf(typeof(Component))) selGO = (param.obj as Component).gameObject;

					if (selGO != null)
					{
						// Parameters must be exact -- they can't be converted like property bindings
						PropertyReferenceDrawer.filter = param.expectedType;
						PropertyReferenceDrawer.canConvert = false;
						List<PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

						int selection;
						string[] props = EventDelegateEditor.GetNames(ents, NGUITools.GetFuncName(param.obj, param.field), out selection);

						lineRect.yMin += lineHeight;
						lineRect.yMax += lineHeight;
						int newSel = EditorGUI.Popup(lineRect, " ", selection, props);

						if (newSel != selection)
						{
							if (newSel == 0)
							{
								param.obj = selGO;
								param.field = null;

								objProp.objectReferenceValue = selGO;
								fieldProp.stringValue = null;
							}
							else
							{
								param.obj = ents[newSel - 1].target;
								param.field = ents[newSel - 1].name;

								objProp.objectReferenceValue = param.obj;
								fieldProp.stringValue = param.field;
							}
						}
					}
					else if (!string.IsNullOrEmpty(param.field))
						param.field = null;

					PropertyReferenceDrawer.filter = typeof(void);
					PropertyReferenceDrawer.canConvert = true;
				}
			}
		}
	}
Beispiel #4
0
	/// <summary>
	/// Draw an editor field for the Unity Delegate.
	/// </summary>

	static public bool Field (Object undoObject, EventDelegate del, bool removeButton, bool minimalistic)
	{
		if (del == null) return false;
		bool prev = GUI.changed;
		GUI.changed = false;
		bool retVal = false;
		MonoBehaviour target = del.target;
		bool remove = false;

		if (removeButton && (del.target != null || del.isValid))
		{
			if (!minimalistic) NGUIEditorTools.SetLabelWidth(82f);

			if (del.target == null && del.isValid)
			{
				EditorGUILayout.LabelField("Notify", del.ToString());
			}
			else
			{
				target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
			}

			GUILayout.Space(-18f);
			GUILayout.BeginHorizontal();
			GUILayout.Space(70f);

			if (GUILayout.Button("", "ToggleMixed", GUILayout.Width(20f), GUILayout.Height(16f)))
			{
				target = null;
				remove = true;
			}
			GUILayout.EndHorizontal();
		}
		else target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;

		if (remove)
		{
			NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
			del.Clear();
			EditorUtility.SetDirty(undoObject);
		}
		else if (del.target != target)
		{
			NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
			del.target = target;
			EditorUtility.SetDirty(undoObject);
		}

		if (del.target != null && del.target.gameObject != null)
		{
			GameObject go = del.target.gameObject;
			List<Entry> list = GetMethods(go);

			int index = 0;
			string[] names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);
			int choice = 0;

			GUILayout.BeginHorizontal();
			choice = EditorGUILayout.Popup("Method", index, names);
			NGUIEditorTools.DrawPadding();
			GUILayout.EndHorizontal();

			if (choice > 0 && choice != index)
			{
				Entry entry = list[choice - 1];
				NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
				del.target = entry.target as MonoBehaviour;
				del.methodName = entry.name;
				EditorUtility.SetDirty(undoObject);
				retVal = true;
			}

			GUI.changed = false;
			EventDelegate.Parameter[] ps = del.parameters;

			if (ps != null)
			{
				for (int i = 0; i < ps.Length; ++i)
				{
					EventDelegate.Parameter param = ps[i];
					Object obj = EditorGUILayout.ObjectField("   Arg " + i, param.obj, typeof(Object), true);

					if (GUI.changed)
					{
						GUI.changed = false;
						param.obj = obj;
						EditorUtility.SetDirty(undoObject);
					}

					if (obj == null) continue;

					GameObject selGO = null;
					System.Type type = obj.GetType();
					if (type == typeof(GameObject)) selGO = obj as GameObject;
					else if (type.IsSubclassOf(typeof(Component))) selGO = (obj as Component).gameObject;

					if (selGO != null)
					{
						// Parameters must be exact -- they can't be converted like property bindings
						PropertyReferenceDrawer.filter = param.expectedType;
						PropertyReferenceDrawer.canConvert = false;
						List<PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

						int selection;
						string[] props = GetNames(ents, NGUITools.GetFuncName(param.obj, param.field), out selection);

						GUILayout.BeginHorizontal();
						int newSel = EditorGUILayout.Popup(" ", selection, props);
						NGUIEditorTools.DrawPadding();
						GUILayout.EndHorizontal();

						if (GUI.changed)
						{
							GUI.changed = false;

							if (newSel == 0)
							{
								param.obj = selGO;
								param.field = null;
							}
							else
							{
								param.obj = ents[newSel - 1].target;
								param.field = ents[newSel - 1].name;
							}
							EditorUtility.SetDirty(undoObject);
						}
					}
					else if (!string.IsNullOrEmpty(param.field))
					{
						param.field = null;
						EditorUtility.SetDirty(undoObject);
					}

					PropertyReferenceDrawer.filter = typeof(void);
					PropertyReferenceDrawer.canConvert = true;
				}
			}
		}
		else retVal = GUI.changed;
		GUI.changed = prev;
		return retVal;
	}
    /// <summary>
    /// Draw an editor field for the Unity Delegate.
    /// </summary>

    static public bool Field(Object undoObject, EventDelegate del, bool removeButton)
    {
        if (del == null)
        {
            return(false);
        }
        bool prev = GUI.changed;

        GUI.changed = false;
        bool          retVal = false;
        MonoBehaviour target = del.target;
        bool          remove = false;

        if (removeButton && (del.target != null || del.isValid))
        {
            if (del.target == null && del.isValid)
            {
                EditorGUILayout.LabelField("Notify", del.ToString());
            }
            else
            {
                target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
            }

            GUILayout.Space(-20f);
            GUILayout.BeginHorizontal();
            GUILayout.Space(64f);

#if UNITY_3_5
            if (GUILayout.Button("X", GUILayout.Width(20f)))
#else
            if (GUILayout.Button("", "ToggleMixed", GUILayout.Width(20f)))
#endif
            {
                target = null;
                remove = true;
            }
            GUILayout.EndHorizontal();
        }
        else
        {
            target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
        }

        if (remove)
        {
            NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
            del.Clear();
            EditorUtility.SetDirty(undoObject);
        }
        else if (del.target != target)
        {
            NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
            del.target = target;
            EditorUtility.SetDirty(undoObject);
        }

        if (del.target != null && del.target.gameObject != null)
        {
            GameObject   go   = del.target.gameObject;
            List <Entry> list = GetMethods(go);

            int      index  = 0;
            string[] names  = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);
            int      choice = 0;

            GUILayout.BeginHorizontal();
            choice = EditorGUILayout.Popup("Method", index, names);
            GUILayout.Space(18f);
            GUILayout.EndHorizontal();

            if (choice > 0 && choice != index)
            {
                Entry entry = list[choice - 1];
                NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
                del.target     = entry.target as MonoBehaviour;
                del.methodName = entry.name;
                EditorUtility.SetDirty(undoObject);
                retVal = true;
            }

            GUI.changed = false;
            EventDelegate.Parameter[] ps = del.parameters;

            if (ps != null)
            {
                for (int i = 0; i < ps.Length; ++i)
                {
                    EventDelegate.Parameter param = ps[i];

                    Object obj = EditorGUILayout.ObjectField("   Arg " + i, param.obj, typeof(Object), true);

                    if (GUI.changed)
                    {
                        GUI.changed = false;
                        param.obj   = obj;
                        EditorUtility.SetDirty(undoObject);
                    }

                    if (obj == null)
                    {
                        continue;
                    }

                    System.Type type = obj.GetType();

                    GameObject selGO = null;
                    if (type == typeof(GameObject))
                    {
                        selGO = obj as GameObject;
                    }
                    else if (type.IsSubclassOf(typeof(Component)))
                    {
                        selGO = (obj as Component).gameObject;
                    }

                    if (selGO != null)
                    {
                        // Parameters must be exact -- they can't be converted like property bindings
                        PropertyReferenceDrawer.filter     = param.expectedType;
                        PropertyReferenceDrawer.canConvert = false;
                        List <PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

                        int      selection;
                        string[] props = GetNames(ents, NGUITools.GetFuncName(param.obj, param.field), out selection);

                        GUILayout.BeginHorizontal();
                        int newSel = EditorGUILayout.Popup(" ", selection, props);
                        GUILayout.Space(18f);
                        GUILayout.EndHorizontal();

                        if (GUI.changed)
                        {
                            GUI.changed = false;

                            if (newSel == 0)
                            {
                                param.obj   = selGO;
                                param.field = null;
                            }
                            else
                            {
                                param.obj   = ents[newSel - 1].target;
                                param.field = ents[newSel - 1].name;
                            }
                            EditorUtility.SetDirty(undoObject);
                        }
                    }
                    else if (!string.IsNullOrEmpty(param.field))
                    {
                        param.field = null;
                        EditorUtility.SetDirty(undoObject);
                    }

                    PropertyReferenceDrawer.filter     = typeof(void);
                    PropertyReferenceDrawer.canConvert = true;
                }
            }
        }
        else
        {
            retVal = GUI.changed;
        }
        GUI.changed = prev;
        return(retVal);
    }
Beispiel #6
0
	public override void OnGUI (Rect rect, SerializedProperty prop, GUIContent label)
	{
		Undo.RecordObject(prop.serializedObject.targetObject, "Delegate Selection");

		SerializedProperty targetProp = prop.FindPropertyRelative("mTarget");
		SerializedProperty methodProp = prop.FindPropertyRelative("mMethodName");

		MonoBehaviour target = targetProp.objectReferenceValue as MonoBehaviour;
		string methodName = methodProp.stringValue;

		EditorGUI.indentLevel = prop.depth;
		EditorGUI.LabelField(rect, label);

		Rect lineRect = rect;
		lineRect.yMin = rect.yMin + lineHeight;
		lineRect.yMax = lineRect.yMin + lineHeight;

		EditorGUI.indentLevel = targetProp.depth;
		target = EditorGUI.ObjectField(lineRect, "Notify", target, typeof(MonoBehaviour), true) as MonoBehaviour;
		targetProp.objectReferenceValue = target;

		if (target != null && target.gameObject != null)
		{
			GameObject go = target.gameObject;
			List<Entry> list = EventDelegateEditor.GetMethods(go);

			int index = 0;
			int choice = 0;

			EventDelegate del = new EventDelegate();
			del.target = target;
			del.methodName = methodName;
			string[] names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);

			lineRect.yMin += lineHeight;
			lineRect.yMax += lineHeight;
			choice = EditorGUI.Popup(lineRect, "Method", index, names);

			if (choice > 0)
			{
				if (choice != index)
				{
					Entry entry = list[choice - 1];
					target = entry.target as MonoBehaviour;
					methodName = entry.name;
					targetProp.objectReferenceValue = target;
					methodProp.stringValue = methodName;
				}
			}

			// Unfortunately Unity's property drawers only work with UnityEngine.Object-derived types.
			// This means that arrays are not supported. And since EventDelegate is not derived from
			// UnityEngine.Object either, it means that it's not possible to modify the parameter array.
			EditorGUI.BeginDisabledGroup(true);

			//SerializedProperty paramProp = prop.FindPropertyRelative("mParameters");
			EventDelegate.Parameter[] ps = del.parameters;

			if (ps != null)
			{
				for (int i = 0; i < ps.Length; ++i)
				{
					EventDelegate.Parameter param = ps[i];
					lineRect.yMin += lineHeight;
					lineRect.yMax += lineHeight;
					param.obj = EditorGUI.ObjectField(lineRect, "   Arg " + i, param.obj, typeof(Object), true);
					if (param.obj == null) continue;

					GameObject selGO = null;
					System.Type type = param.obj.GetType();
					if (type == typeof(GameObject)) selGO = param.obj as GameObject;
					else if (type.IsSubclassOf(typeof(Component))) selGO = (param.obj as Component).gameObject;

					if (selGO != null)
					{
						// Parameters must be exact -- they can't be converted like property bindings
						PropertyReferenceDrawer.filter = param.expectedType;
						PropertyReferenceDrawer.canConvert = false;
						List<PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

						int selection;
						string[] props = EventDelegateEditor.GetNames(ents, NGUITools.GetFuncName(param.obj, param.field), out selection);

						lineRect.yMin += lineHeight;
						lineRect.yMax += lineHeight;
						int newSel = EditorGUI.Popup(lineRect, " ", selection, props);

						if (newSel != selection)
						{
							if (newSel == 0)
							{
								param.obj = selGO;
								param.field = null;
							}
							else
							{
								param.obj = ents[newSel - 1].target;
								param.field = ents[newSel - 1].name;
							}
						}
					}
					else if (!string.IsNullOrEmpty(param.field))
					{
						param.field = null;
					}

					PropertyReferenceDrawer.filter = typeof(void);
					PropertyReferenceDrawer.canConvert = true;
				}
			}

			EditorGUI.EndDisabledGroup();
		}
		//else paramProp.objectReferenceValue = null;
	}
Beispiel #7
0
    public override void OnGUI(Rect rect, SerializedProperty prop, GUIContent label)
    {
        Undo.RecordObject(prop.serializedObject.targetObject, "Delegate Selection");

        SerializedProperty targetProp = prop.FindPropertyRelative("mTarget");
        SerializedProperty methodProp = prop.FindPropertyRelative("mMethodName");

        MonoBehaviour target     = targetProp.objectReferenceValue as MonoBehaviour;
        string        methodName = methodProp.stringValue;

        EditorGUI.indentLevel = prop.depth;
        EditorGUI.LabelField(rect, label);

        Rect lineRect = rect;

        lineRect.yMin = rect.yMin + lineHeight;
        lineRect.yMax = lineRect.yMin + lineHeight;

        EditorGUI.indentLevel = targetProp.depth;
        target = EditorGUI.ObjectField(lineRect, "Notify", target, typeof(MonoBehaviour), true) as MonoBehaviour;
        targetProp.objectReferenceValue = target;

        if (target != null && target.gameObject != null)
        {
            GameObject   go   = target.gameObject;
            List <Entry> list = EventDelegateEditor.GetMethods(go);

            int index  = 0;
            int choice = 0;

            EventDelegate del = new EventDelegate();
            del.target     = target;
            del.methodName = methodName;
            string[] names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);

            lineRect.yMin += lineHeight;
            lineRect.yMax += lineHeight;
            choice         = EditorGUI.Popup(lineRect, "Method", index, names);

            if (choice > 0 && choice != index)
            {
                Entry entry = list[choice - 1];
                target     = entry.target as MonoBehaviour;
                methodName = entry.name;
                targetProp.objectReferenceValue = target;
                methodProp.stringValue          = methodName;
            }

            SerializedProperty        paramArrayProp = prop.FindPropertyRelative("mParameters");
            EventDelegate.Parameter[] ps             = del.parameters;

            if (ps != null)
            {
                paramArrayProp.arraySize = ps.Length;
                for (int i = 0; i < ps.Length; i++)
                {
                    EventDelegate.Parameter param     = ps[i];
                    SerializedProperty      paramProp = paramArrayProp.GetArrayElementAtIndex(i);
                    SerializedProperty      objProp   = paramProp.FindPropertyRelative("obj");
                    SerializedProperty      fieldProp = paramProp.FindPropertyRelative("field");

                    param.obj   = objProp.objectReferenceValue;
                    param.field = fieldProp.stringValue;
                    Object obj = param.obj;

                    lineRect.yMin += lineHeight;
                    lineRect.yMax += lineHeight;

                    obj = EditorGUI.ObjectField(lineRect, "   Arg " + i, obj, typeof(Object), true);

                    objProp.objectReferenceValue = obj;
                    del.parameters[i].obj        = obj;
                    param.obj = obj;

                    if (obj == null)
                    {
                        continue;
                    }

                    GameObject  selGO = null;
                    System.Type type  = param.obj.GetType();
                    if (type == typeof(GameObject))
                    {
                        selGO = param.obj as GameObject;
                    }
                    else if (type.IsSubclassOf(typeof(Component)))
                    {
                        selGO = (param.obj as Component).gameObject;
                    }

                    if (selGO != null)
                    {
                        // Parameters must be exact -- they can't be converted like property bindings
                        PropertyReferenceDrawer.filter     = param.expectedType;
                        PropertyReferenceDrawer.canConvert = false;
                        List <PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

                        int      selection;
                        string[] props = EventDelegateEditor.GetNames(ents, NGUITools.GetFuncName(param.obj, param.field), out selection);

                        lineRect.yMin += lineHeight;
                        lineRect.yMax += lineHeight;
                        int newSel = EditorGUI.Popup(lineRect, " ", selection, props);

                        if (newSel != selection)
                        {
                            if (newSel == 0)
                            {
                                param.obj   = selGO;
                                param.field = null;

                                objProp.objectReferenceValue = selGO;
                                fieldProp.stringValue        = null;
                            }
                            else
                            {
                                param.obj   = ents[newSel - 1].target;
                                param.field = ents[newSel - 1].name;

                                objProp.objectReferenceValue = param.obj;
                                fieldProp.stringValue        = param.field;
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(param.field))
                    {
                        param.field = null;
                    }

                    PropertyReferenceDrawer.filter     = typeof(void);
                    PropertyReferenceDrawer.canConvert = true;
                }
            }
        }
    }
    /// <summary>
    /// Draw an editor field for the Unity Delegate.
    /// </summary>

    static public bool Field(Object undoObject, EventDelegate del, bool removeButton)
    {
        if (del == null)
        {
            return(false);
        }
        bool prev = GUI.changed;

        GUI.changed = false;
        bool          retVal = false;
        MonoBehaviour target = null;

        if (removeButton && del.target != null)
        {
            target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;

            GUILayout.Space(-20f);
            GUILayout.BeginHorizontal();
            GUILayout.Space(64f);

#if UNITY_3_5
            if (GUILayout.Button("X", GUILayout.Width(20f)))
#else
            if (GUILayout.Button("", "ToggleMixed", GUILayout.Width(20f)))
#endif
            {
                target = null;
            }
            GUILayout.EndHorizontal();
        }
        else
        {
            target = EditorGUILayout.ObjectField("Notify", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
        }

        if (del.target != target)
        {
            NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
            del.target = target;
            EditorUtility.SetDirty(undoObject);
        }

        if (del.target != null && del.target.gameObject != null)
        {
            GameObject   go   = del.target.gameObject;
            List <Entry> list = GetMethods(go);

            int      index  = 0;
            string[] names  = GetMethodNames(list, del.ToString(), out index);
            int      choice = 0;

            GUILayout.BeginHorizontal();
            choice = EditorGUILayout.Popup("Method", index, names);
            GUILayout.Space(18f);
            GUILayout.EndHorizontal();

            if (choice > 0)
            {
                if (choice != index)
                {
                    Entry entry = list[choice - 1];
                    NGUIEditorTools.RegisterUndo("Delegate Selection", undoObject);
                    del.target     = entry.target;
                    del.methodName = entry.method.Name;
                    EditorUtility.SetDirty(undoObject);
                    GUI.changed = prev;
                    return(true);
                }
            }
        }

        retVal      = GUI.changed;
        GUI.changed = prev;
        return(retVal);
    }
Beispiel #9
0
        ////////////////////////////////////////////////////////////////

        public void RemoveListener <T>(EventDelegate <T> listener) where T : GameEvent
        {
            Delegate foundEntry;

            if (m_Delegates.TryGetValue(typeof(T), out foundEntry))
            {
                // Is listener registered?
                Delegate resultDelegate = Delegate.Remove(foundEntry, listener);

                if (resultDelegate == null)
                {
                    // No entry for this event left. Clear it!
                    m_Delegates.Remove(typeof(T));
                }
                else
                {
                    // Update entry for this event
                    m_Delegates[typeof(T)] = resultDelegate;
                }
            }
            else
            {
                Debug.LogWarning("EventManager: A listener tries to unregister, even though he is not registered! " + listener.ToString());
            }
        }
    public override void OnGUI(Rect rect, SerializedProperty prop, GUIContent label)
    {
        Undo.RecordObject(prop.serializedObject.targetObject, "Delegate Selection");

        SerializedProperty targetProp = prop.FindPropertyRelative("mTarget");
        SerializedProperty methodProp = prop.FindPropertyRelative("mMethodName");

        MonoBehaviour target     = targetProp.objectReferenceValue as MonoBehaviour;
        string        methodName = methodProp.stringValue;

        EditorGUI.indentLevel = prop.depth;
        EditorGUI.LabelField(rect, label);

        Rect lineRect = rect;

        lineRect.yMin = rect.yMin + lineHeight;
        lineRect.yMax = lineRect.yMin + lineHeight;

        EditorGUI.indentLevel = targetProp.depth;
        target = EditorGUI.ObjectField(lineRect, "Notify", target, typeof(MonoBehaviour), true) as MonoBehaviour;
        targetProp.objectReferenceValue = target;

        if (target != null && target.gameObject != null)
        {
            GameObject   go   = target.gameObject;
            List <Entry> list = EventDelegateEditor.GetMethods(go);

            int index  = 0;
            int choice = 0;

            EventDelegate del = new EventDelegate();
            del.target     = target;
            del.methodName = methodName;
            string[] names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);

            lineRect.yMin += lineHeight;
            lineRect.yMax += lineHeight;
            choice         = EditorGUI.Popup(lineRect, "Method", index, names);

            if (choice > 0)
            {
                if (choice != index)
                {
                    Entry entry = list[choice - 1];
                    target     = entry.target as MonoBehaviour;
                    methodName = entry.name;
                    targetProp.objectReferenceValue = target;
                    methodProp.stringValue          = methodName;
                }
            }

            // Unfortunately Unity's property drawers only work with UnityEngine.Object-derived types.
            // This means that arrays are not supported. And since EventDelegate is not derived from
            // UnityEngine.Object either, it means that it's not possible to modify the parameter array.
            EditorGUI.BeginDisabledGroup(true);

            //SerializedProperty paramProp = prop.FindPropertyRelative("mParameters");
            EventDelegate.Parameter[] ps = del.parameters;

            if (ps != null)
            {
                for (int i = 0; i < ps.Length; ++i)
                {
                    EventDelegate.Parameter param = ps[i];
                    lineRect.yMin += lineHeight;
                    lineRect.yMax += lineHeight;
                    param.obj      = EditorGUI.ObjectField(lineRect, "   Arg " + i, param.obj, typeof(Object), true);
                    if (param.obj == null)
                    {
                        continue;
                    }

                    GameObject  selGO = null;
                    System.Type type  = param.obj.GetType();
                    if (type == typeof(GameObject))
                    {
                        selGO = param.obj as GameObject;
                    }
                    else if (type.IsSubclassOf(typeof(Component)))
                    {
                        selGO = (param.obj as Component).gameObject;
                    }

                    if (selGO != null)
                    {
                        // Parameters must be exact -- they can't be converted like property bindings
                        PropertyReferenceDrawer.filter     = param.expectedType;
                        PropertyReferenceDrawer.canConvert = false;
                        List <PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

                        int      selection;
                        string[] props = EventDelegateEditor.GetNames(ents, NGUITools.GetFuncName(param.obj, param.field), out selection);

                        lineRect.yMin += lineHeight;
                        lineRect.yMax += lineHeight;
                        int newSel = EditorGUI.Popup(lineRect, " ", selection, props);

                        if (newSel != selection)
                        {
                            if (newSel == 0)
                            {
                                param.obj   = selGO;
                                param.field = null;
                            }
                            else
                            {
                                param.obj   = ents[newSel - 1].target;
                                param.field = ents[newSel - 1].name;
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(param.field))
                    {
                        param.field = null;
                    }

                    PropertyReferenceDrawer.filter     = typeof(void);
                    PropertyReferenceDrawer.canConvert = true;
                }
            }

            EditorGUI.EndDisabledGroup();
        }
        //else paramProp.objectReferenceValue = null;
    }
Beispiel #11
0
    public static bool Field(EventDelegate del, Object undoObject)
    {
        if (null == del)
        {
            return(false);
        }
        bool prev = GUI.changed;

        GUI.changed = false;
        bool          retVal = false;
        MonoBehaviour target = del.target;
        bool          remove = false;

        if (del.target != null || del.isValid)
        {
            EditorGUIUtility.labelWidth = 82f;

            if (null == del.target && del.isValid)
            {
                EditorGUILayout.LabelField("事件", del.ToString());
            }
            else
            {
                target = EditorGUILayout.ObjectField("事件", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
            }
            GUILayout.Space(-18f);
            GUILayout.BeginHorizontal();
            GUILayout.Space(70f);
            if (GUILayout.Button("", "ToggleMixed", GUILayout.Width(20f), GUILayout.Height(16f)))
            {
                target = null;
                remove = true;
            }
            GUILayout.EndHorizontal();
        }
        else
        {
            target = EditorGUILayout.ObjectField("事件", del.target, typeof(MonoBehaviour), true) as MonoBehaviour;
        }
        if (remove)
        {
            del.Clear();
            EditorUtility.SetDirty(undoObject);
        }
        else if (del.target != target)
        {
            del.target = target;
            EditorUtility.SetDirty(undoObject);
        }
        if (del.target != null && del.target.gameObject != null)
        {
            GameObject   go    = del.target.gameObject;
            List <Entry> list  = GetMethods(go);
            int          index = 0;
            string[]     names = PropertyReferenceDrawer.GetNames(list, del.ToString(), out index);
            Debug.Log(del.ToString());
            int choice = 0;

            GUILayout.BeginHorizontal();
            choice = EditorGUILayout.Popup("方法", index, names);
            GUILayout.Space(18f);

            GUILayout.EndHorizontal();
            if (choice > 0 && choice != index)
            {
                Debug.Log(choice + index);
                Entry entry = list[choice - 1];
                del.target     = entry.target as MonoBehaviour;
                del.methodName = entry.name;
                del.extentName = entry.extendName;
                del.ParamTypes = entry.paramTypes;
                EditorUtility.SetDirty(undoObject);
                retVal = true;
            }
            GUI.changed = false;
            EventDelegate.Parameter[] ps = del.parameters;
            if (ps != null)
            {
                for (int i = 0; i < ps.Length; ++i)
                {
                    EventDelegate.Parameter param = ps[i];
                    Type   t      = Type.GetType(del.ParamTypes[i]);
                    Object obj    = null;
                    int    select = 0;
                    int    index1 = 0;
                    if (t != typeof(MonoBehaviour))
                    {
                        if (t == typeof(string))
                        {
                            param.stringValue = EditorGUILayout.TextField(new GUIContent("参数" + i), param.stringValue);
                            if (GUI.changed)
                            {
                                GUI.changed = false;
                                EditorUtility.SetDirty(undoObject);
                            }
                        }
                        else if (t == typeof(int))
                        {
                            param.intValue = EditorGUILayout.IntField("参数" + i, param.intValue);
                            if (GUI.changed)
                            {
                                GUI.changed = false;
                                EditorUtility.SetDirty(undoObject);
                            }
                        }
                        else if (t == typeof(GenericType))
                        {
                            //Debug.Log("fwefer2342343");
                            ////如果是泛型的话
                            //select = EditorGUILayout.Popup(index1,Enum.GetNames(typeof(ParamType)));
                            //switch ((ParamType)select)
                            //{
                            //    case ParamType.Int:
                            //        Debug.Log("fwefwef");
                            //        param.intValue = EditorGUILayout.IntField("参数" + i, param.intValue);
                            //        del.mParamGenericTypes.Add("System.Int");
                            //        break;
                            //    case ParamType.String:
                            //        del.mParamGenericTypes.Add("System.String");
                            //        param.stringValue = EditorGUILayout.TextField(new GUIContent("参数" + i), param.stringValue);
                            //        break;
                            //}
                        }
                        PropertyReferenceDrawer.filter     = typeof(void);
                        PropertyReferenceDrawer.canConvert = true;
                    }
                    else
                    {
                        obj = EditorGUILayout.ObjectField("参数" + i, param.obj, typeof(Object), true);

                        if (GUI.changed)
                        {
                            GUI.changed = false;
                            param.obj   = obj;
                            EditorUtility.SetDirty(undoObject);
                        }
                        if (obj == null)
                        {
                            continue;
                        }

                        GameObject  selGO = null;
                        System.Type type  = obj.GetType();
                        if (type == typeof(GameObject))
                        {
                            selGO = obj as GameObject;
                        }
                        else if (type.IsSubclassOf(typeof(Component)))
                        {
                            selGO = (obj as Component).gameObject;
                        }

                        if (selGO != null)
                        {
                            // Parameters must be exact -- they can't be converted like property bindings
                            PropertyReferenceDrawer.filter     = param.expectedType;
                            PropertyReferenceDrawer.canConvert = false;
                            List <PropertyReferenceDrawer.Entry> ents = PropertyReferenceDrawer.GetProperties(selGO, true, false);

                            int      selection;
                            string[] props = GetNames(ents, EditorTool.GetFuncName(param.obj, param.field), out selection);

                            GUILayout.BeginHorizontal();
                            int newSel = EditorGUILayout.Popup(" ", selection, props);
                            GUILayout.Space(18f);
                            GUILayout.EndHorizontal();

                            if (GUI.changed)
                            {
                                GUI.changed = false;

                                if (newSel == 0)
                                {
                                    param.obj   = selGO;
                                    param.field = null;
                                }
                                else
                                {
                                    param.obj   = ents[newSel - 1].target;
                                    param.field = ents[newSel - 1].name;
                                }
                                EditorUtility.SetDirty(undoObject);
                            }
                        }
                        else if (!string.IsNullOrEmpty(param.field))
                        {
                            param.field = null;
                            EditorUtility.SetDirty(undoObject);
                        }

                        PropertyReferenceDrawer.filter     = typeof(void);
                        PropertyReferenceDrawer.canConvert = true;
                    }
                }
            }
        }
        else
        {
            retVal = GUI.changed;
        }
        GUI.changed = prev;
        return(retVal);
    }