Unity制作弹幕

思路:
弹幕:ugui制作。
1.获取屏幕高度,然后看字号的大小进行均分n份。将text做成prefab用到的时候实例出来即可,如果弹幕太多可以使用资源池。
2.建一个数组长度为n,目的是:来一条弹幕,随机产生一个数来确定弹幕的高度,将产生的数在数组中标记或移除,(可以参看之前的不产生重复的随机数)。
3.当数组的元素用完,重置一轮即可,字右飞左即可.
4.长的速度快点,多的话可以使用栈

缺省默认的参数方法:
add(1);//如果不输入a值则a为10。

 private static void add(int b,int a=10)
        {
            Console.WriteLine(a+b);
        }

经常实例删除的object,可以放在list中方便处理

弹幕代码:by: kuilaurence

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

public class BarrageClone : MonoBehaviour
{
    public GameObject textGo;
    public Transform canvas;
    List<int> numList = new List<int>();
    private List<string> msg = new List<string>
    {
        "DOTS Mode does not use classic Unity Scenes. It has its own Scene format which is specialized for",
        "Entity Scenes vs. classic Scenes",
        "When inspecting an Entity in DOTS Mode, the Inspector window displays the DOTS Mode components attached to that Entity, in a very similar way to viewing the components attached to a GameObject. There are many built-in DOTS Mode components which are similar - but not the same as - classic Unity components.",
        "The Inspector window",
        "For example, DOTS Mode has its own Sprite2DRenderer component, which is similar to the classic Unity SpriteRenderer component, ",
        "The Play button",
        "The play button in DOTS Mode. Pressing this builds your projects and runs the resulting build.",
        "Another important difference is that you cannot add more than one component of the same type to an Entity.",
        "More configuration options are available under Edit > Project Settings, then select DOTS.",
        "D by: kuilaurence",
    };

    float scrrenWeight = 0;//宽
    float screenHeight = 0;//高
    float initX = 0;       //初始的x
    int part = 20;         //屏幕竖直方向分块
    float fixheight = 0;   //每块的宽度
    int rdheight = 0;      //随机高度
    int rdText = 0;        //随机文字段   //测试用
    int perCharWeight = 18;//每个字母宽度 //自定义
    private void Start()
    {
        screenHeight = Screen.height;
        scrrenWeight = Screen.width;
        fixheight = screenHeight / part;
        initX = scrrenWeight / 2;
        InitNumList();
    }
    public void SendMsg()
    {
        GameObject go = GameObjectPool.Instance.GetObject("barrage");
        if (go!=null)
        {
            go.SetActive(true);
        }else
        {
            go = Instantiate(textGo, canvas);
        }
        rdheight = GetRDNum();
        rdText = Random.Range(0, 9);
        go.GetComponent<RectTransform>().localPosition = new Vector3(initX, -screenHeight/2+fixheight * rdheight, 0);
        go.transform.GetComponent<Text>().text = msg[rdText];
        go.transform.GetComponent<Text>().color=GetColor();
        go.GetComponent<RectTransform>().sizeDelta = new Vector2(msg[rdText].Length * perCharWeight, 50);
        go.transform.DOMoveX(initX - scrrenWeight - msg[rdText].Length * perCharWeight, 20);
        StartCoroutine(StoreObject("barrage", go));
    }

    private Color GetColor()
    {
        int rd = Random.Range(0, 100);
        if (rd > 70)
        {
            return Color.red;
        }else if (rd > 40)
        {
            return Color.green;
        }
        else
        {
            return Color.white;
        }
    }
    /// <summary>
    /// 取不同的值的随机数,随机的是下标取的是对应的数值 by: kuilaurence
    /// </summary>
    /// <returns>一个随机数</returns>
    private int GetRDNum()
    {
        if (numList.Count == 0) InitNumList();
        int index = Random.Range(0, numList.Count - 1);
        int n = numList[index];
        numList.RemoveAt(index);
        return n;
    }
    private void InitNumList()
    {
        for (int i = 0; i < part; i++)
        {
            numList.Add (i);
        }
    }
    IEnumerator StoreObject(string name,GameObject go)
    {
        yield return new WaitForSeconds(20);
        GameObjectPool.Instance.StoreObject(go, name);
    }
}

资源池代码:by: kuilaurence

using UnityEngine;
using System.Collections.Generic;

public class GameObjectPool : MonoBehaviour
{
    public static GameObjectPool Instance;
    private void Awake()
    {
        if (Instance != null)
        {
            DestroyImmediate(gameObject);
        }
        else
        {
            Instance = this;
        }
        DontDestroyOnLoad(gameObject);
    }
    //弹幕barrage
    Stack<GameObject> barrageList = new Stack<GameObject>();    //为什么使用栈,因为栈方便处理直接pop返回值,其他的还需要remove
    private void Start()
    {
        barrageList.Clear();
    }
    /// <summary>
    /// 取资源 by: kuilaurence
    /// </summary>
    /// <returns></returns>
    public GameObject GetObject(string  name="barrage")
    {
        switch (name)
        {
            case "barrage":
                {
                    if (barrageList.Count > 0)
                        return barrageList.Pop();
                    else
                        return null;
                    break;
                }
            default:
                {
                    return null;
                    break;
                }
        }
    }
    /// <summary>
    /// 存资源 by: kuilaurence
    /// </summary>
    /// <param name="go"></param>
    /// <param name="name"></param>
    public void StoreObject(GameObject go,string name="barrage")
    {
        switch (name)
        {
            case "barrage":
                {
                    barrageList.Push(go);
                    go.SetActive(false);
                    break;
                }
            default:
                break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值