/// <summary> /// Gather the current details of the <see cref="AnimancerNode.Root"/> and register this /// <see cref="CustomFade"/> to be updated by it so that it can replace the regular fade behaviour. /// </summary> protected void Apply(AnimancerState state) { #if UNITY_ASSERTIONS Debug.Assert(state != null, "State is null."); Debug.Assert(state.IsValid, "State is not valid."); Debug.Assert(state.IsPlaying, "State is not playing."); Debug.Assert(state.LayerIndex >= 0, "State is not connected to a layer."); Debug.Assert(state.TargetWeight > 0, "State is not fading in."); var animancer = state.Root; Debug.Assert(animancer != null, $"{nameof(state)}.{nameof(state.Root)} is null."); #if UNITY_EDITOR if (WarningType.CustomFadeBounds.IsEnabled()) { if (CalculateWeight(0) != 0) { WarningType.CustomFadeBounds.Log("CalculateWeight(0) != 0.", animancer.Component); } if (CalculateWeight(1) != 1) { WarningType.CustomFadeBounds.Log("CalculateWeight(1) != 1.", animancer.Component); } } #endif #endif _FadeSpeed = state.FadeSpeed; if (_FadeSpeed == 0) { return; } _Time = 0; _TargetState = new StateWeight(state); _TargetLayer = state.Layer; _CommandCount = _TargetLayer.CommandCount; OtherStates.Clear(); for (int i = _TargetLayer.ChildCount - 1; i >= 0; i--) { var other = _TargetLayer.GetChild(i); other.FadeSpeed = 0; if (other != state && other.Weight != 0) { OtherStates.Add(new StateWeight(other)); } } state.Root.RequireUpdate(this); }
/// <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(); } } } }