123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
-
- using UnityEngine;
- using System.Collections.Generic;
- using System.Linq;
- using System.Collections;
- using System;
- using UnityEngine.Events;
- /// <summary>
- /// Play the specified tween on click.
- /// </summary>
- [ExecuteInEditMode, DefaultExecutionOrder(-100)]
- public sealed class PlayTween : MonoBehaviour
- {
- /// <summary>
- /// Target on which there is one or more tween.
- /// </summary>
- public GameObject tweenTarget;
- /// <summary>
- /// If there are multiple tweens, you can choose which ones get activated by changing their group.
- /// </summary>
- public int tweenGroup = 0;
- /// <summary>
- /// How long will the tweener wait before starting the tween?
- /// </summary>
- public float delay = 0f;
- /// <summary>
- /// Direction to tween in.
- /// </summary>
- public Direction playDirection = Direction.Forward;
- /// <summary>
- /// Whether the tween will be reset to the start or end when activated. If not, it will continue from where it currently is.
- /// </summary>
- public bool resetOnPlay = false;
- /// <summary>
- /// Whether the tween will be reset to the start if it's disabled when activated.
- /// </summary>
- public bool resetIfDisabled = false;
- /// <summary>
- /// What to do if the tweenTarget game object is currently disabled.
- /// </summary>
- public EnableCondition ifDisabledOnPlay = EnableCondition.DoNothing;
- /// <summary>
- /// What to do with the tweenTarget after the tween finishes.
- /// </summary>
- public DisableCondition disableWhenFinished = DisableCondition.DoNotDisable;
- /// <summary>
- /// Whether the tweens on the child game objects will be considered.
- /// </summary>
- public bool includeChildren = false;
- [SerializeField]
- UnityEvent m_OnBeginning = new UnityEvent();
- [SerializeField]
- UnityEvent m_OnFinished = new UnityEvent();
- /// <summary>
- /// 开始前执行
- /// </summary>
- public event Action onBeginning;
- /// <summary>
- /// 完毕后执行
- /// </summary>
- public event Action onFinished;
- Tweener[] mTweens;
- int mActive = 0;
- /// <summary>
- /// 正向
- /// </summary>
- public void PlayForward()
- {
- Play(true);
- }
- /// <summary>
- /// 反向
- /// </summary>
- public void PlayReverse()
- {
- Play(false);
- }
- void Awake()
- {
- onBeginning += m_OnBeginning.Invoke;
- onFinished += m_OnFinished.Invoke;
- if (tweenTarget == null)
- tweenTarget = gameObject;
- }
- /// <summary>
- /// Activate the tweeners.
- /// </summary>
- public void Play(bool forward)
- {
- if (onBeginning != null)
- onBeginning();
- mActive = 0;
- GameObject go = (tweenTarget == null) ? gameObject : tweenTarget;
- if (!go.activeSelf)
- {
- // If the object is disabled, don't do anything
- if (ifDisabledOnPlay != EnableCondition.EnableThenPlay)
- return;
- // Enable the game object before tweening it
- go.SetActive(true);
- }
- // Gather the tweening components
- mTweens = includeChildren ? go.GetComponentsInChildren<Tweener>() : go.GetComponents<Tweener>();
- if (mTweens.Length == 0)
- {
- // No tweeners found -- should we disable the object?
- if (disableWhenFinished != DisableCondition.DoNotDisable)
- tweenTarget.SetActive(true);
- }
- else
- {
- bool activated = false;
- if (playDirection == Direction.Reverse)
- forward = !forward;
- // Run through all located tween components
- Tweener maxDurTW = null;
- for (int i = 0, imax = mTweens.Length; i < imax; ++i)
- {
- Tweener tw = mTweens[i];
- tw.onFinished = null;
- // If the tweener's group matches, we can work with it
- if (tw.tweenGroup == tweenGroup)
- {
- // Ensure that the game objects are enabled
- if (!activated && !go.activeSelf)
- {
- activated = true;
- go.SetActive(true);
- }
- ++mActive;
- // Toggle or activate the tween component
- if (playDirection == Direction.Toggle)
- {
- tw.Toggle();
- }
- else
- {
- if (resetOnPlay || (resetIfDisabled && !tw.enabled))
- tw.ResetToBeginning();
- tw.Play(forward);
- }
- if (maxDurTW == null)
- {
- maxDurTW = tw;
- }
- else
- {
- if (maxDurTW.delay + maxDurTW.duration < tw.delay + tw.duration)
- maxDurTW = tw;
- }
- }
- }
- maxDurTW.onFinished = () =>
- {
- if (onFinished != null)
- onFinished();
- if (disableWhenFinished != DisableCondition.DoNotDisable && (int)maxDurTW.direction == (int)disableWhenFinished)
- {
- tweenTarget.SetActive(false);
- }
- else
- {
- tweenTarget.SetActive(true);
- }
- };
- }
- }
- }
|