脚本创建的GameObject(Prefab object)在Unity3d中不出现在Game View中而是出现在Scene View中

GameObject (Prefab object ) created by script does not appear in Game View but appears in Scene View in Unity3d

我通过脚本创建了一个图钉 object 并将其附加到球体 object 。

using UnityEngine;

public class InstantiateMarkerPin : MonoBehaviour
{
    public float Xpos;
    public float Ypos;
    public float Zpos;
    public GameObject gameObjectPinInstantiate;

    // Start is called before the first frame update
    private void Start()
    {
        Xpos = 0.09f;
        Ypos = 0.50f;
        Zpos = 1.1f;
        //The original object, where to instantiate, and the orientation of the new object
        GameObject marker = (GameObject)Resources.Load("gameObjectPin");
        Vector3 location = new Vector3(Xpos, Ypos, Zpos);
        Quaternion rotation = Quaternion.Euler(0, 0, 0);
        //The object the script is attached to
        GameObject world = this.gameObject;
        //Instantiate the prefab
        gameObjectPinInstantiate = Instantiate(marker, location, rotation, world.transform);

        Debug.Log("InstantiateMarkerPin class : Marker  Location 2 :X, Y, Z : " + gameObjectPinInstantiate.transform.position);
    }

    // Update is called once per frame
    private void Update()
    {

    }
}

此脚本已附加到球体 Object。我的球体 Object 具有地球图像(地球)的着色器 material。 这个球体表面上的实例化预制件(游戏ObjectPin)出现在场景中但不在游戏屏幕上,当我select相机预览中的相机object也是这个object没有出现。

场景视图

相机处于 selected

时的场景视图

我是 Unity 的新手,我应该检查或更正什么才能在球体上显示我创建的 object 基本上,我正在尝试将图钉添加到相应的国家/地区并对其进行标记。类似于此 http://kitsdmcc.com/news

上的地球

游戏object 在球体上单击“播放”时创建 object

当 Pin Object 在播放模式下 select 时

哦,我明白了!你所做的只是通过这个菜单

设置它的 GIZMO

只显示在SceneView中。

这与 GameView 中的渲染无关,只是一种更容易在 SceneView 中查看和查找特定类型对象的方法,因为如果不选择它们通常是不可见的。

来自Glossary:

A graphic overlay associated with a GameObject in a Scene , and displayed in the Scene View . Built-in scene tools such as the move tool are Gizmos , and you can create custom Gizmos using textures or scripting. Some Gizmos are only drawn when the GameObject is selected, while other Gizmos are drawn by the Editor regardless of which GameObjects are selected.


如评论中所述,您的 GameObject 上根本没有组件,因此游戏视图中没有呈现任何内容。

当然现在您也可以通过 Gizmos 开关

为 GameView 启用 Gizmos

但我想您更愿意尝试实现的是在最终应用程序中呈现该图标。


您可能想使用例如SpriteRenderer 组件在这里。只需将您的图标拖到 Sprite 属性.

您可能需要将 Pin Texture's TextureType 更改为 Sprite (2D and UI)


一般来说,我也会推荐Create a Prefab而不是使用这里的Resources文件夹。


一般情况下,我还会对您的代码进行一些更改:

public class InstantiateMarkerPin : MonoBehaviour
{
    [Header("Settings")]

    // directly use a Vector3 for setting the values
    //                              | default value for this field 
    //                              | (only valid until changed via Inspector!)
    //                              v
    public Vector3 TargetPosition = new Vector3(0.09f, 0.5f, 1.1f);
    // Instead of using Resources simply reference the 
    // created Prefab here
    public GameObject gameObjectPinPrefab;

    [Header("Outputs")]
    public GameObject gameObjectPinInstantiate;

    private void Start()
    {
        // Careful this currently ovewrites any value set via the
        // Inspector. Later you will probably want to remove this.
        TargetPosition = new Vector3(0.09f, 0.5f, 1.1f);

        //As said I would rather use a prefab here and simply reference it above in the field
        //gameObjectPinPrefab = (GameObject)Resources.Load("gameObjectPin");

        //Instantiate the prefab
        gameObjectPinInstantiate = Instantiate(gameObjectPinPrefab, TargetPosition, Quaternion.identity, transform);

        Debug.Log("InstantiateMarkerPin class : Marker  Location 2 :X, Y, Z : " + gameObjectPinInstantiate.transform.position);
    }

    // Always remove empty Unity methods
    // They are called as messages if they exist 
    // and only cause unnecessary overhead
}