private void CreateState(Vector2 mousePosition) { Undo.RegisterCompleteObjectUndo(new UnityEngine.Object[] { stateMachine, nodesInfo, this }, "State Created"); nodesInfo.StatesCount++; nodesInfo.Names.Add("New State"); nodesInfo.Rects.Add(new Rect(mousePosition, NODE_SIZE)); transitions.Add(new List <int>()); List <Action[]> actions = new List <Action[]>(StateMachineReflections.GetActions(stateMachine)); actions.Add(new Action[0]); List <bool[]> isDurationEnd = new List <bool[]>(StateMachineReflections.GetIsDurationsEnd(stateMachine)); isDurationEnd.Add(new bool[0]); List <Duration> durations = new List <Duration>(StateMachineReflections.GetDurations(stateMachine)); durations.Add(new ConstantDuration()); List <Condition[][]> conditions = new List <Condition[][]>(StateMachineReflections.GetConditions(stateMachine)); conditions.Add(new Condition[0][]); SaveStateMachine(actions.ToArray(), isDurationEnd.ToArray(), durations.ToArray(), conditions.ToArray()); if (nodesInfo.StatesCount == 1) { StateMachineReflections.SetStartStateIndex(stateMachine, startStateIndex = 0); } }
private void DeleteState(int stateIndex) { Undo.RegisterCompleteObjectUndo(new UnityEngine.Object[] { stateMachine, nodesInfo, this }, "State Deleted"); DeleteStateReferencesInTransitions(stateIndex); nodesInfo.StatesCount--; nodesInfo.Names.RemoveAt(stateIndex); nodesInfo.Rects.RemoveAt(stateIndex); transitions.RemoveAt(stateIndex); List <Action[]> actions = new List <Action[]>(StateMachineReflections.GetActions(stateMachine)); actions.RemoveAt(stateIndex); List <bool[]> isDurationEnd = new List <bool[]>(StateMachineReflections.GetIsDurationsEnd(stateMachine)); isDurationEnd.RemoveAt(0); List <Duration> durations = new List <Duration>(StateMachineReflections.GetDurations(stateMachine)); durations.RemoveAt(0); List <Condition[][]> conditions = new List <Condition[][]>(StateMachineReflections.GetConditions(stateMachine)); conditions.RemoveAt(stateIndex); SaveStateMachine(actions.ToArray(), isDurationEnd.ToArray(), durations.ToArray(), conditions.ToArray()); if (startStateIndex == stateIndex) { StateMachineReflections.SetStartStateIndex(stateMachine, startStateIndex = nodesInfo.StatesCount > 0 ? 0 : -1); } }
private void ProcessContextMenu(Vector2 mousePosition) { GenericMenu genericMenu = new GenericMenu(); int stateIndex = GetStateIndex(mousePosition); if (stateIndex != -1) { genericMenu.AddItem(new GUIContent("Create Transition"), false, () => creatingTransitionFromStateIndex = stateIndex); genericMenu.AddItem(new GUIContent("Set as Start State"), false, () => StateMachineReflections.SetStartStateIndex(stateMachine, startStateIndex = stateIndex)); genericMenu.AddItem(new GUIContent("Delete State"), false, () => DeleteState(stateIndex)); } else { Vector2Int transition = GetTransition(mousePosition); if (transition != new Vector2Int(-1, -1)) { genericMenu.AddItem(new GUIContent("Delete Transition"), false, () => DeleteTransition(transition.x, transition.y)); } else { genericMenu.AddItem(new GUIContent("New State"), false, () => CreateState(mousePosition)); genericMenu.AddItem(new GUIContent("Reset Panning"), false, ResetPanning); } } genericMenu.ShowAsContext(); }