我正在尝试创建一个迷宫,但在 运行 游戏时我的 cpu 使用或内存使用过载,我该如何解决?
I'm trying to create a maze but my cpu usage or memory usage get overload when running the game how can i fix it?
我向教主添加了两个立方体。
第一个我附上了脚本。
我将第二个立方体拖到 Inspector 到 Wall Holder。
当 运行 游戏时,我的整个电脑都卡住了,我不得不关闭电脑并重新启动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour {
public GameObject wallHolder;
public float wallLength = 1.0f;
public int xSize = 5;
public int ySize = 5;
private Vector3 initialPos;
// Use this for initialization
void Start ()
{
CreateWalls();
}
// Update is called once per frame
void Update () {
}
void CreateWalls()
{
wallHolder = new GameObject();
wallHolder.name = "Maze";
initialPos = new Vector3((-xSize / 2) + wallLength / 2, (-ySize / 2) + wallLength / 2);
Vector3 myPos = initialPos;
GameObject tempWall;
//For x axis
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j <= xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength) - wallLength / 2, 0.0f, initialPos.z + (i * wallLength) - wallLength / 2);
tempWall = Instantiate(wallHolder, myPos, Quaternion.identity) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
//for y axis
for (int i = 0; i <= ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength), 0.0f, initialPos.z + (i * wallLength) - wallLength);
tempWall = Instantiate(wallHolder, myPos, Quaternion.Euler(0.0f, 90.0f, 0.0f)) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
}
}
您分配和引用 wallHolder 的方式有些奇怪 属性。密切注意你是如何处理它的。像这样分解它是个好主意:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator: MonoBehaviour {
public GameObject wallObjectRoot;
public GameObject wallObjectReference;
public float wallLength = 1.0f;
public int xSize = 5;
public int ySize = 5;
private Vector3 initialPos;
// Use this for initialization
void Start ()
{
if (wallObjectReference == null || wallObjectRoot == null){
Debug.LogError("WallHolder properties need to be assigned to MazeGenerator");
}
CreateWalls();
}
// Update is called once per frame
void Update () {
}
void CreateWalls()
{
initialPos = new Vector3((-xSize / 2) + wallLength / 2, (-ySize / 2) + wallLength / 2);
//For x axis
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j <= xSize; j++)
{
Vector3 spawnPos = new Vector3(initialPos.x + (j * wallLength) - wallLength / 2, 0.0f, initialPos.z + (i * wallLength) - wallLength / 2);
GameObject wallObject = Instantiate(wallObjectReference, spawnPos, Quaternion.identity);
wallObject.transform.parent = wallObjectRoot.transform;
}
}
//for y axis
for (int i = 0; i <= ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
Vector3 spawnPos = new Vector3(initialPos.x + (j * wallLength), 0.0f, initialPos.z + (i * wallLength) - wallLength);
GameObject wallObject = Instantiate(wallObjectReference, spawnPos, Quaternion.Euler(0.0f, 90.0f, 0.0f));
wallObject.transform.parent = wallObjectRoot.transform;
}
}
}
}
将 MazeGenerator 脚本放在名为 MazeGenerator 的对象上。使用 Inspector,将场景中的一个 GameObject 指定为 wallObjectRoot,并将项目中的一个 Prefab 指定为 wallObjectReference。您也可以使用场景中的游戏对象作为 wallObjectReference,但这有点不靠谱。
此外,如果 unity 进程冻结,您应该可以通过从 windows 任务管理器 (Ctrl+Shift+Esc) 取消它来退出它。
我向教主添加了两个立方体。 第一个我附上了脚本。 我将第二个立方体拖到 Inspector 到 Wall Holder。
当 运行 游戏时,我的整个电脑都卡住了,我不得不关闭电脑并重新启动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator : MonoBehaviour {
public GameObject wallHolder;
public float wallLength = 1.0f;
public int xSize = 5;
public int ySize = 5;
private Vector3 initialPos;
// Use this for initialization
void Start ()
{
CreateWalls();
}
// Update is called once per frame
void Update () {
}
void CreateWalls()
{
wallHolder = new GameObject();
wallHolder.name = "Maze";
initialPos = new Vector3((-xSize / 2) + wallLength / 2, (-ySize / 2) + wallLength / 2);
Vector3 myPos = initialPos;
GameObject tempWall;
//For x axis
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j <= xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength) - wallLength / 2, 0.0f, initialPos.z + (i * wallLength) - wallLength / 2);
tempWall = Instantiate(wallHolder, myPos, Quaternion.identity) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
//for y axis
for (int i = 0; i <= ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
myPos = new Vector3(initialPos.x + (j * wallLength), 0.0f, initialPos.z + (i * wallLength) - wallLength);
tempWall = Instantiate(wallHolder, myPos, Quaternion.Euler(0.0f, 90.0f, 0.0f)) as GameObject;
tempWall.transform.parent = wallHolder.transform;
}
}
}
}
您分配和引用 wallHolder 的方式有些奇怪 属性。密切注意你是如何处理它的。像这样分解它是个好主意:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MazeGenerator: MonoBehaviour {
public GameObject wallObjectRoot;
public GameObject wallObjectReference;
public float wallLength = 1.0f;
public int xSize = 5;
public int ySize = 5;
private Vector3 initialPos;
// Use this for initialization
void Start ()
{
if (wallObjectReference == null || wallObjectRoot == null){
Debug.LogError("WallHolder properties need to be assigned to MazeGenerator");
}
CreateWalls();
}
// Update is called once per frame
void Update () {
}
void CreateWalls()
{
initialPos = new Vector3((-xSize / 2) + wallLength / 2, (-ySize / 2) + wallLength / 2);
//For x axis
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j <= xSize; j++)
{
Vector3 spawnPos = new Vector3(initialPos.x + (j * wallLength) - wallLength / 2, 0.0f, initialPos.z + (i * wallLength) - wallLength / 2);
GameObject wallObject = Instantiate(wallObjectReference, spawnPos, Quaternion.identity);
wallObject.transform.parent = wallObjectRoot.transform;
}
}
//for y axis
for (int i = 0; i <= ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
Vector3 spawnPos = new Vector3(initialPos.x + (j * wallLength), 0.0f, initialPos.z + (i * wallLength) - wallLength);
GameObject wallObject = Instantiate(wallObjectReference, spawnPos, Quaternion.Euler(0.0f, 90.0f, 0.0f));
wallObject.transform.parent = wallObjectRoot.transform;
}
}
}
}
将 MazeGenerator 脚本放在名为 MazeGenerator 的对象上。使用 Inspector,将场景中的一个 GameObject 指定为 wallObjectRoot,并将项目中的一个 Prefab 指定为 wallObjectReference。您也可以使用场景中的游戏对象作为 wallObjectReference,但这有点不靠谱。
此外,如果 unity 进程冻结,您应该可以通过从 windows 任务管理器 (Ctrl+Shift+Esc) 取消它来退出它。