Ejemplo n.º 1
0
        /// <summary>
        /// Adds a state to the supplied parent.
        /// Automatically handles undo.
        /// <param name="parent">The ParentBehaviour to add the new state.</param>
        /// <param name="type">The new state type.</param>
        /// <returns>The new created state.<returns>
        /// </summary>
        public static InternalStateBehaviour AddState(ParentBehaviour parent, System.Type type)
        {
            // Validate parameters
            if (type != null && parent != null)
            {
                // Create and register undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterSceneUndo("Create State (" + type.Name + ")");
                #endif

                InternalStateBehaviour newState = parent.AddState(type);


                if (newState != null)
                {
                    // Register undo
                    #if !UNITY_4_0_0 && !UNITY_4_1 && !UNITY_4_2
                    Undo.RegisterCreatedObjectUndo(newState, "Create State (" + type.Name + ")");
                    #endif

                    // The parent is a FSM?
                    var fsm = parent as InternalStateMachine;
                    if (fsm != null)
                    {
                        // The newState is an AnyState?
                        if (newState is InternalAnyState)
                        {
                            // The fsm already has an AnyState?
                            if (fsm.anyState != null)
                            {
                                // Destroy the curren anyState
                                StateUtility.Destroy(fsm.anyState);
                            }

                            // Add the new anyState
                            fsm.anyState = newState as InternalAnyState;
                        }
                        // The start state is null?
                        else if (fsm.startState == null)
                        {
                            // Set the new state as the start state
                            fsm.startState = newState;
                            EditorUtility.SetDirty(fsm);
                        }
                    }

                    // Sets dirty flag
                    EditorUtility.SetDirty(parent.gameObject);
                }

                return(newState);
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a state to the supplied parent.
        /// Automatically handles undo. 
        /// <param name="parent">The ParentBehaviour to add the new state.</param>
        /// <param name="type">The new state type.</param>
        /// <returns>The new created state.<returns>
        /// </summary> 
        public static InternalStateBehaviour AddState (ParentBehaviour parent, System.Type type) {
            // Validate parameters
            if (type != null && parent != null) {

                // Create and register undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterSceneUndo("Create State (" + type.Name + ")");
                #endif

                InternalStateBehaviour newState = parent.AddState(type);

                
                if (newState != null) {
                    // Register undo
                    #if !UNITY_4_0_0 && !UNITY_4_1 && !UNITY_4_2
                    Undo.RegisterCreatedObjectUndo(newState, "Create State (" + type.Name + ")");
                    #endif

                    // The parent is a FSM?
                    var fsm = parent as InternalStateMachine;
                    if (fsm != null) {
                        // The newState is an AnyState?
                        if (newState is InternalAnyState) {
                            // The fsm already has an AnyState?
                            if (fsm.anyState != null)
                                // Destroy the curren anyState
                                StateUtility.Destroy(fsm.anyState);

                            // Add the new anyState
                            fsm.anyState = newState as InternalAnyState;
                        }
                        // The start state is null?
                        else if (fsm.startState == null) {
                            // Set the new state as the start state
                            fsm.startState = newState;
                            EditorUtility.SetDirty(fsm);
                        }
                    }

                    // Sets dirty flag
                    EditorUtility.SetDirty(parent.gameObject);
                }

                return newState;
            }
            return null;
        }