using UnityEngine; using System.Linq; using System.Collections; using System.Collections.Generic; public class TweenSr : TweenRoot { #region 变量 public override bool InOrigin { get { if (Target.color.Equal(Origin)) { InOrigin_ = true; } else { InOrigin_ = false; } return InOrigin_; } set { InOrigin_ = value; InPause = false; InForward = false; InBackward = false; if (InOrigin_) { Target.color = Origin; UpdateChildSpriteRenderer(); 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]]; if (AlphaOnly) { ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * Target.color.a); } else { ChildList[i].color = selfColor * Target.color; } } } BackwardFinish(); } } } public override bool InDestination { get { if (Target.color.Equal(Destination)) { InDestination_ = true; } else { InDestination_ = false; } return InDestination_; } set { InDestination_ = value; InPause = false; InForward = false; InBackward = false; if (InDestination_) { Target.color = Destination; UpdateChildSpriteRenderer(); 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]]; if (AlphaOnly) { ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * Target.color.a); } else { ChildList[i].color = selfColor*Target.color; } } } ForwardFinish(); } } } public bool Group; public bool AlphaOnly; public Color Delta; public Color Origin; public Color Destination; public SpriteRenderer Target; public CurveFunctionC Func; public List ChildList = new List(); public Dictionary ColorDic = new Dictionary(); #endregion public TweenSr(SpriteRenderer 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; } UpdateChildSpriteRenderer(); if (InBackward) { InBackward = false; Timer = AnimManager.GetTimerColor(Target.color, Duration, Origin, Delta, Curve); } return false; } public override bool StartBackward() { if (base.StartBackward()) { return true; } UpdateChildSpriteRenderer(); if (InForward) { InForward = false; Timer = AnimManager.GetTimerColor(Target.color, 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 { Target.color = 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]]; if (AlphaOnly) { ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * Target.color.a); } else { ChildList[i].color = selfColor * Target.color; } } } 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 { Target.color = 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]]; if (AlphaOnly) { ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a*Target.color.a); } else { ChildList[i].color = selfColor * Target.color; } } } return false; } } public void UpdateChildSpriteRenderer() { if (Group) { SpriteRenderer[] renderers = Target.GetComponentsInChildren(); ChildList = new List(); for (int i = 1; i < renderers.Length; i++) { SpriteRenderer renderer = renderers[i]; ChildList.Add(renderer); if (!ColorDic.ContainsKey(renderer)) { ColorDic.Add(renderer, renderer.color); } } List keyList = ColorDic.Keys.ToList(); for (int i = 0; i < keyList.Count; i++) { if (!ChildList.Contains(keyList[i])) { ColorDic.Remove(keyList[i]); } } } } }