运行效果:点击跑马灯1,实现图3的效果.
实现步骤:
简单搭建如上图所示场景,注意:1 noticeTip_Panel上绑定Mask.cs 2 txt_Msg文本的Horizontal Overflo设置成Overflow(如图2所示)。
接下来就是代码的编写了。首先说一下思路:1 、设置text的起始位置在如图2的1位置处,text的末尾在2处。使用DoTween动画进行移动。
2 、我们知道队列的特点是先进先出,所以用到了队列和协程,只有当前消息播放完毕之后才会播放下条消息。
3 、使用了单例的泛型类。
具体步骤:
1、创建一个单例的泛型类,之后用到单例的时候都继承该脚本。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MB_Singleton<T> : MonoBehaviour where T : Component
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType(typeof(T)) as T;
GameObject obj = new GameObject();
obj.hideFlags = HideFlags.HideAndDontSave;
_instance = obj.AddComponent(typeof(T)) as T;
}
return _instance;
}
}
public virtual void Awake()
{
DontDestroyOnLoad(this.gameObject);
if (_instance == null)
{
_instance = this as T;
}
else
{
Destroy(gameObject);
}
}
}
2.创建一个NoticeController脚本,绑定在noticeTip_Panel上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class NoticeController : MB_Singleton<NoticeController>
{
[SerializeField]
Text m_TxtMsg;//跑马灯text.
Queue<string> m_MsgQueue;//灯队列.
//Font m_Font;
bool isScrolling = false;//判断当前text中的跑马灯是否跑完.
private void Start()
{
Init();
}
public void Init()
{
m_MsgQueue = new Queue<string>();
}
/// <summary>
/// 添加跑马灯信息.
/// </summary>
/// <param name="msg"></param>
public void AddMessage(string msg)
{
if (!gameObject.activeSelf)
{
gameObject.SetActive(true);
Init();
}
m_MsgQueue.Enqueue(msg);
if (isScrolling) return;
StartCoroutine(Scrolling());
}
public IEnumerator Scrolling()
{
float beginX = 450;
float leftX = -400;
while (m_MsgQueue.Count > 0)
{
float duration = 10f;
float speed = 200f;
int loop = 3;
string msg = m_MsgQueue.Dequeue();
m_TxtMsg.text = msg;
float txtWidth = m_TxtMsg.preferredWidth;//文本自身的长度.
Vector3 pos = m_TxtMsg.rectTransform.localPosition;
float distance = beginX - leftX + txtWidth;
duration = distance / speed;
isScrolling = true;
while (loop-- > 0)
{
Debug.Log(loop);
m_TxtMsg.rectTransform.localPosition = new Vector3(beginX, pos.y, pos.z);
m_TxtMsg.rectTransform.DOLocalMoveX(-distance, duration).SetEase(Ease.Linear);
yield return new WaitForSeconds(duration);
}
yield return null;
}
isScrolling = false;
gameObject.SetActive(false);
yield break;
}
}
3 在别处调用,创建一个空物体,挂上下面这个脚本。press(),press2()分别为图1,中两个button按钮的点击事件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MB_Singleton<GameManager>
{
public GameObject noticeTip_Panel;
/// <summary>
/// 先对noticeTip_Panel进行初始化操作.
/// </summary>
public void PreInit()
{
if (!noticeTip_Panel.activeSelf)
{
noticeTip_Panel.SetActive(true);
NoticeController.Instance.Init();
}
}
public void press()
{
PreInit();
//noticeTip_Panel.GetComponent<NoticeController>().AddMessage("年三年实打实的 阿萨德高压闪光灯哈桑的话是对哈撒啊是否打算");
NoticeController.Instance.AddMessage("年三年实打实的 阿萨德高压闪光灯哈桑的话是对哈撒啊是否打算");
}
public void press2()
{
PreInit();
NoticeController.Instance.AddMessage("1111111111111111");
}
}