Unity简单使用对象池技术实现弹幕功能

简单使用对象池技术实现弹幕功能


UI界面
在这里插入图片描述
效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

DanMuSystem.cs :

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

public class DanMuSystem : MonoBehaviour
{
    public GameObject sendMsgText;
    public Transform canvas;
    public Transform objectPoolContent;

    public Button sendButton;
    public Button deleteButton;
    public InputField inputField;

    List<GameObject> msgTextList = new List<GameObject>();

    //对象池
    public List<GameObject> objectPool = new List<GameObject>();
    //对象池个数
    public int objcetCount;

    float scrrenWeight = 0;//宽
    float screenHeight = 0;//高

    float fixheight = 0;   //每块的高度
    float fixwidth = 0;   //每块的宽度

    int wCount = 30;  //宽的块数
    int hCount = 20; //高的块数

    int rdText = 0; //文字个数
    // Start is called before the first frame update
    void Start()
    {
        screenHeight = Screen.height;
        scrrenWeight = Screen.width;
        fixheight = screenHeight / hCount;
        fixwidth = scrrenWeight / wCount;
        sendButton.onClick.AddListener(() =>
        {
            SendMsg();
        });

        deleteButton.onClick.AddListener(() =>
        {
            DeleteAllMsgList();
        });

        //创建对象池
        for(int i=0;i< objcetCount; i++)
        {
             GameObject go = GameObject.Instantiate(sendMsgText,objectPoolContent);
             objectPool.Add(go);
             go.SetActive(false);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    private void SendMsg()
    {
        if(inputField.text=="")
        {
            return;
        }
        GameObject go = GetObjectPool();
        objectPool.Remove(go);
        go.SetActive(true);
        go.transform.SetParent(canvas);
        msgTextList.Add(go);
        go.GetComponent<RectTransform>().localPosition = new Vector3(GetRDNum(wCount) * fixwidth - canvas.GetComponent<RectTransform>().localPosition.x, GetRDNum(hCount) * fixheight - canvas.GetComponent<RectTransform>().localPosition.y, 0);
        go.GetComponent<Text>().text = inputField.text;
        go.GetComponent<RectTransform>().sizeDelta= new Vector2(inputField.text.Length * go.GetComponent<Text>().fontSize, 50);
        go.GetComponent<Text>().color = GetColor();
        inputField.text = "";
    }

    private int GetRDNum(int count)
    {
        int index = Random.Range(10, count-1);//为了使整个弹幕界面初始位置看起来好一些这里进行了微调
        return index;
    }

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

    public void RemoveMsgTextList(GameObject go)
    {
        msgTextList.Remove(go);
        objectPool.Add(go);
        go.transform.SetParent(objectPoolContent);
        go.SetActive(false);
    }

    private void DeleteAllMsgList()
    {
        for(int i=0;i<msgTextList.Count; i++)
        {
            objectPool.Add(msgTextList[i]);
            msgTextList[i].transform.SetParent(objectPoolContent);
            msgTextList[i].SetActive(false);
        }
        msgTextList.RemoveAll(isGameobjct);
    }

    private bool isGameobjct(GameObject obj)
    {
        return true;
    }

    //获取对象池元素
    public GameObject GetObjectPool()
    {
        if(objectPool.Count==0)
        {
            GameObject go = GameObject.Instantiate(sendMsgText);
            objectPool.Add(go);
            return go;
        }
        else
        {
            return objectPool[0];
        }
        
    }
}

对话框移动脚本:LeftMove.cs

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

public class LeftMove : MonoBehaviour
{
    // Start is called before the first frame update
    public int speed=10;
    Vector3 position;
    void Start()
    {
        position = new Vector3(-561, transform.localPosition.y, transform.localPosition.z); //此处“-561”,要根据自己屏幕最左侧的位置x的坐标确定,这里比较粗糙
    }

    // Update is called once per frame
    void Update()
    {
        transform.localPosition= transform.position = Vector3.MoveTowards(gameObject.transform.localPosition, position, speed*Time.deltaTime);
        if (Vector3.Distance(transform.localPosition, position) < 0.01f)
        {
            transform.parent.GetChild(0).GetComponent<DanMuSystem>().RemoveMsgTextList(this.gameObject);
        }
    }
}

不会使用CSDN!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值