溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity游戲廣播走馬燈可循環(huán)可插入

發(fā)布時間:2020-02-28 02:02:58 來源:網絡 閱讀:573 作者:GuangYao_Li 欄目:游戲開發(fā)

應朋友要求幫忙寫的一個游戲廣播走馬燈程序,可以循環(huán)播放單條消息的次數也可以插入消息優(yōu)先播放,廢話不多說直接上代碼,(這里只是基本的功能實現,具體封裝自己去封裝了,我就不封裝了,也是對你們的鍛煉,哈哈)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

using System;

enum CirculationType
{
    Single,
    Circulation,
    Insertion,
}

class Item
{
    public Item(string pText, int pCount)
    {
        m_pText = pText;
        Count = pCount;
    }
    public string m_pText;
    public int Count;
}

public class NewBehaviourScript : MonoBehaviour
{

    /// <summary>
    /// 公告跑馬燈
    /// </summary>
    public Text NoticeText;
    public Vector3 Speed = new Vector3(2, 0, 0);
    public float _MoveEndPoint;
    public Vector2 _MoveStartPoint;
    public float _ParentWidth;
    public float _SelfWidth;
    public Button button;

    private int spe = 3;
    string pname;

    List<string> pName;

    bool m_bCirculation;
    bool m_bInsertion;
    bool m_bRun=true;
    int m_nRun = 0;
    Queue<Item> GetVs = new Queue<Item>();

    /// <summary>
    /// 土豪數據 先進后出
    /// </summary>
    Stack<Item> GetStack = new Stack<Item>();

    public event Action m_pUpdateQueueText;

    public event Action m_pUpdateStackText;

    private void Start()
    {
        GetVs.Enqueue(new Item("元素1222222222222222222222222222222222222222222222222222222222222", 0));
        GetVs.Enqueue(new Item("元素233333333333333333333333333333333333333333333333333333333333", 2));//2  3
        GetVs.Enqueue(new Item("元素344444444444444444444444444444444444444444444444444444444444", 3));//3  4
        GetVs.Enqueue(new Item("元素35555555555555555555555555555555555555555555555555555555555", 0));

        GetQueueText();
        RegisterBroadcast();
        m_pUpdateQueueText += GetQueueText;

        m_pUpdateStackText += GetStackText;

        button.onClick.AddListener(delegate ()
        {
            GetStack.Push(new Item("土豪充值:百萬!?。。。。。。?這個是超級土豪?。。。?! 大家歡迎?。。?!", 3));
        });
    }

    /// <summary>
    /// 注冊 公告,便于刷新
    /// </summary>
    /// <param name="text"></param>
    void RegisterBroadcast()
    {
        _ParentWidth = NoticeText.transform.parent.GetComponent<RectTransform>().rect.width;
        _SelfWidth = NoticeText.preferredWidth;
        NoticeText.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 0.5f);

        _MoveEndPoint = -_ParentWidth / 2 - _SelfWidth;

        _MoveStartPoint = new Vector2(_ParentWidth / 2, 0);
    }

    private void FixedUpdate()
    {
        if (GetStack.Count == 0 && GetVs.Count == 0)
        {
            if (m_bRun)
            {
                m_nRun++;
                m_bRun = false;
                Debug.LogError("ddd");
            }
        }
        else
        {

            m_bRun = true;
        }

        if (m_nRun >=2) return;
        m_bInsertion = GetStack.Count != 0 ? true : false;

        // 公告移動
        if (NoticeText.gameObject.activeInHierarchy)
        {
            NoticeText.transform.localPosition -= Speed;
            if (NoticeText.transform.localPosition.x <= _MoveEndPoint)
            {
                NoticeText.transform.localPosition = _MoveStartPoint;

                if (m_nRun == 1) m_nRun++;
                m_bCirculation = spe > 0 ? true : false;
                if (m_bCirculation)
                    spe--;

                if (m_bInsertion)
                {
                    if (m_pUpdateStackText != null)
                        m_pUpdateStackText();
                }
                else
                {
                    if (m_pUpdateQueueText != null && GetVs.Count != 0 && !m_bCirculation)
                    {
                        m_pUpdateQueueText();
                    }
                }
            }
        }
    }

    /// <summary>
    /// 獲取下一條
    /// </summary>
    /// <returns></returns>
    public void GetQueueText()
    {
        Item item = GetVs.Dequeue();
        NoticeText.text = item.m_pText;
        spe = item.Count - 1;

    }

    public void GetStackText()
    {
        Item item = GetStack.Pop();
        NoticeText.text = item.m_pText;
        spe = item.Count - 1;
    }
}

我這里是使用 棧隊和事件完成的,有什么好的建議可以留言,(其實這個代碼還可以優(yōu)化,只是沒時間去改了,有興趣的同學可以自己拿去優(yōu)化試試~)。

下面是執(zhí)行效果 :
順序播放
Unity游戲廣播走馬燈可循環(huán)可插入

插入
Unity游戲廣播走馬燈可循環(huán)可插入

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI