Unity:在创建时停用GameObject

时间:2018-08-02 14:56:41

标签: c# unity3d gameobject disable

我在启动应用程序时正在运行时创建一个GameObject(带有纹理的矩形),并希望以后显示它。但是,如果我立即将其设置为非活动状态(myGO.setActive(false),甚至在仍在创建它时(GameObject o = new GameObject()之后),它会显示一秒钟,然后又消失。

我知道我可以在第一次真正需要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;
}

1 个答案:

答案 0 :(得分:1)

也许也禁用IN,然后重新启用它?

MeshRenderer