游戏里的文本tips

大概实现效果如图! 下面是实现的脚本!!!!


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;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值