/// <summary> /// Searches for the supplied eventName in the state transitions; if found a transition between states will be performed. /// <param name="eventName">The name of the event.</param> /// <returns>True if the event was used; False otherwise.</returns> /// </summary> public bool SendEvent(string eventName) { if (enabled) { // The eventName is a valid string and the state has a valid blackboard? if (!string.IsNullOrEmpty(eventName) && blackboard != null) { FsmEvent fsmEvent = null; // Get the fsmEvent InternalBlackboard myBlackboard = blackboard; if (myBlackboard != null) { fsmEvent = myBlackboard.GetFsmEvent(eventName); } // Try to get the fsmEvent in the GlobalBlackboard if (fsmEvent == null && InternalGlobalBlackboard.Instance != null) { fsmEvent = InternalGlobalBlackboard.Instance.GetFsmEvent(eventName); } // The fsmEvent is valid? if (fsmEvent != null) { return(SendEvent(fsmEvent.id)); } } else { throw new System.ArgumentException("Parameter cannot be null or empty", "eventName"); } } return(false); }
/// <summary> /// Constructor for variables that will be added to a . /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public Variable(string name, InternalBlackboard blackboard, int id) { m_Blackboard = blackboard; m_IsConstant = false; m_ID = id; this.name = name; }
/// <summary> /// BehaviourMachine callback to update the members. /// </summary> public virtual void UpdateLogic() { // Validate Blackboard if (m_Blackboard == null) { m_Blackboard = GetComponent <InternalBlackboard>(); } // Validate name if (string.IsNullOrEmpty(m_StateName)) { m_StateName = GetType().Name; } // Validate parent if (m_Parent != null && m_Parent.gameObject != gameObject) { m_Parent = m_LastParent != null && m_LastParent.gameObject == gameObject ? m_LastParent : null; this.enabled = m_Parent == null; } // Check for invalid events destinations for (int i = 0; i < m_Transitions.Length; i++) { // The destination state is not in this game object? var destination = m_Transitions[i].destination; if (destination != null && destination.parent != m_Parent) { m_Transitions[i].destination = null; } } }
/// <summary> /// Displays a context menu to add variables to a blackboard. /// <param name="blackboard">The target blackboard to add a new variable.</param> /// </summary> public static void OnAddContextMenu(InternalBlackboard blackboard) { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; var menu = new GenericMenu(); menu.AddItem(new GUIContent("Float"), false, delegate() { BlackboardUtility.AddFloatVar(blackboard); }); menu.AddItem(new GUIContent("Int"), false, delegate() { BlackboardUtility.AddIntVar(blackboard); }); menu.AddItem(new GUIContent("Bool"), false, delegate() { BlackboardUtility.AddBoolVar(blackboard); }); menu.AddItem(new GUIContent("String"), false, delegate() { BlackboardUtility.AddStringVar(blackboard); }); menu.AddItem(new GUIContent("Vector3"), false, delegate() { BlackboardUtility.AddVector3Var(blackboard); }); menu.AddItem(new GUIContent("Rect"), false, delegate() { BlackboardUtility.AddRectVar(blackboard); }); menu.AddItem(new GUIContent("Color"), false, delegate() { BlackboardUtility.AddColorVar(blackboard); }); menu.AddItem(new GUIContent("Quaternion"), false, delegate() { BlackboardUtility.AddQuaternionVar(blackboard); }); menu.AddItem(new GUIContent("GameObject"), false, delegate() { BlackboardUtility.AddGameObjectVar(blackboard); }); menu.AddItem(new GUIContent("Texture"), false, delegate() { BlackboardUtility.AddTextureVar(blackboard); }); menu.AddItem(new GUIContent("Material"), false, delegate() { BlackboardUtility.AddMaterialVar(blackboard); }); menu.AddItem(new GUIContent("Object"), false, delegate() { BlackboardUtility.AddObjectVar(blackboard); }); menu.AddItem(new GUIContent("DynamicList"), false, delegate() { BlackboardUtility.AddDynamicList(blackboard); }); menu.AddItem(new GUIContent("FsmEvent"), false, delegate() { BlackboardUtility.AddFsmEvent(blackboard); }); if (!(blackboard is InternalGlobalBlackboard)) { menu.AddSeparator(""); menu.AddItem(new GUIContent("Global Blackboard"), false, delegate() { EditorApplication.ExecuteMenuItem("Tools/BehaviourMachine/Global Blackboard"); }); } menu.ShowAsContext(); }
/// <summary> /// Adds a new FsmEvent var to the supplied blackboard. /// Automatically handles undo. /// <param name="blackboard">The blackboard to add a new FsmEventVar.</param> /// <returns>The new variable.</returns> /// </summary> public static FsmEvent AddFsmEvent(InternalBlackboard blackboard) { BlackboardUtility.RegisterVariableUndo(blackboard, "Add FsmEvent"); var newVariable = blackboard.AddFsmEvent(); EditorUtility.SetDirty(blackboard); return(newVariable); }
/// <summary> /// Register undo before add a new variable. /// <param name="userData">The blackboard to register undo.</param> /// </summary> static void RegisterVariableUndo(InternalBlackboard blackboard) { #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterUndo(blackboard, "Add Variable"); #else Undo.RecordObject(blackboard, "Add Variable"); #endif }
/// <summary> /// Adds a new bool var to the supplied blackboard. /// Automatically handles undo. /// <param name="blackboard">The blackboard to add a new BoolVar.</param> /// <returns>The new variable.</returns> /// </summary> public static BoolVar AddBoolVar(InternalBlackboard blackboard) { BlackboardUtility.RegisterVariableUndo(blackboard, "Add Bool Variable"); var newVariable = blackboard.AddBoolVar(); EditorUtility.SetDirty(blackboard); return(newVariable); }
/// <summary> /// Register undo before add a new variable. /// <param name="blackboard">The blackboard to register undo.</param> /// <param name="name">The name of the undo.</param> /// </summary> private static void RegisterVariableUndo(InternalBlackboard blackboard, string name) { #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterUndo(blackboard, name); #else Undo.RecordObject(blackboard, name); #endif }
/// <summary> /// Unity callback called when the object is loaded. /// </summary> void OnEnable() { if (target != null) { m_SerialObj = new SerializedObject(target); m_ScriptProperty = m_SerialObj.FindProperty("m_Script"); m_Blackboard = target as InternalBlackboard; m_VariableEditor = new VariableEditor(m_Blackboard); } }
public override Status Update() { // Validate members if (eventToSend.isNone) { return(Status.Error); } storeEventUsed.Value = InternalBlackboard.SendEventToAll(eventToSend.id); return(Status.Success); }
static void EnabledStatesNameGizmo(InternalBlackboard blackboard, GizmoType gizmoType) { // Is in playmode? if (EditorApplication.isPlaying && BehaviourMachinePrefs.enabledStateName) { // The styles is null? if (s_Styles == null) { s_Styles = new GizmoDrawer.Styles(); } // Get root parents var rootParents = blackboard.GetEnabledRootParents(); Camera currentCamera = Camera.current; // There is at least one fsm enabled? if (rootParents.Length <= 0 || currentCamera == null) { return; } // The object is visible by the camera? Vector3 position = blackboard.transform.position; Vector3 viewportPoint = currentCamera.WorldToViewportPoint(position); if (viewportPoint.z <= 0 || !(new Rect(0, 0, 1, 1)).Contains(viewportPoint)) { return; } // Get enabled state names string names = rootParents[0].GetEnabledStateName(); for (int i = 1; i < rootParents.Length; i++) { names += "\n" + rootParents[i].GetEnabledStateName(); } // Handles.Label has an offset bug when working with styles that are not MiddleLeft, bellow is a workaround to center the text. GUIContent nameContent = new GUIContent(names); Vector2 size = s_Styles.enabledStateName.CalcSize(nameContent); Vector3 screenPoint = currentCamera.WorldToScreenPoint(position); position = currentCamera.ScreenToWorldPoint(new Vector3(screenPoint.x - size.x * .5f, screenPoint.y, -screenPoint.z)); // Draw enabled states name Handles.Label(position, nameContent, s_Styles.enabledStateName); } }
/// <summary> /// Update the hideFlag of all states on the same GameObject as the supplied blackboard. /// <param name ="blackboard">The target blackboard.</param> /// </summary> static void OnBlackboardHideFlag(InternalBlackboard blackboard) { // It's a blackboard? if (blackboard != null) { // Get the prefab type var prefabType = UnityEditor.PrefabUtility.GetPrefabType(blackboard.gameObject); // Its an instance of a prefab? if (prefabType != UnityEditor.PrefabType.None) { // Get all states in the blackboard InternalStateBehaviour[] states = blackboard.GetComponents <InternalStateBehaviour>(); for (int i = 0; i < states.Length; i++) { OnStateHideFlag(states[i]); } } } }
/// <summary> /// Removes the supplied variable from its blackboard. /// <param name="variable">The variable to be removed.</param> /// <summary> public static void RemoveVariable(Variable variable) { if (variable != null && variable.blackboard != null) { // Get the target blackboard InternalBlackboard blackboard = variable.blackboard; // Register undo #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterUndo(blackboard, "Delete Variable"); #else Undo.RecordObject(blackboard, "Delete Variable"); #endif // Remove variable blackboard.RemoveVariable(variable); // Set dirty EditorUtility.SetDirty(blackboard); } }
static void FsmEvent2ConcreteFsmEvent(InternalBlackboard blackboard, bool isPrefab) { // Get the target serialized object var serializedBlackboard = new SerializedObject(blackboard); // Get the m_FsmEvents property var fsmEventProperty = serializedBlackboard.FindProperty("m_FsmEvents"); // Get the m_ConcreteFsmEvents property var concreteFsmEventProperty = serializedBlackboard.FindProperty("m_ConcreteFsmEvents"); // Copy data if (fsmEventProperty != null && concreteFsmEventProperty != null && (isPrefab || !fsmEventProperty.isInstantiatedPrefab || fsmEventProperty.prefabOverride)) { for (int i = 0; i < fsmEventProperty.arraySize; i++) { // Add new FsmEvent concreteFsmEventProperty.InsertArrayElementAtIndex(i); // Get the new FsmEvent SerializedProperty newFsmProperty = concreteFsmEventProperty.GetArrayElementAtIndex(i); // Get the old FsmEvent SerializedProperty oldFsmProperty = fsmEventProperty.GetArrayElementAtIndex(i); // Update the new FsmEvent properties newFsmProperty.FindPropertyRelative("m_ID").intValue = oldFsmProperty.FindPropertyRelative("m_ID").intValue; newFsmProperty.FindPropertyRelative("m_Name").stringValue = oldFsmProperty.FindPropertyRelative("m_Name").stringValue; newFsmProperty.FindPropertyRelative("m_Blackboard").objectReferenceValue = oldFsmProperty.FindPropertyRelative("m_Blackboard").objectReferenceValue; newFsmProperty.FindPropertyRelative("m_IsConstant").boolValue = oldFsmProperty.FindPropertyRelative("m_IsConstant").boolValue; newFsmProperty.FindPropertyRelative("m_EventId").intValue = oldFsmProperty.FindPropertyRelative("m_EventId").intValue; newFsmProperty.FindPropertyRelative("m_IsSystem").boolValue = oldFsmProperty.FindPropertyRelative("m_IsSystem").boolValue; } // Clear the fsmEvent event property fsmEventProperty.ClearArray(); } // Dispose properties fsmEventProperty.Dispose(); concreteFsmEventProperty.Dispose(); // Update serialized data serializedBlackboard.ApplyModifiedProperties(); }
/// <summary> /// Returns the total height to draw the blackboard variables. /// <param name="blackboard">The target blackboard to calculate the height.</param> /// <returns>The required height to draw all variables in the blackboard.</returns> /// </summary> public static float GetHeight(InternalBlackboard blackboard) { if (blackboard == null) { return(0f); } float height = blackboard.GetFloatsSize() * c_OneLineHeight + blackboard.GetIntsSize() * c_OneLineHeight + blackboard.GetBoolsSize() * c_OneLineHeight + blackboard.GetStringsSize() * c_OneLineHeight + blackboard.GetVector3sSize() * c_OneLineHeight + blackboard.GetRectsSize() * c_TwoLinesHeight + blackboard.GetColorsSize() * c_OneLineHeight + blackboard.GetQuaternionsSize() * c_OneLineHeight + blackboard.GetGameObjectsSize() * c_OneLineHeight + blackboard.GetTexturesSize() * c_OneLineHeight + blackboard.GetMaterialsSize() * c_OneLineHeight + blackboard.GetObjectsSize() * c_TwoLinesHeight + blackboard.GetDynamicListsSize() * c_OneLineHeight + blackboard.GetFsmEventsSize() * c_OneLineHeight; return(height); }
/// <summary> /// Unity callback called when the script is loaded or a value is changed in the inspector (Called in the editor only). /// Workaround to copy component values in editor. /// </summary> public virtual void OnValidate() { // Validate Blackboard if (m_Blackboard == null || m_Blackboard.gameObject != this.gameObject) { m_Blackboard = GetComponent <InternalBlackboard>(); } // The component has been copied to a new game object? if (m_Parent == null || m_Parent.gameObject != gameObject) { UpdateLogic(); } // Workaround to not disable the state in the playmode when editing the inspector if (Application.isPlaying && m_Parent != null && !m_Parent.IsEnabled(this)) { base.enabled = m_Parent == null; // enables/disables the state. } // The user has reverted to prefab values or pasted component values? if (m_CachedInstanceID != this.GetInstanceID()) { m_CachedInstanceID = this.GetInstanceID(); UpdateLogic(); InternalStateBehaviour.s_Refresh = true; } // The user has changed the parent property? if (m_LastParent != m_Parent) { InternalStateBehaviour.s_Refresh = true; } // Set Dirty StateSetDirty(); }
/// <summary> /// Constructor for string variables that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public ConcreteStringVar(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { Value = string.Empty; }
/// <summary> /// Constructor for LevelManager variables that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public LevelManagerVar(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { }
/// <summary> /// Draw the blackboard view. /// <param name="rect">The position to draw the variables.</param> /// <param name="blackboard">The blackboard to be drawn.</param> /// <param name="blackboardHeight">The size needed to show all variables in the blackboard.</param> /// </summary> void DrawBlackboardView(Rect rect, InternalBlackboard blackboard, float blackboardHeight) { // Draw header Rect headerRect = new Rect(rect.x, rect.y, rect.width, blackboardHeaderHeight); if (GUI.Button(headerRect, new GUIContent("Variables [" + blackboard.GetSize().ToString() + "]", "Click to expand/collapse"), s_Styles.blackboardHeader)) { if (Event.current.mousePosition.x >= headerRect.xMax - c_BlackboardHeaderButtonWidth) { BlackboardGUIUtility.OnAddContextMenu(blackboard); m_BlackboardViewIsExpanded = true; } else { m_BlackboardViewIsExpanded = !m_BlackboardViewIsExpanded; } } // Draw plus button headerRect.y += 2f; headerRect.xMin = headerRect.width - c_BlackboardHeaderButtonWidth; GUI.Label(headerRect, s_Styles.iconToolbarPlus); // The blackboard is expanded if (m_BlackboardViewIsExpanded && rect.height - headerRect.height > 0f) { rect.yMin += headerRect.height; // Draw background if (Event.current.type == EventType.Repaint) { s_Styles.blackboardBox.Draw(rect, false, false, false, false); } // Do scroll bar? bool doScroll = blackboardHeight > rect.height; // Scroll bar logic if (doScroll) { // Create a gui group rect.yMin += 2f; rect.yMax -= 2f; GUI.BeginGroup(rect); rect.y = rect.x = 0f; // Get scroll event if (Event.current.type == EventType.ScrollWheel) { m_BlackboardScroll += Event.current.delta.y * 10f; Event.current.Use(); } // Update rect rect.y -= m_BlackboardScroll; rect.width -= 12f; } // Draw variables BlackboardGUIUtility.DrawVariables(rect, blackboard); // Draw scroll bar if (doScroll) { rect.y += m_BlackboardScroll; rect.width += 12f; var scrollPosition = new Rect(rect.x + rect.width - 16f, rect.y, 16f, rect.height); m_BlackboardScroll = GUI.VerticalScrollbar(scrollPosition, m_BlackboardScroll, rect.height, 0f, blackboardHeight); GUI.EndGroup(); } } }
/// <summary> /// Unity callback used to draw controls in the window. /// Draws the toolbar, the gui parent and the blackboard view. /// </summary> void OnGUI() { // Debug.Log(Event.current); // Create style? if (s_Styles == null) { s_Styles = new BehaviourWindow.Styles(); } // Refresh active objects during UndoRedoPerformed command if (!EditorApplication.isPlaying && Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed") { // Reload tree var tree = BehaviourWindow.activeTree; if (tree != null) { tree.LoadNodes(); // Force node selection update activeNodeID = activeNodeID; } // Update the active parent var lastSelectedParent = activeParent; var selectedState = Selection.activeObject as InternalStateBehaviour; if (lastSelectedParent == null || (selectedState != null && selectedState.parent != lastSelectedParent)) { var selectedParent = selectedState as ParentBehaviour; if (selectedParent != null) { activeParent = selectedParent; } } Refresh(); // Repaint(); return; } // Refresh window? if (InternalStateBehaviour.refresh) { // Workaround for the missing revert prefab button Unity callback... Refresh(); } // Draw toolbar DoStatusBarGUI(); DrawSearch(); // Draw the gui parent and the blackboard view GUI.BeginGroup(new Rect(0, EditorStyles.toolbar.fixedHeight - 2 + 25, position.width, position.height), ""); { if (m_ParentGUI != null && activeParent != null) { // Get the blackboard InternalBlackboard activeBlackboard = EditorUtility.InstanceIDToObject(m_ActiveBlackboardID) as InternalBlackboard; // Get the blackboard view rect and height float blackboardHeight = BlackboardGUIUtility.GetHeight(activeBlackboard) + 2f; float blackboardViewHeight = GetBlackboardViewHeight(blackboardHeight); Rect blackboardViewRect = new Rect(0f, position.height - blackboardViewHeight - 17f, 260f, blackboardViewHeight); // Show Scroll View? if (BehaviourMachinePrefs.showScrollView) { blackboardViewRect.y -= 16f; } // Get event type EventType eventType = Event.current.type; // Should ignore event? if (ShouldIgnoreEvent(blackboardViewRect)) { Event.current.type = EventType.Ignore; } m_ParentGUI.OnGUIBeforeWindows(); BeginWindows(); m_ParentGUI.OnGUIWindows(); EndWindows(); m_ParentGUI.OnGUIAfterWindows(); // Restore event? if (Event.current.type != EventType.Used) { Event.current.type = eventType; } // Draw variables if (activeBlackboard != null) { DrawBlackboardView(blackboardViewRect, activeBlackboard, blackboardHeight); } } // Show notification message else if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { ShowNotification(new GUIContent("Select a Game Object and right click in this window")); } } GUI.EndGroup(); // Show context menu? if (Event.current.type == EventType.ContextClick) { OnContextMenu(); Event.current.Use(); } }
/// <summary> /// Constructor for FsmEvent that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// <param name="isSystem">Returns true if this is a system event; otherwise false.</param> /// </summary> public FsmEvent(string name, InternalBlackboard blackboard, int id, bool isSystem) : base(name, blackboard, id) { m_IsSystem = isSystem; }
/// <summary> /// Constructor for Stater variables that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public StaterVar(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { }
/// <summary> /// Constructor for int variables that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public ConcreteIntVar(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { }
/// <inheritdoc/> public ConcreteDynamicList(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { }
/// <summary> /// Constructor for ConcreteFsmEvent that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// <param name="isSystem">Returns true if this is a system event; otherwise false.</param> /// </summary> public ConcreteFsmEvent(string name, InternalBlackboard blackboard, int id, bool isSystem) : base(name, blackboard, id, isSystem) { }
/// <summary> /// Constructor for Texture variables that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public TextureVar(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { }
/// <summary> /// Paste the state in StateUtility.stateToPaste in the supplied fsm. /// <param name="gameObject">The target gameObject.</param> /// <param name="originalStates">The original states.</param> /// <param name="parent">Optionally parent for the cloned states.</param> /// </summary> public static void CloneStates(GameObject gameObject, InternalStateBehaviour[] originalStates, ParentBehaviour parent) { if (gameObject != null && originalStates != null && originalStates.Length > 0) { var orginalClone = new Dictionary <InternalStateBehaviour, InternalStateBehaviour>(); var originalFsm = parent != null ? originalStates[0].parent as InternalStateMachine : null; var newFsm = parent as InternalStateMachine; InternalStateBehaviour startState = null, concurrentState = null; InternalAnyState anyState = null; // Copy blackboard data? var newBlackboard = gameObject.GetComponent <InternalBlackboard>(); if (newBlackboard == null) { // Get the original blackboard InternalBlackboard originalBlackboard = originalStates[0].GetComponent <InternalBlackboard>(); #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterSceneUndo("Paste State"); // Create the new blacbkoard newBlackboard = gameObject.AddComponent(originalBlackboard.GetType()) as InternalBlackboard; #else // Create the new blacbkoard newBlackboard = gameObject.AddComponent(originalBlackboard.GetType()) as InternalBlackboard; if (newBlackboard != null) { Undo.RegisterCreatedObjectUndo(newBlackboard, "Paste State"); } #endif // Copy serialized values EditorUtility.CopySerialized(originalBlackboard, newBlackboard); } foreach (InternalStateBehaviour state in originalStates) { // Don't clone AnyState in StateMachines if (state != null && (newFsm == null || !(state is InternalAnyState) || newFsm.anyState == null)) { #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterSceneUndo("Paste State"); // Create a new state var newState = gameObject.AddComponent(state.GetType()) as InternalStateBehaviour; #else // Create a new state var newState = gameObject.AddComponent(state.GetType()) as InternalStateBehaviour; if (newState != null) { Undo.RegisterCreatedObjectUndo(newState, "Paste State"); } #endif if (newState != null) { // Store state orginalClone.Add(state, newState); // Copy serialized values EditorUtility.CopySerialized(state, newState); // Update blackboard if (state.gameObject != newState.gameObject) { var serialObj = new SerializedObject(newState); serialObj.FindProperty("m_Blackboard").objectReferenceValue = newBlackboard; serialObj.ApplyModifiedProperties(); serialObj.Dispose(); } // Update the AnyState, StartState and ConcurrentState if (newState is InternalStateMachine) { var fsm = newState as InternalStateMachine; fsm.startState = null; fsm.concurrentState = null; fsm.anyState = null; } EditorUtility.SetDirty(newState); // Set new parent if (parent != null) { newState.parent = parent; // Update position if (parent == state.parent) { newState.position += new Vector2(20f, 20f); } } else { newState.parent = null; } // Saves state and sets dirty flag INodeOwner nodeOwner = newState as INodeOwner; if (nodeOwner != null) { nodeOwner.LoadNodes(); StateUtility.SetDirty(nodeOwner); } else { EditorUtility.SetDirty(newState); } // Try to get the StartState, AnyState and ConcurrentState if (originalFsm != null) { if (originalFsm.startState == state) { startState = newState; } if (anyState == null) { anyState = newState as InternalAnyState; } if (originalFsm.concurrentState == state) { concurrentState = newState; } } } } } // Set StartState, AnyState and ConcurrentState if (newFsm != null) { if (newFsm.startState == null) { newFsm.startState = startState; } if (newFsm.anyState == null) { newFsm.anyState = anyState; } if (newFsm.concurrentState == null) { newFsm.concurrentState = concurrentState; } EditorUtility.SetDirty(newFsm); } // Try to update the transitions' destination foreach (KeyValuePair <InternalStateBehaviour, InternalStateBehaviour> pair in orginalClone) { InternalStateBehaviour state = pair.Key; InternalStateBehaviour newState = pair.Value; // Update the newState transition for (int i = 0; i < newState.transitions.Length && i < state.transitions.Length; i++) { // The original destination is valid? if (state.transitions[i].destination != null && orginalClone.ContainsKey(state.transitions[i].destination)) { newState.transitions[i].destination = orginalClone[state.transitions[i].destination]; } } if (newState is ParentBehaviour) { var stateAsParent = state as ParentBehaviour; // Removes the newState from the children state to avoid an infinite loop List <InternalStateBehaviour> children = stateAsParent.states; if (children.Contains(newState)) { children.Remove(newState); } StateUtility.CloneStates(newState.gameObject, children.ToArray(), newState as ParentBehaviour); } EditorUtility.SetDirty(newState); } EditorUtility.SetDirty(gameObject); } }
/// <summary> /// Constructor for Object variables that will be added to a blackboard. /// <param name="name">The name of the variable.</param> /// <param name="blackboard">The variable blackboard.</param> /// <param name="id">The unique id of the variable</param> /// </summary> public ObjectVar(string name, InternalBlackboard blackboard, int id) : base(name, blackboard, id) { }
/// <summary> /// Class constructor. /// <param name="stateTransition">The target transition.</param> /// <param name="destination">The target destination.</param> /// <param name="index">The transition index.</param> /// <param name="blackboard">The state blackboard.</param> /// </summary> public TransitionGUI(StateTransition stateTransition, InternalStateBehaviour destination, int index, InternalBlackboard blackboard) { m_Transition = stateTransition; m_Destination = destination; // It's a global event? if (m_Transition.eventID < 0) { if (InternalGlobalBlackboard.Instance != null) { m_FsmEvent = InternalGlobalBlackboard.Instance.GetFsmEvent(m_Transition.eventID); } } // It's a local variable and the blackboard is not null? else if (m_Transition.eventID > 0 && blackboard != null) { m_FsmEvent = blackboard.GetFsmEvent(m_Transition.eventID); } // Get the transition arrow vertical offset m_VerticalOffset = StateGUI.defaultHeight + TransitionGUI.defaultHeight * (index + .35f); }
/// <summary> /// Constructor for none variables. /// </summary> public Variable() { m_Blackboard = null; m_IsConstant = false; m_Name = " "; }