Esempio n. 1
0
            public TimelineState GetTimelineState(GameObject sourceObject = null)
            {
                if (_timelineState == null)
                {
                    if (string.IsNullOrEmpty(_file._filePath))
                    {
                        if (_stateMachine != null)
                        {
                            foreach (TimelineState state in _stateMachine._states)
                            {
                                if (state._stateId == _stateId)
                                {
                                    _timelineState = state;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("TimelineStateRefProperty need to be fixed up by TimelineStateMachine");
                        }
                    }
                    else
                    {
                        TextAsset asset = _file.LoadAsset();
                        _stateMachine = TimelineStateMachine.FromTextAsset(asset, sourceObject);
                        _file.UnloadAsset();
                        _timelineState = _stateMachine.GetTimelineState(_stateId);
                    }
                }

                return(_timelineState);
            }
            public static void FixupStateRefs(TimelineStateMachine timeLineStateMachine, object obj)
            {
                if (obj != null)
                {
                    object[] nodeFieldObjects = SerializedFieldInfo.GetSerializedFieldInstances(obj);

                    foreach (object nodeFieldObject in nodeFieldObjects)
                    {
                        TimelineStateRef stateRefProperty = nodeFieldObject as TimelineStateRef;

                        if (stateRefProperty != null)
                        {
                            stateRefProperty.FixUpRef(timeLineStateMachine);
                        }
                        else
                        {
#if UNITY_EDITOR
                            LocalisedStringRef localisedstring = nodeFieldObject as LocalisedStringRef;

                            if (localisedstring != null)
                            {
                                localisedstring.SetEditorStateMachine(timeLineStateMachine);
                            }
#endif
                        }

                        FixupStateRefs(timeLineStateMachine, nodeFieldObject);
                    }
                }
            }
                public void Save()
                {
                    if (!string.IsNullOrEmpty(_currentFileName))
                    {
                        //If we're in state view first need to apply state changes to state machine view
                        if (_currentMode == eMode.ViewingState)
                        {
                            _editedState.GetEditableObject()._timeline = _stateEditor.ConvertToTimeline();
                        }

                        //Update state machine name to reflect filename
                        TimelineStateMachine stateMachine = ConvertToTimelineStateMachine();
                        stateMachine._name = System.IO.Path.GetFileNameWithoutExtension(_currentFileName);

                        //Save to file
                        SerializeConverter.ToFile(stateMachine, _currentFileName);

                        ClearDirtyFlag();
                        _stateEditor.ClearDirtyFlag();

                        GetEditorWindow().DoRepaint();

                        //Hack, save string on save scene
                        Localisation.SaveStrings();
                    }
                    else
                    {
                        SaveAs();
                    }
                }
            public static void OnTimelineStateStarted(StateMachine stateMachine, TimelineState state, string fileName)
            {
                TimelineStateMachine parentStateMachine = state._debugParentStateMachine;

                if (parentStateMachine != null)
                {
                    if (parentStateMachine != null)
                    {
                        StateInfo stateInfo;

                        if (!_stateMachineMap.TryGetValue(stateMachine.gameObject, out stateInfo))
                        {
                            stateInfo = new StateInfo();
                            _stateMachineMap.Add(stateMachine.gameObject, stateInfo);
                        }

                        stateInfo._stateMachine = parentStateMachine;
                        stateInfo._state        = state;
                        stateInfo._time         = 0.0f;
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            stateInfo._fileName = fileName;
                        }
                    }
                }
            }
Esempio n. 5
0
            private void UpdateStateName()
            {
                _editorStateName = "<none>";

                switch (_editorLinkType)
                {
                case eType.External:
                {
                    if (_file._editorAsset != null)
                    {
                        TimelineStateMachine stateMachines = SerializeConverter.FromTextAsset <TimelineStateMachine>(_file._editorAsset);
                        TimelineState[]      states        = stateMachines._states;

                        foreach (TimelineState state in states)
                        {
                            if (state._stateId == _stateId)
                            {
                                _editorStateName = _file._editorAsset.name + ":" + StringUtils.GetFirstLine(state.GetDescription());
                                break;
                            }
                        }
                    }
                }
                break;

                case eType.Internal:
                {
                    if (GetTimelineState() != null)
                    {
                        _editorStateName = StringUtils.GetFirstLine(_timelineState.GetDescription());
                    }
                }
                break;
                }
            }
Esempio n. 6
0
            private static IEnumerator DoStateThenGoTo(StateMachine stateMachine, TimelineStateRef TimelineState, TimelineStateRef goToState)
            {
                yield return(stateMachine.StartCoroutine(TimelineStateMachine.Run(stateMachine, TimelineState)));

                stateMachine.GoToState(TimelineStateMachine.Run(stateMachine, goToState));

                yield break;
            }
            public bool IsConditionMet(StateMachine stateMachine)
            {
                ITimelineStateMachineTimer timer = TimelineStateMachine.GetTimer(stateMachine.gameObject);

                _time -= timer.GetDeltaTime();

                return(_time <= 0.0f);
            }
Esempio n. 8
0
            public void FixUpRef(TimelineStateMachine stateMachine)
            {
                _stateMachine  = stateMachine;
                _timelineState = null;
#if UNITY_EDITOR
                _editorStateName = null;
#endif
            }
            public eEventTriggerReturn Update(StateMachine stateMachine, float eventTime)
            {
                if (_condition.IsConditionMet(stateMachine))
                {
                    stateMachine.GoToState(TimelineStateMachine.Run(stateMachine, _state));
                    return(eEventTriggerReturn.EventFinishedExitState);
                }

                return(eEventTriggerReturn.EventOngoing);
            }
            public TimelineState LoadTimelineState(GameObject sourceObject = null)
            {
                if (_file != null)
                {
                    TimelineStateMachine stateMachine = TimelineStateMachine.FromTextAsset(_file, sourceObject);
                    return(stateMachine.GetTimelineState(_timelineId));
                }

                return(null);
            }
            public static TimelineStateMachine FromTextAsset(TextAsset asset, GameObject sourceObject = null)
            {
                TimelineStateMachine timelineStateMachine = SerializeConverter.FromTextAsset <TimelineStateMachine>(asset);

                if (sourceObject != null)
                {
                    GameObjectRef.FixupGameObjectRefs(sourceObject, timelineStateMachine);
                }

                return(timelineStateMachine);
            }
Esempio n. 12
0
            private bool DrawStateNamePopUps()
            {
                TimelineState[] states = null;

                switch (_editorLinkType)
                {
                case eType.External:
                {
                    TimelineStateMachine stateMachines = SerializeConverter.FromFile <TimelineStateMachine>(AssetDatabase.GetAssetPath(_file._editorAsset));
                    states = stateMachines._states;
                }
                break;

                case eType.Internal:
                {
                    if (_stateMachine != null)
                    {
                        states = _stateMachine._states;
                    }
                }
                break;
                }

                if (states != null && states.Length > 0)
                {
                    string[] stateNames = new string[states.Length + 1];
                    int      index      = 0;
                    stateNames[0] = "<none>";

                    for (int i = 0; i < states.Length; i++)
                    {
                        stateNames[i + 1] = "State" + states[i]._stateId + " (" + StringUtils.GetFirstLine(states[i].GetDescription()) + ")";

                        if (states[i]._stateId == _stateId)
                        {
                            index = i + 1;
                        }
                    }

                    EditorGUI.BeginChangeCheck();

                    index = EditorGUILayout.Popup("Timeline", index, stateNames);

                    if (EditorGUI.EndChangeCheck())
                    {
                        _stateId         = index == 0 ? -1 : (int)states[index - 1]._stateId;
                        _timelineState   = null;
                        _editorStateName = null;
                        return(true);
                    }
                }

                return(false);
            }
                public void New()
                {
                    _currentFileName       = null;
                    _editorPrefs._fileName = null;
                    _editorPrefs._stateId  = -1;
                    SaveEditorPrefs();

                    TimelineStateMachine stateMachine = new TimelineStateMachine();

                    SetStateMachine(stateMachine);
                    _stateEditor.SetTimeline(null);
                    SwitchToStatemachineView();

                    GetEditorWindow().DoRepaint();
                }
Esempio n. 14
0
            public eEventTriggerReturn Trigger(StateMachine stateMachine)
            {
                switch (_stateType)
                {
                case eStateType.Coroutine:
                {
                    stateMachine.GoToState(_coroutine.RunCoroutine());
                }
                break;

                case eStateType.Timeline:
                {
                    stateMachine.GoToState(TimelineStateMachine.Run(stateMachine, _state));
                }
                break;
                }

                return(eEventTriggerReturn.EventFinishedExitState);
            }
                private void SetStateMachine(TimelineStateMachine stateMachine)
                {
                    ClearObjects();
#if DEBUG
                    _playModeHighlightedState = null;
#endif

                    for (int i = 0; i < stateMachine._states.Length; i++)
                    {
                        AddNewObject(stateMachine._states[i]);
                    }

                    for (int i = 0; i < stateMachine._editorNotes.Length; i++)
                    {
                        AddNewObject(stateMachine._editorNotes[i]);
                    }

                    CenterCamera();
                }
            public eEventTriggerReturn Trigger(StateMachine stateMachine)
            {
                eEventTriggerReturn ret = eEventTriggerReturn.EventFinished;

                if (_condition != null)
                {
                    _condition.OnStartConditionChecking(stateMachine);

                    if (_condition.IsConditionMet(stateMachine))
                    {
                        stateMachine.GoToState(TimelineStateMachine.Run(stateMachine, _state));
                        ret = eEventTriggerReturn.EventFinishedExitState;
                    }
                    if (_duration > 0.0f)
                    {
                        ret = eEventTriggerReturn.EventOngoing;
                    }
                }

                return(ret);
            }
                private TimelineStateMachine ConvertToTimelineStateMachine()
                {
                    List <TimelineState>            states = new List <TimelineState>();
                    List <TimelineStateMachineNote> notes  = new List <TimelineStateMachineNote>();

                    foreach (TimelineStateEditorGUI editorGUI in _editableObjects)
                    {
                        if (editorGUI.IsNote)
                        {
                            notes.Add((TimelineStateMachineNote)editorGUI.GetEditableObject());
                        }
                        else if (!editorGUI.IsExternal)
                        {
                            states.Add(editorGUI.GetEditableObject());
                        }
                    }

                    TimelineStateMachine stateMachine = new TimelineStateMachine();

                    stateMachine._states      = states.ToArray();
                    stateMachine._editorNotes = notes.ToArray();
                    return(stateMachine);
                }
                private void LoadFile(string fileName)
                {
                    _currentFileName = fileName;

                    TimelineStateMachine stateMachine = SerializeConverter.FromFile <TimelineStateMachine>(fileName);

                    if (stateMachine != null)
                    {
                        if (_editorPrefs._fileName != fileName)
                        {
                            _editorPrefs._fileName = fileName;
                            _editorPrefs._stateId  = -1;
                            SaveEditorPrefs();
                        }

                        SetStateMachine(stateMachine);
                        _stateEditor.SetTimeline(null);

                        if (_editorPrefs._stateId != -1)
                        {
                            SwitchToStateView(_editorPrefs._stateId);
                        }
                        else
                        {
                            SwitchToStatemachineView();
                        }
                    }
                    else
                    {
                        _editorPrefs._fileName = null;
                        _editorPrefs._stateId  = -1;
                        SaveEditorPrefs();
                    }

                    GetEditorWindow().DoRepaint();
                }
            void Start()
            {
                StateMachine stateMachine = GetComponent <StateMachine>();

                stateMachine.GoToState(TimelineStateMachine.Run(stateMachine, _initialState));
            }
            private static IEnumerator PerformState(StateMachine stateMachine, TimelineState state, Timeline timeline)
            {
                if (timeline != null && timeline._events.Length > 0)
                {
                    ITimelineStateMachineTimer timer = TimelineStateMachine.GetTimer(stateMachine.gameObject);

                    float        currentTime      = 0.0f;
                    List <Event> nonInstantEvents = new List <Event>();

                    int   eventIndex   = 0;
                    Event currentEvent = timeline._events[eventIndex];

                    while (currentEvent != null || nonInstantEvents.Count > 0)
                    {
                        IStateMachineEvent currentStateMachineEvent = currentEvent as IStateMachineEvent;

                        if (currentStateMachineEvent == null && currentEvent != null)
                        {
                            throw new System.Exception("Event doesn't implement IStateMachineEvent");
                        }

                        float nextEventTime = currentEvent != null ? timeline._events[eventIndex].GetTime() : 0.0f;

                        //Wait until event time
                        while (currentTime < nextEventTime || (currentEvent == null && nonInstantEvents.Count > 0))
                        {
                            currentTime += timer.GetDeltaTime();
#if DEBUG
                            TimelineStateMachineDebug.OnTimelineStateTimeProgress(stateMachine, state, currentTime);
#endif

                            //Updated non instant events, if any now wants to exit the state then break out of coroutine
                            if (UpdateNonInstantEvents(stateMachine, ref nonInstantEvents, currentTime))
                            {
                                EndNonInstantEvents(stateMachine, ref nonInstantEvents);
                                yield break;
                            }

                            yield return(null);
                        }

                        if (currentEvent == null)
                        {
                            break;
                        }

                        //Trigger event
                        eEventTriggerReturn status = currentStateMachineEvent.Trigger(stateMachine);

                        switch (status)
                        {
                        case eEventTriggerReturn.EventFinished:
                            //Do nothing, just move on to next event
                            break;

                        case eEventTriggerReturn.EventFinishedExitState:
                            //Exit state so break out of coroutine
                            EndNonInstantEvents(stateMachine, ref nonInstantEvents);
                            yield break;

                        case eEventTriggerReturn.EventOngoing:
                            //Track timed event, move on to next event
                            nonInstantEvents.Add(currentEvent);
                            break;
                        }

                        //Get next
                        currentEvent = ++eventIndex < timeline._events.Length ? timeline._events[eventIndex] : null;
                    }
                }

#if DEBUG
                TimelineStateMachineDebug.OnTimelineStateStoped(stateMachine);
#endif

                yield break;
            }
                public void OnAddedNewXmlNode(object obj)
                {
                    TimelineStateMachine stateMachine = ConvertToTimelineStateMachine();

                    TimelineStateMachine.FixupStateRefs(stateMachine, obj);
                }
Esempio n. 22
0
 public IEnumerator GetGoToState(StateMachine stateMachine)
 {
     return(TimelineStateMachine.Run(stateMachine, _goToState));
 }
Esempio n. 23
0
                public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    Rect foldoutPosition = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);

                    _foldout = EditorGUI.Foldout(foldoutPosition, _foldout, property.displayName);
                    _height  = EditorGUIUtility.singleLineHeight;

                    if (_foldout)
                    {
                        int origIndent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel++;

                        Rect filePosition = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight, position.width, EditorGUIUtility.singleLineHeight);

                        SerializedProperty fileProp = property.FindPropertyRelative("_file");
                        fileProp.objectReferenceValue = EditorGUI.ObjectField(filePosition, "File", fileProp.objectReferenceValue, typeof(TextAsset), true);

                        _height += EditorGUIUtility.singleLineHeight;

                        if (fileProp.objectReferenceValue != null)
                        {
                            SerializedProperty TimelineProp = property.FindPropertyRelative("_timelineId");

                            //Load all time lines from a file
                            //Get gameobject from  property
                            TimelineStateMachine stateMachine = TimelineStateMachine.FromTextAsset((TextAsset)fileProp.objectReferenceValue, FindParentGameObject(property));

                            if (stateMachine != null)
                            {
                                TimelineState[] states = stateMachine._states;

                                if (states != null && states.Length > 0)
                                {
                                    string[] stateNames = new string[states.Length];
                                    int      index      = 0;
                                    for (int i = 0; i < states.Length; i++)
                                    {
                                        stateNames[i] = "State" + states[i]._stateId + " (" + states[i]._editorDescription + ")";

                                        if (states[i]._stateId == TimelineProp.intValue)
                                        {
                                            index = i;
                                        }
                                    }

                                    Rect TimelinePosition = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight * 2, position.width, EditorGUIUtility.singleLineHeight);

                                    index = EditorGUI.Popup(TimelinePosition, "Timeline", index, stateNames);
                                    TimelineProp.intValue = states[index]._stateId;

                                    _height += EditorGUIUtility.singleLineHeight;
                                }

                                Rect editButtonPosition = new Rect(position.x + EditorGUIUtility.labelWidth, position.y + EditorGUIUtility.singleLineHeight * 3, position.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight);

                                if (GUI.Button(editButtonPosition, "Edit"))
                                {
                                    TimelineStateMachineEditorWindow.Load(fileProp.objectReferenceValue as TextAsset);
                                }

                                _height += EditorGUIUtility.singleLineHeight;
                            }
                        }

                        EditorGUI.indentLevel = origIndent;
                    }

                    EditorGUI.EndProperty();
                }
Esempio n. 24
0
 public void SetEditorStateMachine(TimelineStateMachine stateMachine)
 {
     _editorStateMachine = stateMachine;
 }