简单使用对象池技术实现弹幕功能
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!