Beispiel #1
0
        /// <summary>
        /// 添加一个状态
        /// </summary>
        /// <param name="stateID"></param>
        /// <param name="stateResHandle"></param>
        public bool AddState(APStateBase state)
        {
            int findIdx = -1;

            for (int i = 0; i < m_states.Count; i++)
            {
                APStateBase s = m_states[i];
                if (s.m_IsDisposed)
                {
                    findIdx = i;
                    continue;
                }

                // 状态重复
                if (s.StateID == state.StateID)
                {
                    return(false);
                }
            }

            if (findIdx == -1)
            {
                m_states.Add(state);
            }
            else
            {
                m_states[findIdx] = state;
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// 过渡到状态
        /// </summary>
        /// <param name="stateID">状态ID</param>
        /// <param name="startTime">新状态开始时间</param>
        /// <param name="transtionTime">过渡时间</param>
        /// <param name="transtionCurve">过渡曲线</param>
        public void TranstionState(EAPStateID stateID, float startTime, float transtionTime, AnimationCurve transtionCurve = null)
        {
            APStateBase state = GetState(stateID);

            int freeIdx = FindFreeMixerInputIndex();

            // 过渡时间为0 则直接将权重设置为1
            if (transtionTime <= Mathf.Epsilon)
            {
                for (int i = 0; i < m_inputStates.Count; i++)
                {
                    m_inputStates[i].m_Weight = i == freeIdx ? 1 : 0;
                }
                m_transitionSpeed = 0;
            }
            else
            {
                m_inputStates[freeIdx].m_Weight = 0;
                m_transitionSpeed = 1 / transtionTime;
            }

            m_inputStates[freeIdx].m_StateID = stateID;
            m_crtPlayingInputIdx             = freeIdx;

            Playable statePlayable = state.Output;

            state.SetTime(startTime);

            // 状态已经连接上
            state.m_IsFree = false;

            StateMixer.ConnectInput(freeIdx, statePlayable, 0);
            StateMixer.SetInputWeight(freeIdx, 0);
        }
Beispiel #3
0
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="deltatime"></param>
        public void UpdateTranstion(float deltatime)
        {
            float sumWeight = 0;

            for (int i = 0; i < m_inputStates.Count; i++)
            {
                Playable input = StateMixer.GetInput(i);
                if (false == input.IsValid())
                {
                    continue;
                }

                float inputWeight = m_inputStates[i].m_Weight;
                inputWeight += m_transitionSpeed * deltatime * (m_crtPlayingInputIdx == i ? 1 : -1);
                inputWeight  = Mathf.Clamp01(inputWeight);

                sumWeight += inputWeight;
            }

            // 归一化权重
            for (int i = 0; i < m_inputStates.Count; i++)
            {
                Playable input = StateMixer.GetInput(i);
                if (false == input.IsValid())
                {
                    continue;
                }

                StateWeight stateWeight = m_inputStates[i];
                stateWeight.m_Weight /= sumWeight;
                float weight = stateWeight.m_Weight;
                StateMixer.SetInputWeight(i, weight);

                // 权重为0 断开连接
                if (weight <= Mathf.Epsilon)
                {
                    StateMixer.DisconnectInput(i);

                    APStateBase inputState = GetState(stateWeight.m_StateID);
                    if (inputState == null)
                    {
                        Debug.LogError($"UpdateTranstion: 断开连接时 状态[{stateWeight.m_StateID}]不存在");
                    }
                    else
                    {
                        inputState.m_IsFree           = true;
                        inputState.m_LstEnterFreeTime = StateMixer.GetTime();
                    }
                }
            }
        }