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