Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
做坦克实例化不出来
你的小可爱发起了问答2019-08-11
7
回复
394
浏览

删除那段实例化坦克地图的代码unity可以按开始然后运行,有实例化的代码按开始键实例化不出来,unity就死机了,重启几次都有没用

所有回复
  • 老师_Trigger 2019-08-12

    同学你好,可能是存在死循环,会造成Unity卡死,检查一下我们的代码。

    还有-3条回复,点击查看
    你还没有登录,请先登录注册
  • 你的小可爱 2019-08-13

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class MapCreation : MonoBehaviour { public GameObject[] item; private List itemPositionList = new List(); private void Awake() { CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity); CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity); CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity); for (int i = -1; i < 2; i++) { CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity); } for (int i = -11; i < 12; i++) { CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity); } for(int i = -11; i< 12; i++) { CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity); } for(int i = -8; i < 9; i++) { CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity); } for(int i = -8; i < 9; i++) { CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity); } for(int i = 0; i < 60; i++) { CreateItem(item[1], CreateRandomPosition(), Quaternion.identity); } for(int i = 0; i < 20; i++) { CreateItem(item[2], CreateRandomPosition(), Quaternion.identity); } for(int i = 0; i < 20; i++) { CreateItem(item[4], CreateRandomPosition(), Quaternion.identity); } for (int i = 0; i < 20; i++) { CreateItem(item[5], CreateRandomPosition(), Quaternion.identity); } } private void CreateItem(GameObject createGameObject, Vector3 createPosition, Quaternion createRotation) { GameObject itemGo = Instantiate(createGameObject, createPosition, createRotation); itemGo.transform.SetParent(gameObject.transform); itemPositionList.Add(createPosition); } private Vector3 CreateRandomPosition() { while (true) { Vector3 createPosition = new Vector3(Random.Range(-9, -10), Random.Range(-7, 8), 0); if (!HasThePosition(createPosition)) { return createPosition; } } } private bool HasThePosition(Vector3 createPos) { for (int i = 0; i < itemPositionList.Count; i++) { if (createPos == itemPositionList[i]) { return true; } } return false; } }

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 你的小可爱 2019-08-13

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class MapCreation : MonoBehaviour { public GameObject[] item; private List itemPositionList = new List(); private void Awake() { CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity); CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity); CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity); for (int i = -1; i < 2; i++) { CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity); } for (int i = -11; i < 12; i++) { CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity); } for(int i = -11; i< 12; i++) { CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity); } for(int i = -8; i < 9; i++) { CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity); } for(int i = -8; i < 9; i++) { CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity); } for(int i = 0; i < 60; i++) { CreateItem(item[1], CreateRandomPosition(), Quaternion.identity); } for(int i = 0; i < 20; i++) { CreateItem(item[2], CreateRandomPosition(), Quaternion.identity); } for(int i = 0; i < 20; i++) { CreateItem(item[4], CreateRandomPosition(), Quaternion.identity); } for (int i = 0; i < 20; i++) { CreateItem(item[5], CreateRandomPosition(), Quaternion.identity); } } private void CreateItem(GameObject createGameObject, Vector3 createPosition, Quaternion createRotation) { GameObject itemGo = Instantiate(createGameObject, createPosition, createRotation); itemGo.transform.SetParent(gameObject.transform); itemPositionList.Add(createPosition); } private Vector3 CreateRandomPosition() { while (true) { Vector3 createPosition = new Vector3(Random.Range(-9, -10), Random.Range(-7, 8), 0); if (!HasThePosition(createPosition)) { return createPosition; } } } private bool HasThePosition(Vector3 createPos) { for (int i = 0; i < itemPositionList.Count; i++) { if (createPos == itemPositionList[i]) { return true; } } return false; } }

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 你的小可爱 2019-08-13

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 老师_Trigger 2019-08-13

    是下边我们自己添加的代码问题,添加的60,20,20个游戏物体可能把位置用完了所以它卡死了。可以把这些删除,如果想加可以少加一点,也可以把我们代码优化一下,如果目前不会做可以先搁置往后学习其他案例。

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!