大概实现效果如图! 下面是实现的脚本!!!!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 提示条
/// </summary>
public class UIInfoTips : MonoBehaviour
{
public static int BgHeight = 24;
List<GameObject> _Items = new List<GameObject>();
GameObject _Template = null;
GameObject _Parent = null;
void Awake()
{
_Template = ObjectCommon.GetChild(gameObject, "Anchor/item");
UITools.setActive(_Template, false);
_Parent = ObjectCommon.GetChild(gameObject, "Anchor");
UISprite sprite = ObjectCommon.GetChildComponent<UISprite>(gameObject, "Anchor/item/bg");
if (sprite != null)
{
BgHeight = sprite.height;
}
}
public void CreateTip(string str)
{
if (_Template != null && _Items != null && _Parent != null)
{
GameObject go = UITools.GetOneCached(_Items, _Parent, _Template);
if (go != null)
{
go.SetActive(true);
TipsChildItem childItem = go.GetComponent<TipsChildItem>();
if (childItem == null)
{
childItem = go.AddComponent<TipsChildItem>();
}
childItem.FadeOut(str);
_Items.Add(childItem.gameObject);
}
}
}
public void CreateItemInfoTip(List<string> str, Vector3 startPos)
{
StartCoroutine(OnCreateItemInfoTip(str, startPos));
}
IEnumerator OnCreateItemInfoTip(List<string> str, Vector3 startPos)
{
if (str == null || _Template == null || _Items == null || _Parent == null)
{
yield break;
}
for (int i = 0; i < str.Count; i++)
{
GameObject go = UITools.GetOneCached(_Items, _Parent, _Template);
if (go != null)
{
go.SetActive(true);
TipsChildItem childItem = go.GetComponent<TipsChildItem>();
if (childItem == null)
{
childItem = go.AddComponent<TipsChildItem>();
}
float offsetY = 50 + (str.Count - i) * 20f;
childItem.ItemInfoFadeOut(str[i], startPos, offsetY);
_Items.Add(childItem.gameObject);
yield return new WaitForSeconds(0.2f);
}
}
}
public class TipsChildItem : MonoBehaviour
{
UILabel _Label = null;
UITexture _Bg = null;
bool _Animationing = false;
UIPanel _Panel = null;
float _AlphaTime = 1f;
void Awake()
{
_Label = ObjectCommon.GetChildComponent<UILabel>(gameObject, "label");
_Bg = ObjectCommon.GetChildComponent<UITexture>(gameObject, "bg");
_Panel = gameObject.GetComponent<UIPanel>();
_Panel.depth = PanelDepth.Depth_5;
}
public void FadeOut(string str)
{
UITools.SetLabText(_Label, str);
if (_Bg != null)
{
_Bg.width = 138 + _Label.width;
if (!_Bg.gameObject.activeSelf)
{
_Bg.gameObject.SetActive(true);
}
}
UIAnimationParam param = new UIAnimationParam();
param._Target = gameObject;
param._Pos = new Vector3(transform.localPosition.x, transform.localPosition.y + 100f, transform.localPosition.z);
param._Time = _AlphaTime;
param._Del = "OnRest";
UIAnimationController.CloseSlideOut(param);
}
public void ItemInfoFadeOut(string str, Vector3 startPos, float offsetY)
{
UITools.SetLabText(_Label, str);
if (_Bg != null)
{
_Bg.gameObject.SetActive(false);
}
gameObject.transform.localPosition = startPos;
UIAnimationParam param = new UIAnimationParam();
param._Target = gameObject;
param._Pos = new Vector3(transform.localPosition.x, transform.localPosition.y + offsetY, transform.localPosition.z);
param._Time = _AlphaTime;
param._Del = "OnRest";
UIAnimationController.CloseSlideOut(param);
}
void Update()
{
if (_Animationing == true && _Panel != null)
{
_Panel.alpha -= Time.deltaTime;
}
}
void OnRest()
{
StartCoroutine(WaitShow());
}
IEnumerator WaitShow()
{
_Animationing = true;
yield return new WaitForSeconds(_AlphaTime);
transform.localPosition = Vector3.zero;
gameObject.SetActive(false);
_Animationing = false;
if (_Panel != null)
{
_Panel.alpha = 1f;
}
}
}
}
这里脚本里用到的方法!
public static GameObject GetOneCached(List<GameObject> cachedItems, GameObject parent, GameObject item)
{
if (cachedItems == null)
{
cachedItems = new List<GameObject>();
}
for (int i = 0; i < cachedItems.Count; i++)
{
if (cachedItems[i].activeSelf == false)
{
cachedItems[i].SetActive(true);
return cachedItems[i];
}
}
if (parent != null && item != null)
{
GameObject go = NGUITools.AddChild(parent, item);
go.SetActive(true);
cachedItems.Add(go);
return go;
}
return null;
}