目前正在为学校开发一款游戏。我们打算通过使用碰撞触发器来实例化平台,因为我们的角色降落在平台上。我已经成功地触发了它们,登陆它们,然后触发实例化新的平台。我现在的问题是,在9个触发器之后,游戏会滞后,我觉得这种方法使用了太多的内存,这就是为什么在一段时间后游戏会滞后。
有没有更好的方法来解决这个问题?
编辑:
public Transform[] spawnLocations;
public GameObject[] platform1;
public GameObject[] platformclone;
public float movespeed;
public bool moveRight;
void OnTriggerEnter2D (Collider2D col)
{
platformclone[0] = Instantiate(platform1[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
platformclone[1] = Instantiate(platform1[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
}此脚本已附加到平台。然后,我们在初始平台下创建了两个空子对象,并预先定位了它们。当我们的角色落在平台上时,通过碰撞触发器,在下面创建两个平台(在层次中,这被标记为平台(克隆)。
带有触发器标记的长方体Collider2D附加到角色上。平台上有一个盒子collider2D。
当角色到达其中一个新平台时,将创建两个新平台,然后标签将变为平台(克隆)(克隆)。
整个过程不断重复,直到第9-10个场景,角色静止不动,因为他们是一个盒子对撞机,但看不到平台,游戏在这一点上变得迟缓。
发布于 2015-10-06 21:46:35
我认为你想要开发无限运行游戏。如果为真,则需要实例化visible+1 perefab对象,并在传递预置时将其转换为最后一个元素的位置。你需要改变,而不是破坏和重建你的平台(我认为这是预制的)。
编辑
我不检查语法错误下面的代码是用来解释逻辑的。
public Transform[] spawnLocations;
public GameObject[] platform1;
public GameObject[] platformclone;
public float movespeed;
public bool moveRight;
public List<GameObject> instantiatedPlatforms;
public int poolSize = 10;
void Start(){
//Pool Platforms
instantiatedPlatforms = new List<GameObject>();
instantiatedPlatforms.Add(Instantiate(platform1[0], Vector3.zero, Quaternion.Euler(0, 0, 0)) as GameObject); //you can set whatever position you want this is for initialization.
instantiatedPlatforms.Add(Instantiate(platform1[1], Vector3.zero, Quaternion.Euler(0, 0, 0)) as GameObject);
//If you have many platform1 objects. you can do it with loop
//for(int i=0;i<poolSize;i++){
// instantiatedPlatforms.Add(Instantiate(platform1[i], Vector3.zero, Quaternion.Euler(0, 0, 0)) as GameObject);
//}
//after that you can set first platform prefab
instantiatedPlatforms[0].transform.position = spawnLocations[0].transform.position;
instantiatedPlatforms[1].transform.position = spawnLocations[1].transform.position;
}
void OnTriggerEnter2D (Collider2D col)
{
//platformclone[0] = Instantiate(platform1[0], spawnLocations[0].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
//platformclone[1] = Instantiate(platform1[1], spawnLocations[1].transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
//Get usedPrefabs
GameObject obj1 = instantiatedPlatforms[0];
GameObject obj2 = instantiatedPlatforms[1];
//Change Value
obj1.transform.position = Vector3.zero;
obj2.transform.position = Vector3.zero;
//Change List Position (Remove First insert last)
instantiatedPlatforms.Remove(0); //remove first and second elements
instantiatedPlatforms.Remove(0); //after first remove second member becomes first so need 2 times remove.
instantiatedPlatforms.Add(obj1); //set last
instantiatedPlatforms.Add(obj2);
//set new prefabs to spawn positions
instantiatedPlatforms[0].transform.position = spawnLocations[0].transform.position;
instantiatedPlatforms[1].transform.position = spawnLocations[1].transform.position;
}https://stackoverflow.com/questions/32971547
复制相似问题