public override void OnGUI(Rect pos, SerializedProperty properties, GUIContent label) { SerializedDelegate target = DrawerHelper.GetParent(properties) as SerializedDelegate; if (target == null) { return; } SerializedProperty targetProperty = properties.FindPropertyRelative("m_Target"); SerializedProperty methodProperty = properties.FindPropertyRelative("m_Method"); SerializedProperty componentProperty = properties.FindPropertyRelative("m_Component"); // pass through label EditorGUIUtility.LookLikeControls(); EditorGUI.LabelField( new Rect(pos.x, pos.y, pos.width / 2, pos.height / rows), label ); // target + method section EditorGUI.indentLevel++; // select target if (EditorGUI.PropertyField(new Rect(pos.x, pos.y += pos.height / rows, pos.width, pos.height / rows), targetProperty)) { Log.Debug("SerializedDelegate", "PropertyField()"); } List <string> components = GetComponents(targetProperty); if (components != null) { int selected = components.IndexOf(componentProperty.stringValue); int select = EditorGUI.Popup(new Rect(pos.x, pos.y += pos.height / rows, pos.width, pos.height / rows), "Component", selected, components.ToArray()); if (select != selected) { componentProperty.stringValue = components[select]; } List <string> methods = GetComponentMethods(target.DelegateType, targetProperty, componentProperty); if (methods != null) { selected = methods.IndexOf(methodProperty.stringValue); select = EditorGUI.Popup(new Rect(pos.x, pos.y += pos.height / rows, pos.width, pos.height / rows), "Method", selected, methods.ToArray()); if (select != selected) { methodProperty.stringValue = methods[select]; } } } EditorGUI.indentLevel--; }
public static EventCallback DeserializeDelegate(SerializedDelegate serial) { EventCallback callback = null; for (int i = 0; i < serial.methods.Length; i += 1) { EventCallback subCallback = (EventCallback)System.Delegate.CreateDelegate(typeof(EventCallback), serial.targets[i], serial.methods[i]); callback += subCallback; } return(callback); }
public static SerializedDelegate SerializeDelegate(EventCallback callback, string eventname) { System.Delegate[] delegates = callback.GetInvocationList(); SerializedDelegate serial = new SerializedDelegate() { eventname = eventname, targets = new Object[delegates.Length], methods = new string[delegates.Length] }; for (int i = 0; i < delegates.Length; i += 1) { serial.targets[i] = (UnityEngine.Object)delegates[i].Target; serial.methods[i] = delegates[i].Method.Name; } return(serial); }