简体   繁体   中英

Unity: Deactivate GameObject on creation

I'm creating a GameObject (rectangle with a texture) at runtime while starting my app and want to display it later. But if I set it inactive right away ( myGO.setActive(false) ) or even while it's still creating it (directly after GameObject o = new GameObject() ), it shows up for a second, then disappears again.

I'm aware that I could create the GameObject the first time it's actually needed but I want to do all the setup like that while displaying a loading screen.

Is it possible to set a GameObject inactive instantly when it's ready but before it's actually displayed?

Code (shortened version):

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM