using UnityEngine; using System.Linq; using System.Collections; using System.Collections.Generic; public class TweenRenderer : TweenRoot { #region 变量 public override bool InOrigin { get { Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; if (material.GetColor(PropertyName).Equal(Origin)) { InOrigin_ = true; } else { InOrigin_ = false; } return InOrigin_; } set { InOrigin_ = value; InPause = false; InForward = false; InBackward = false; if (InOrigin_) { Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; material.SetColor(PropertyName, Origin); if (Group) { for (int i = 0; i < ChildList.Count; i++) { if (!ChildList[i].transform.IsChildOf(Target.transform)) { ChildList.RemoveAt(i--); continue; } Color selfColor = ColorDic[ChildList[i]]; material = UseSharedMaterial ? ChildList[i].sharedMaterial : ChildList[i].material; if (AlphaOnly) { material.SetColor(PropertyName, new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * material.GetColor(PropertyName).a)); } else { material.SetColor(PropertyName, selfColor * material.GetColor(PropertyName)); } } } BackwardFinish(); } } } public override bool InDestination { get { Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; if (material.GetColor(PropertyName).Equal(Destination)) { InDestination_ = true; } else { InDestination_ = false; } return InDestination_; } set { InDestination_ = value; InPause = false; InForward = false; InBackward = false; if (InDestination_) { Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; material.SetColor(PropertyName, Destination); if (Group) { for (int i = 0; i < ChildList.Count; i++) { if (!ChildList[i].transform.IsChildOf(Target.transform)) { ChildList.RemoveAt(i--); continue; } Color selfColor = ColorDic[ChildList[i]]; material = UseSharedMaterial ? ChildList[i].sharedMaterial : ChildList[i].material; if (AlphaOnly) { material.SetColor(PropertyName, new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * material.GetColor(PropertyName).a)); } else { material.SetColor(PropertyName, selfColor * material.GetColor(PropertyName)); } } } ForwardFinish(); } } } public bool Group; public bool AlphaOnly; public bool UseSharedMaterial; public string PropertyName = "_Color"; public Color Delta; public Color Origin; public Color Destination; public Renderer Target; public CurveFunctionC Func; public List ChildList = new List(); public Dictionary ColorDic = new Dictionary(); #endregion public TweenRenderer(Renderer target, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false) : base(cg, curve, target) { Func = AnimManager.CurveFuncDicC[curve]; Target = target; InForward = false; InBackward = false; Delta = destination - origin; Origin = origin; Group = group; Duration = duration; DestActive = destActive; Destination = destination; OriginActive = originActive; } public override bool StartForward() { if (base.StartForward()) { return true; } if (Group) { Renderer[] renderers = Target.GetComponentsInChildren(); ChildList = new List(); for (int i = 1; i < renderers.Length; i++) { Renderer renderer = renderers[i]; ChildList.Add(renderer); if (!ColorDic.ContainsKey(renderer)) { ColorDic.Add(renderer, renderer.material.GetColor(PropertyName)); } } List keyList = ColorDic.Keys.ToList(); for (int i = 0; i < keyList.Count; i++) { if (!ChildList.Contains(keyList[i])) { ColorDic.Remove(keyList[i]); } } } if (InBackward) { InBackward = false; Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; Timer = AnimManager.GetTimerColor(material.GetColor(PropertyName), Duration, Origin, Delta, Curve); } return false; } public override bool StartBackward() { if (base.StartBackward()) { return true; } if (Group) { Renderer[] renderers = Target.GetComponentsInChildren(); ChildList = new List(); for (int i = 1; i < renderers.Length; i++) { Renderer renderer = renderers[i]; ChildList.Add(renderer); if (!ColorDic.ContainsKey(renderer)) { ColorDic.Add(renderer, renderer.material.GetColor(PropertyName)); } } List keyList = ColorDic.Keys.ToList(); for (int i = 0; i < keyList.Count; i++) { if (!ChildList.Contains(keyList[i])) { ColorDic.Remove(keyList[i]); } } } if (InForward) { InForward = false; Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; Timer = AnimManager.GetTimerColor(material.GetColor(PropertyName), Duration, Destination, new Color(-Delta.r, -Delta.g, -Delta.b, -Delta.a), Curve); } return false; } public override bool DoForward() { Timer += Time.deltaTime; if (Timer > Duration) { InDestination = true; if (OnForwardFinish != null) { OnForwardFinish.Invoke(); } SetLoop(EventType.ForwardFinish); return true; } else { Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; material.SetColor(PropertyName, Func(Timer, Duration, Origin, Delta)); if (Group) { for (int i = 0; i < ChildList.Count; i++) { if (!ChildList[i].transform.IsChildOf(Target.transform)) { ChildList.RemoveAt(i--); continue; } Color selfColor = ColorDic[ChildList[i]]; material = UseSharedMaterial ? ChildList[i].sharedMaterial : ChildList[i].material; if (AlphaOnly) { material.SetColor(PropertyName, new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * material.GetColor(PropertyName).a)); } else { material.SetColor(PropertyName, selfColor * material.GetColor(PropertyName)); } } } return false; } } public override bool DoBackward() { Timer += Time.deltaTime; if (Timer > Duration) { InOrigin = true; if (OnBackwardFinish != null) { OnBackwardFinish.Invoke(); } SetLoop(EventType.BackwardFinish); return true; } else { Material material = UseSharedMaterial ? Target.sharedMaterial : Target.material; material.SetColor(PropertyName, Func(Timer, Duration, Destination, new Color(-Delta.r, -Delta.g, -Delta.b, -Delta.a))); if (Group) { for (int i = 0; i < ChildList.Count; i++) { if (!ChildList[i].transform.IsChildOf(Target.transform)) { ChildList.RemoveAt(i--); continue; } Color selfColor = ColorDic[ChildList[i]]; material = UseSharedMaterial ? ChildList[i].sharedMaterial : ChildList[i].material; if (AlphaOnly) { material.SetColor(PropertyName, new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * material.GetColor(PropertyName).a)); } else { material.SetColor(PropertyName, selfColor * material.GetColor(PropertyName)); } } } return false; } } }