拿空间换时间的方案,存 取 自我清理便用
所有重复使用的东西都用对象池。
简易的对象池使用:
public List<GameObject> list;
public GameObject Bullet;
Vector3 BirthPostion;
// Start is called before the first frame update
void Start()
{
list = new List<GameObject>();
BirthPostion = Camera.main.transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (list.Count < 15)//数组中物体少于15
{
GameObject go = GameObject.Instantiate(Bullet, BirthPostion, Quaternion.Euler(0, 180, 0)) as GameObject;
go.GetComponent<Rigidbody>().velocity = go.transform.forward * 100;
}
else
{
list[list.Count - 1].transform.position = BirthPostion;
list[list.Count - 1].SetActive(true);
list.RemoveAt(list.Count - 1);
}
}
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "abc")
{
other.gameObject.SetActive(false);
Camera.main.GetComponent<buttkelong>().list.Add(other.gameObject);
}