using System; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; public class MatControlMixerBehaviour : PlayableBehaviour { Color m_DefaultColor; Color m_DefaultEmissionColor; Renderer m_TrackBinding; bool init = false; float inputWeight = 0; public override void ProcessFrame(Playable playable, FrameData info, object playerData) { m_TrackBinding = playerData as Renderer; if (m_TrackBinding == null) return; if (!init) { init = true; if (Application.isPlaying) { // 复制材质球 m_DefaultColor = m_TrackBinding.material.color; m_DefaultEmissionColor = GetEmissionColor(m_TrackBinding); } } int inputCount = playable.GetInputCount(); for (int i = 0; i < inputCount; i++) { inputWeight = playable.GetInputWeight(i); if (inputWeight == 0) continue; // float blend = (float)playable.GetTime(); // Debug.Log("cur weight = " + inputWeight); ScriptPlayable inputPlayable = (ScriptPlayable)playable.GetInput(i); MatControlBehaviour input = inputPlayable.GetBehaviour(); // Debug.Log(input.Clip.start + " " + input.Clip.end); if (input.isContinued) { if (Application.isPlaying) { m_TrackBinding.sharedMaterial.color = input.color; m_DefaultColor = input.color; } continue; } Playable PlayableDir = playable.GetGraph().GetRootPlayable(0); float playtime = (float)PlayableDir.GetTime(); float colorWeight = Mathf.InverseLerp((float)input.Clip.start, (float)input.Clip.end, playtime); // Debug.Log("colorWeight = " + colorWeight); if (Application.isPlaying) { // PlayableDir.Pause(); m_TrackBinding.sharedMaterial.color = Color.Lerp(m_DefaultColor, input.color, colorWeight); if (input.isEmission) { Color c = Color.Lerp(m_DefaultEmissionColor, input.Emission, colorWeight); SetEmissionColor(m_TrackBinding, c); } } } } public Color GetEmissionColor(Renderer r) { if (r.sharedMaterial.IsKeywordEnabled("_EMISSION")) { return r.sharedMaterial.GetColor("_EmissionColor"); } return Color.clear; } public void SetEmissionColor(Renderer r, Color c) { if (r.sharedMaterial.IsKeywordEnabled("_EMISSION")) { r.sharedMaterial.SetColor("_EmissionColor", c); } } public override void OnPlayableDestroy(Playable playable) { ResetColor(); base.OnPlayableDestroy(playable); } public override void OnGraphStop(Playable playable) { ResetColor(); base.OnGraphStop(playable); } public override void OnGraphStart(Playable playable) { ResetColor(); base.OnGraphStart(playable); } public override void OnBehaviourPlay(Playable playable, FrameData info) { ResetColor(); base.OnBehaviourPlay(playable, info); } public override void OnBehaviourPause(Playable playable, FrameData info) { ResetColor(); base.OnBehaviourPause(playable, info); } public void ResetColor() { if (m_TrackBinding != null) { if (Application.isPlaying) { m_TrackBinding.sharedMaterial.color = m_DefaultColor; if (m_TrackBinding.sharedMaterial.IsKeywordEnabled("_EMISSION")) { m_TrackBinding.sharedMaterial.SetColor("_EmissionColor", m_DefaultEmissionColor); } } } } }