让NGUI按钮间隔一段时间后通过alpha透明显示,这两个类都是我同事写的。不长,也比较容易理解。只要给父节点添加上代码,然后每次父节点显示的时候触发一次OnEnable事件,就可以看到子UI会一个个alpha透明出现,非常好用。
using UnityEngine;
using System.Collections;
using System.Linq;
public class DelayAniShow : MonoBehaviour
{
public float delay = 1.0f;
public float duration = 1.0f;
void OnEnable()
{
UISprite[] sprite = GetComponentsInChildren<UISprite>();
sprite = sprite.OrderBy(s => s.spriteName).ToArray();
foreach(var item in sprite)
item.alpha = 0.0f;
int index = 0;
foreach(UISprite sp in sprite)
{
TweenAlpha tween = TweenAlpha.Begin(sp.gameObject, duration, 1.0f);
tween.from = 0.0f;
tween.delay = delay * index++;
}
}
}
using UnityEngine;
using System.Collections;
public class MainPanelEnter : MonoBehaviour
{
// panel 下的子物体项
Transform[] childs;
// 两个 Item 出现的间隔
public float interval = 0.5f;
// 每个Item 从全部透明到完全不透明的变化的所需时间
public float alphaAniDuration = 0.5f;
void Start()
{
if(childs == null)
{
// 获取 panel 的子物体个数, 并且把遍历一次把他的子物体的 Transform 保存在 数组中
childs = new Transform[transform.childCount];
for(int i = 0, length = transform.childCount; i < length; i++)
{
childs[i] = transform.GetChild(i);
}
}
}
// OnEnable 函数, 当每次被唤醒的时候,都会执行一次
void OnEnable()
{
if(childs == null)
Start();
//foreach(var item in childs)
//{
// item.collider.enabled = true;
//}
StartCoroutine(OnBeginEnter());
}
void Update()
{
}
IEnumerator OnBeginEnter()
{
foreach(Transform t in childs)
{
t.gameObject.SetActive(false);
}
foreach(Transform t in childs)
{
yield return new WaitForSeconds(interval);
t.gameObject.SetActive(true);
TweenAlpha tween = TweenAlpha.Begin(t.gameObject, alphaAniDuration, 1.0f);
tween.from = 0.0f;
tween.eventReceiver = gameObject;
tween.callWhenFinished = "OnOnBeginEnterAniFinishod";
// yield return new WaitForEndOfFrame();
t.GetComponentInChildren<Animation>().Rewind();
}
}
void OnBeginOut()
{
foreach(var item in childs)
{
item.collider.enabled = false;
TweenAlpha.Begin(item.gameObject, alphaAniDuration, 0.0f);
}
}
void OnOnBeginEnterAniFinishod(UITweener tween)
{
foreach(var item in childs)
{
if(item.IsChildOf(tween.transform))
item.collider.enabled = true;
}
}
}