0

我在启动我的应用程序时在运行时创建了一个 GameObject(带纹理的矩形),并希望稍后显示它。但是,如果我立即将其设置为非活动状态myGO.setActive(false)GameObject o = new 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;
}
4

1 回答 1

1

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

myGO.GetComponent<MeshRenderer>().enabled = false;
于 2018-08-02T14:59:39.387 回答