Unity:在创建时停用 GameObject
Unity: Deactivate GameObject on creation
我在运行时创建一个游戏对象(带有纹理的矩形),同时启动我的应用程序并希望稍后显示它。但是,如果我立即将其设置为非活动状态 (myGO.setActive(false)
) 或什至在它仍在创建它时(紧接在 GameObject o = new GameObject()
之后),它会显示一秒钟,然后再次消失。
我知道我可以在第一次实际需要时创建 GameObject,但我想在显示加载屏幕时进行所有设置。
是否可以在准备好但实际显示之前立即将游戏对象设置为非活动状态?
代码(简化版):
private void StartUpMethod() {
GameObject myGO = MakePhoto(vertices);
if(myGO==null) {
ShowError("Error Message");
} else {
myGO.SetActive(false);
}
}
private GameObject MakePhoto(Vector3[] vertices) {
byte[] fileData = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(2,2);
tex.LoadImage(fileData);
GameObject o = new GameObject();
Mesh m = new Mesh();
Material mat = new Material(Shader.Find("Standard")) { mainTexture = tex };
Vector3[] vv = new Vector3[] { new Vector3(0,0,0),new Vector3(length,0,0),new Vector3(length,width,0),new Vector3(0,width,0) };
if(o.GetComponent<MeshFilter>() == null) { o.AddComponent<MeshFilter>(); }
if(o.GetComponent<MeshRenderer>() == null) { o.AddComponent<MeshRenderer>(); }
o.GetComponent<MeshFilter>().mesh = m;
o.GetComponent<MeshRenderer>().material = mat;
m.vertices = vv;
m.uv = new Vector2[] {new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0)};
m.triangles = new int[] { 0,1,2,0,2,3 };
m.RecalculateBounds();
m.RecalculateNormals();
o.transform.Rotate(new Vector3(270,0,0));
o.transform.Rotate(new Vector3(0,0,270));
o.transform.position = new Vector3(x,z,y);
return o;
}
也许还要禁用 MeshRenderer
,然后再重新启用它?
myGO.GetComponent<MeshRenderer>().enabled = false;
我在运行时创建一个游戏对象(带有纹理的矩形),同时启动我的应用程序并希望稍后显示它。但是,如果我立即将其设置为非活动状态 (myGO.setActive(false)
) 或什至在它仍在创建它时(紧接在 GameObject o = new GameObject()
之后),它会显示一秒钟,然后再次消失。
我知道我可以在第一次实际需要时创建 GameObject,但我想在显示加载屏幕时进行所有设置。
是否可以在准备好但实际显示之前立即将游戏对象设置为非活动状态?
代码(简化版):
private void StartUpMethod() {
GameObject myGO = MakePhoto(vertices);
if(myGO==null) {
ShowError("Error Message");
} else {
myGO.SetActive(false);
}
}
private GameObject MakePhoto(Vector3[] vertices) {
byte[] fileData = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(2,2);
tex.LoadImage(fileData);
GameObject o = new GameObject();
Mesh m = new Mesh();
Material mat = new Material(Shader.Find("Standard")) { mainTexture = tex };
Vector3[] vv = new Vector3[] { new Vector3(0,0,0),new Vector3(length,0,0),new Vector3(length,width,0),new Vector3(0,width,0) };
if(o.GetComponent<MeshFilter>() == null) { o.AddComponent<MeshFilter>(); }
if(o.GetComponent<MeshRenderer>() == null) { o.AddComponent<MeshRenderer>(); }
o.GetComponent<MeshFilter>().mesh = m;
o.GetComponent<MeshRenderer>().material = mat;
m.vertices = vv;
m.uv = new Vector2[] {new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0)};
m.triangles = new int[] { 0,1,2,0,2,3 };
m.RecalculateBounds();
m.RecalculateNormals();
o.transform.Rotate(new Vector3(270,0,0));
o.transform.Rotate(new Vector3(0,0,270));
o.transform.position = new Vector3(x,z,y);
return o;
}
也许还要禁用 MeshRenderer
,然后再重新启用它?
myGO.GetComponent<MeshRenderer>().enabled = false;