public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var cache = SPGUI.DisableIfPlaying(); /* * Component[] states = null; * var src = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject); * if (src != null) states = ParentComponentStateSupplier<IAIState>.GetComponentsOnTarg(src, false).Cast<Component>().ToArray(); * else states = new Component[] { }; * * var componentLabels = (from c in states select EditorHelper.TempContent(string.Format("{0} ({1})", c.name, c.GetType().Name))).ToArray(); * property.objectReferenceValue = SPEditorGUI.SelectComponentField(position, label, states, componentLabels, property.objectReferenceValue as Component); * * cache.Reset(); */ Component[] states = null; var src = GameObjectUtil.GetGameObjectFromSource(property.serializedObject.targetObject); if (src != null) { states = ParentComponentStateSupplier <IAIState> .GetComponentsOnTarg(src, false).Cast <Component>().Prepend(null).ToArray(); } else { states = new Component[] { } }; var componentLabels = (from c in states select(c != null) ? EditorHelper.TempContent(string.Format("{0} ({1})", c.name, c.GetType().Name)) : EditorHelper.TempContent("...Nothing")).ToArray(); property.objectReferenceValue = SPEditorGUI.SelectComponentField(position, label, states, componentLabels, property.objectReferenceValue as Component); cache.Reset(); } }
protected override void OnSPInspectorGUI() { this.serializedObject.Update(); this.DrawDefaultInspectorExcept(PROP_STATEMACHINE, PROP_STATE, PROP_WAITON); var stateMachineProp = this.serializedObject.FindProperty(PROP_STATEMACHINE); SPEditorGUILayout.PropertyField(stateMachineProp); var src = GameObjectUtil.GetGameObjectFromSource(stateMachineProp.objectReferenceValue); if (src != null) { var states = ParentComponentStateSupplier <IAIState> .GetComponentsOnTarg(src, false).ToArray(); var stateProp = this.serializedObject.FindProperty(PROP_STATE); int index = System.Array.IndexOf(states, stateProp.objectReferenceValue); var names = (from s in states select EditorHelper.TempContent(s.DisplayName)).ToArray(); EditorGUI.BeginChangeCheck(); index = EditorGUILayout.Popup(EditorHelper.TempContent("State"), index, names); if (EditorGUI.EndChangeCheck()) { stateProp.objectReferenceValue = (index >= 0) ? states[index] as UnityEngine.Object : null; } } else { EditorGUILayout.LabelField("State", "*Select a State Machine first*"); } this.DrawPropertyField(PROP_WAITON); this.serializedObject.ApplyModifiedProperties(); }
protected override void OnSPInspectorGUI() { this.serializedObject.Update(); var targ = this.target as AIController; if (targ == null) { return; } this.DrawPropertyField(EditorHelper.PROP_SCRIPT); var sourceProp = this.serializedObject.FindProperty(PROP_STATESOURCE); SPEditorGUILayout.PropertyField(sourceProp); var cache = SPGUI.DisableIfPlaying(); var stateProp = this.serializedObject.FindProperty(PROP_DEFAULTSTATE); switch (sourceProp.GetEnumValue <AIStateMachineSourceMode>()) { case AIStateMachineSourceMode.SelfSourced: { var states = ComponentStateSupplier <IAIState> .GetComponentsOnTarg(targ.gameObject).Cast <Component>().ToArray(); stateProp.objectReferenceValue = SPEditorGUILayout.SelectComponentField(stateProp.displayName, states, stateProp.objectReferenceValue as Component); } break; case AIStateMachineSourceMode.ChildSourced: { var states = ParentComponentStateSupplier <IAIState> .GetComponentsOnTarg(targ.gameObject, false); var names = (from s in states select EditorHelper.TempContent(GameObjectUtil.GetGameObjectFromSource(s).name + " (" + s.GetType().Name + ")")).ToArray(); int i = states.IndexOf(stateProp.objectReferenceValue); i = EditorGUILayout.Popup(EditorHelper.TempContent(stateProp.displayName), i, names); stateProp.objectReferenceValue = (i >= 0) ? states[i] as UnityEngine.Object : null; } break; default: { var states = ArrayUtil.Empty <Component>(); stateProp.objectReferenceValue = SPEditorGUILayout.SelectComponentField(stateProp.displayName, states, stateProp.objectReferenceValue as Component); } break; } cache.Reset(); this.DrawDefaultInspectorExcept(EditorHelper.PROP_SCRIPT, PROP_STATESOURCE, PROP_DEFAULTSTATE); this.serializedObject.ApplyModifiedProperties(); if (Application.isPlaying) { if (targ.States != null && targ.States.Current != null) { var c = targ.States.Current; var msg = string.Format("Currently active state is {0} ({1}).", c.DisplayName, c.GetType().Name); EditorGUILayout.HelpBox(msg, MessageType.Info); } else { EditorGUILayout.HelpBox("Currently active state is null.", MessageType.Info); } } }