public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var attrib = this.attribute as DefaultOrConfiguredAttribute; if (attrib == null) { SPEditorGUI.DefaultPropertyField(position, property, label); return; } if (attrib.DrawAsDefault(EditorHelper.GetPropertyValue(property))) { var value = attrib.GetValueToDisplayAsDefault(); label.text += " (Default Value)"; EditorGUI.BeginChangeCheck(); value = SPEditorGUI.DefaultPropertyField(position, label, value, attrib.FieldType); if (EditorGUI.EndChangeCheck()) { EditorHelper.SetPropertyValue(property, value); } } else { var r0 = new Rect(position.xMin, position.yMin, Mathf.Max(0f, position.width - SPEditorGUI.X_BTN_WIDTH), position.height); SPEditorGUI.DefaultPropertyField(r0, property, label); var w = position.width = r0.width; if (w > 1f) { var r1 = new Rect(position.xMax - w, position.yMin, w, EditorGUIUtility.singleLineHeight); if (SPEditorGUI.XButton(r1, "Reset to default value.")) { EditorHelper.SetPropertyValue(property, attrib.GetDefaultValue()); } } } }
public void OnGUI(Rect position, SerializedProperty property) { //if (property.propertyType != SerializedPropertyType.ObjectReference || !TypeUtil.IsType(_restrictionType, typeof(Component), typeof(IComponent))) if (property.propertyType != SerializedPropertyType.ObjectReference || (!this.AllowNonComponents && !(TypeUtil.IsType(_restrictionType, typeof(Component)) || _restrictionType.IsInterface))) { this.DrawAsMismatchedAttribute(position, property); return; } this.Init(); GameObject targGo; if (this.ForceOnlySelf) { targGo = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject); if (targGo == null) { this.DrawAsMismatchedAttribute(position, property); return; } if (property.objectReferenceValue == null) { property.objectReferenceValue = targGo.GetComponent(_restrictionType); } } targGo = GameObjectUtil.GetGameObjectFromSource(property.objectReferenceValue); if (property.objectReferenceValue == null) { //SPEditorGUI.DefaultPropertyField(position, property, label); if (!this.ForceOnlySelf) { this.DrawObjectRefField(position, property); } else { EditorGUI.LabelField(position, "Malformed serializable field."); } } else if (this.AllowNonComponents) { if (targGo == null) { this.DrawObjectRefField(position, property); } else { this.ChoiceSelector.BeforeGUI(this, property, this.ComponentRestrictionType); var components = this.ChoiceSelector.GetComponents(); var fullsize = position; if (components.Length == 0 || (this.ShowXButton && SPEditorGUI.XButton(ref position, "Clear Selected Object", this.XButtonOnRightSide))) { property.objectReferenceValue = null; fullsize = this.DrawDotDotButton(fullsize, property); this.DrawObjectRefField(fullsize, property); this.ChoiceSelector.GUIComplete(property, -1); } else { position = this.DrawDotDotButton(position, property); var names = this.ChoiceSelector.GetPopupEntries(); System.Array.Resize(ref names, names.Length + 1); names[names.Length - 1] = EditorHelper.TempContent(targGo.name + " (...GameObject)"); int oi = (property.objectReferenceValue is GameObject) ? names.Length - 1 : this.ChoiceSelector.GetPopupIndexOfComponent(property.objectReferenceValue as Component); int ni = EditorGUI.Popup(position, oi, names); if (oi != ni) { if (ni == names.Length - 1) { property.objectReferenceValue = targGo; } else { property.objectReferenceValue = this.ChoiceSelector.GetComponentAtPopupIndex(ni); } //if (ni < components.Length) // property.objectReferenceValue = this.ChoiceSelector.GetComponentAtPopupIndex(ni); //else // property.objectReferenceValue = targGo; } this.ChoiceSelector.GUIComplete(property, ni); } } } else { this.ChoiceSelector.BeforeGUI(this, property, this.ComponentRestrictionType); var components = this.ChoiceSelector.GetComponents(); var fullsize = position; if (components.Length == 0 || (this.ShowXButton && SPEditorGUI.XButton(ref position, "Clear Selected Object", this.XButtonOnRightSide))) { property.objectReferenceValue = null; fullsize = this.DrawDotDotButton(fullsize, property); this.DrawObjectRefField(fullsize, property); this.ChoiceSelector.GUIComplete(property, -1); } else { position = this.DrawDotDotButton(position, property); var names = this.ChoiceSelector.GetPopupEntries(); int oi = this.ChoiceSelector.GetPopupIndexOfComponent(property.objectReferenceValue as Component); int ni = EditorGUI.Popup(position, oi, names); if (oi != ni) { property.objectReferenceValue = this.ChoiceSelector.GetComponentAtPopupIndex(ni); } this.ChoiceSelector.GUIComplete(property, ni); } } }
private void DrawAdvanced_CallMethodOnSelected(Rect area, SerializedProperty property) { //Draw Target /* * var targRect = new Rect(area.xMin, area.yMin, area.width, EditorGUIUtility.singleLineHeight); * var targProp = property.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); * var targLabel = EditorHelper.TempContent("Triggerable Target"); * var targGo = GameObjectUtil.GetGameObjectFromSource(targProp.objectReferenceValue); * var newTargGo = EditorGUI.ObjectField(targRect, targLabel, targGo, typeof(GameObject), true) as GameObject; * if (newTargGo != targGo) * { * targGo = newTargGo; * targProp.objectReferenceValue = (targGo != null) ? targGo.transform : null; * } * * var targCompPopupRect = new Rect(area.xMin, targRect.yMax, area.width, EditorGUIUtility.singleLineHeight); * if (targGo != null) * { * EditorGUI.BeginChangeCheck(); * var selectedComp = SPEditorGUI.SelectComponentFromSourceField(targCompPopupRect, "Target Component", targGo, targProp.objectReferenceValue as Component); * if (EditorGUI.EndChangeCheck()) * { * targProp.objectReferenceValue = selectedComp; * } * } * else * { * EditorGUI.LabelField(targCompPopupRect, "Target Component", "(First Select a Target)"); * } */ var targRect = new Rect(area.xMin, area.yMin, area.width, EditorGUIUtility.singleLineHeight); var targProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); targRect = EditorGUI.PrefixLabel(targRect, EditorHelper.TempContent("Triggerable Target")); var targGo = GameObjectUtil.GetGameObjectFromSource(targProp.objectReferenceValue); if (targGo != null) { if (SPEditorGUI.XButton(ref targRect, "Clear Selected Object", true)) { targProp.objectReferenceValue = null; goto DrawMethodName; } EditorGUI.BeginChangeCheck(); var selectedComp = SPEditorGUI.SelectComponentFromSourceField(targRect, GUIContent.none, targGo, targProp.objectReferenceValue as Component); if (EditorGUI.EndChangeCheck()) { targProp.objectReferenceValue = selectedComp; } } else { targProp.objectReferenceValue = TargetObjectField(targRect, GUIContent.none, targProp.objectReferenceValue); } //Draw Method Name DrawMethodName: //var methNameRect = new Rect(area.xMin, targCompPopupRect.yMax, area.width, EditorGUIUtility.singleLineHeight); var methNameRect = new Rect(area.xMin, targRect.yMax, area.width, EditorGUIUtility.singleLineHeight); System.Reflection.MemberInfo selectedMember = null; if (targProp.objectReferenceValue != null) { var methProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_METHODNAME); //var tp = targProp.objectReferenceValue.GetType(); //var members = GetAvailableMethods(tp).ToArray(); //var members = com.spacepuppy.Dynamic.DynamicUtil.GetEasilySerializedMembers(targProp.objectReferenceValue, System.Reflection.MemberTypes.Method).ToArray(); var members = com.spacepuppy.Dynamic.DynamicUtil.GetEasilySerializedMembers(targProp.objectReferenceValue, System.Reflection.MemberTypes.All, spacepuppy.Dynamic.DynamicMemberAccess.Write).ToArray(); System.Array.Sort(members, (a, b) => string.Compare(a.Name, b.Name, true)); var memberNames = members.Select((m) => m.Name).ToArray(); int index = System.Array.IndexOf(memberNames, methProp.stringValue); index = EditorGUI.Popup(methNameRect, new GUIContent("Method", "The method/prop on the target to call."), index, (from n in memberNames select new GUIContent(n)).ToArray()); methProp.stringValue = (index >= 0) ? memberNames[index] : null; selectedMember = (index >= 0) ? members[index] : null; } else { EditorGUI.Popup(methNameRect, new GUIContent("Method", "The method/prop on the target to call."), -1, new GUIContent[0]); } property.serializedObject.ApplyModifiedProperties(); //Draw Triggerable Arg var parr = (selectedMember != null) ? com.spacepuppy.Dynamic.DynamicUtil.GetDynamicParameterInfo(selectedMember) : null; if (parr == null || parr.Length == 0) { //NO PARAMETERS _callMethodModeExtraLines = 1; var argRect = new Rect(area.xMin, methNameRect.yMax, area.width, EditorGUIUtility.singleLineHeight); var argArrayProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLEARGS); if (argArrayProp.arraySize > 0) { argArrayProp.arraySize = 0; argArrayProp.serializedObject.ApplyModifiedProperties(); } var cache = SPGUI.Disable(); EditorGUI.LabelField(argRect, GUIContent.none, new GUIContent("*Zero Parameter Count*")); cache.Reset(); } else { //MULTIPLE PARAMETERS - special case, does not support trigger event arg _callMethodModeExtraLines = parr.Length; var argArrayProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLEARGS); if (argArrayProp.arraySize != parr.Length) { argArrayProp.arraySize = parr.Length; argArrayProp.serializedObject.ApplyModifiedProperties(); } EditorGUI.indentLevel++; for (int i = 0; i < parr.Length; i++) { var paramType = parr[i].ParameterType; var argRect = new Rect(area.xMin, methNameRect.yMax + i * EditorGUIUtility.singleLineHeight, area.width, EditorGUIUtility.singleLineHeight); var argProp = argArrayProp.GetArrayElementAtIndex(i); if (paramType == typeof(object)) { //draw the default variant as the method accepts anything _variantDrawer.RestrictVariantType = false; _variantDrawer.ForcedObjectType = null; _variantDrawer.OnGUI(argRect, argProp, EditorHelper.TempContent("Arg " + i.ToString() + ": " + parr[i].ParameterName, "A parameter to be passed to the method if needed.")); } else { _variantDrawer.RestrictVariantType = true; _variantDrawer.TypeRestrictedTo = paramType; _variantDrawer.ForcedObjectType = (paramType.IsInterface || TypeUtil.IsType(paramType, typeof(Component))) ? paramType : null; _variantDrawer.OnGUI(argRect, argProp, EditorHelper.TempContent("Arg " + i.ToString() + ": " + parr[i].ParameterName, "A parameter to be passed to the method if needed.")); } } EditorGUI.indentLevel--; } }
private void DrawAdvanced_TriggerSelected(Rect area, SerializedProperty property) { /* * //Draw Target * var targRect = new Rect(area.xMin, area.yMin, area.width, EditorGUIUtility.singleLineHeight); * var targProp = property.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); * var targLabel = EditorHelper.TempContent("Triggerable Target"); * var targGo = GameObjectUtil.GetGameObjectFromSource(targProp.objectReferenceValue); * var newTargGo = EditorGUI.ObjectField(targRect, targLabel, targGo, typeof(GameObject), true) as GameObject; * if (newTargGo != targGo) * { * targGo = newTargGo; * targProp.objectReferenceValue = (targGo != null) ? targGo.GetComponent<ITriggerableMechanism>() as Component : null; * } * * var targCompPopupRect = new Rect(area.xMin, targRect.yMax, area.width, EditorGUIUtility.singleLineHeight); * if (targProp.objectReferenceValue != null) * { * var selectedType = targProp.objectReferenceValue.GetType(); * var availableMechanismTypes = (from c in targGo.GetComponents<ITriggerableMechanism>() select c.GetType()).ToArray(); * var availableMechanismTypeNames = availableMechanismTypes.Select((tp) => tp.Name).ToArray(); * * var index = System.Array.IndexOf(availableMechanismTypes, selectedType); * EditorGUI.BeginChangeCheck(); * index = EditorGUI.Popup(targCompPopupRect, "Target Component", index, availableMechanismTypeNames); * if (EditorGUI.EndChangeCheck()) * { * targProp.objectReferenceValue = (index >= 0) ? targGo.GetComponent(availableMechanismTypes[index]) : null; * } * } * else * { * EditorGUI.LabelField(targCompPopupRect, "Target Component", "(First Select a Target)"); * } * * //Draw Triggerable Arg * var argRect = new Rect(area.xMin, targCompPopupRect.yMax, area.width - ARG_BTN_WIDTH, EditorGUIUtility.singleLineHeight); * var btnRect = new Rect(argRect.xMax, argRect.yMin, ARG_BTN_WIDTH, EditorGUIUtility.singleLineHeight); * var argArrayProp = property.FindPropertyRelative(TriggerTargetPropertyDrawer.PROP_TRIGGERABLEARGS); * if (argArrayProp.arraySize == 0) * { * EditorGUI.LabelField(argRect, _defaultArgLabel, _undefinedArgLabel); * if (GUI.Button(btnRect, _argBtnLabel)) * { * argArrayProp.arraySize = 1; * argArrayProp.serializedObject.ApplyModifiedProperties(); * } * } * else * { * if (argArrayProp.arraySize > 1) argArrayProp.arraySize = 1; * var argProp = argArrayProp.GetArrayElementAtIndex(0); * //EditorGUI.PropertyField(argRect, argProp, _defaultArgLabel); * _variantDrawer.RestrictVariantType = false; * _variantDrawer.ForcedObjectType = null; * _variantDrawer.OnGUI(argRect, argProp, _defaultArgLabel); * * if (GUI.Button(btnRect, _argBtnLabel)) * { * argArrayProp.arraySize = 0; * argArrayProp.serializedObject.ApplyModifiedProperties(); * } * } */ var targRect = new Rect(area.xMin, area.yMin, area.width, EditorGUIUtility.singleLineHeight); var targProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLETARG); targRect = EditorGUI.PrefixLabel(targRect, EditorHelper.TempContent("Triggerable Target")); //validate if (!GameObjectUtil.IsGameObjectSource(targProp.objectReferenceValue) && !(targProp.objectReferenceValue is ITriggerable)) { targProp.objectReferenceValue = null; } //draw obj field if (targProp.objectReferenceValue != null) { if (SPEditorGUI.XButton(ref targRect, "Clear Selected Object", true)) { targProp.objectReferenceValue = null; goto DrawTriggerableArg; } var targObj = targProp.objectReferenceValue; var availableMechanisms = ObjUtil.GetAllFromSource <ITriggerable>(targObj); var availableMechanismTypeNames = availableMechanisms.Select((o) => EditorHelper.TempContent(o.GetType().Name)).ToArray(); var index = System.Array.IndexOf(availableMechanisms, targObj); EditorGUI.BeginChangeCheck(); index = EditorGUI.Popup(targRect, GUIContent.none, index, availableMechanismTypeNames); if (EditorGUI.EndChangeCheck()) { targObj = (index >= 0) ? availableMechanisms[index] as UnityEngine.Object : null; targProp.objectReferenceValue = targObj; } } else { EditorGUI.BeginChangeCheck(); var targObj = TargetObjectField(targRect, GUIContent.none, targProp.objectReferenceValue); if (EditorGUI.EndChangeCheck()) { targObj = ObjUtil.GetAsFromSource <ITriggerable>(targObj) as UnityEngine.Object; targProp.objectReferenceValue = targObj; } } //Draw Triggerable Arg DrawTriggerableArg: var argRect = new Rect(area.xMin, targRect.yMax, area.width - ARG_BTN_WIDTH, EditorGUIUtility.singleLineHeight); var btnRect = new Rect(argRect.xMax, argRect.yMin, ARG_BTN_WIDTH, EditorGUIUtility.singleLineHeight); var argArrayProp = property.FindPropertyRelative(EventTriggerTargetPropertyDrawer.PROP_TRIGGERABLEARGS); if (argArrayProp.arraySize == 0) { EditorGUI.LabelField(argRect, _defaultArgLabel, _undefinedArgLabel); if (GUI.Button(btnRect, _argBtnLabel)) { argArrayProp.arraySize = 1; argArrayProp.serializedObject.ApplyModifiedProperties(); } } else { if (argArrayProp.arraySize > 1) { argArrayProp.arraySize = 1; } var argProp = argArrayProp.GetArrayElementAtIndex(0); //EditorGUI.PropertyField(argRect, argProp, _defaultArgLabel); _variantDrawer.RestrictVariantType = false; _variantDrawer.ForcedObjectType = null; _variantDrawer.OnGUI(argRect, argProp, _defaultArgLabel); if (GUI.Button(btnRect, _argBtnLabel)) { argArrayProp.arraySize = 0; argArrayProp.serializedObject.ApplyModifiedProperties(); } } }