我早些时候实例化并销毁了我的游戏对象,但在得知它是处理器密集型的之后,我操纵了我的代码,使其成为一个包含 1 个对象的对象池。当我按下 Z 时,只要按下 Z,我的对象就会被激活并开始放大,并且在向上键时,它会停用对象,但是当再次按下 Z 时,对象不会再次被调用。我究竟做错了什么 ?
float speed = 2f;
public GameObject radius;
public int pooledAmount = 1;
List<GameObject> radiusorb;
void Start(){
radiusorb = new List<GameObject> ();
for (int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate (radius);
obj.SetActive(false);
radiusorb.Add (obj);
}
}
void Update (){
if(Input.GetKeyDown(KeyCode.Z))
{
Invoke("LetRadiusExist",0.001f); //Instantiating the prefab
}
if (Input.GetKey(KeyCode.Z)) { //Scale the object
if (gameObject != null)
{
gameObject.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);
}
}
if(Input.GetKeyUp(KeyCode.Z)) //To deactivate the prefab
{
gameObject.SetActive(false);
//Destroy(cloneRadius, 0);
}
}
void LetRadiusExist()
{
for (int i = 0; i < radiusorb.Count; i++)
{
if (!radiusorb[i].activeInHierarchy)
{
radiusorb[i].SetActive(true);
radiusorb[i].transform.parent = transform;
radiusorb[i].transform.localPosition = new Vector3(0,0,0);
break;
}
}
}