【发布时间】:2017-03-11 00:27:49
【问题描述】:
我有一个特定大小的程序生成板。为了看到它,我通常必须点击运行,这很烦人并且很难想象。
是否可以通过 InitializeOnLoad 制作并可视化电路板?
我试了一下:
Setup.cs
[InitializeOnLoad]
public class Setup : MonoBehaviour {
public static Map initialMap;
static Setup(){
initialMap = new Map ();
initialMap.createMap ();
}
}
Map.cs
public class Map {
private Tile[,] tiles= new Tile[5,5];
//I had Resources.Load here, but apparently thats not allowed either...
public GameObject defaultObj= new GameObject("MyCreatedGO");
public Map (){
Debug.Log("In Constructor");
}
public void createMap(){
for (int x = 0; x < tiles.GetLength(0); x += 1) {
for (int y = 0; y < tiles.GetLength(1); y += 1) {
// Tile Instantiates the defaultObj
tiles [x, y] = new Tile (defaultObj, new Vector3 (x, y, 0));
}
}
}
}
但团结的回应是抱怨
UnityException: 不允许调用 Internal_CreateGameObject 从 MonoBehaviour 构造函数(或实例字段初始值设定项),调用 它改为唤醒或开始。从 MonoBehaviour 'Setup' 调用 游戏对象“地图”。
当 InitializeOnLoad-ed 静态构造函数发生时,引擎是否处于可以生成对象的状态?
如果这不可能,您应该如何在 Unity 中可视化程序生成的地图?
【问题讨论】:
标签: c# unity3d gameobject