首页 > 解决方案 > 脚本创建的 GameObject(Prefab 对象)在 Unity3d 中不出现在 Game View 中但出现在 Scene View 中

问题描述

我通过将其附加到球体对象的脚本创建了一个 pin 对象。

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()
    {

    }
}

此脚本附加到球体对象。我的球体对象具有地球图像(地球)的着色器材质。球体表面上的这个实例化预制件(gameObjectPin)出现在场景上但不在游戏屏幕上,当我在相机预览中选择相机对象时,这个对象也不会出现。

场景视图

场景视图

选择相机时的场景视图

选择相机时的场景视图

我是 Unity 的新手,我应该检查或更正什么以使我创建的对象出现在球体上基本上我正在尝试将引脚添加到相应的国家并标记它。类似于这个http://kitsdmcc.com/news上的地球

在球体对象上单击 Play 时创建游戏对象

在球体对象上单击 Play 时创建游戏对象

在播放模式下选择 Pin Object 时

在播放模式下选择 Pin Object 时

标签: unity3dunity-components

解决方案


哦,现在我明白了!你所做的只是通过这个菜单设置它的GIZMO

在此处输入图像描述

仅在 SceneView 中显示。

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

词汇表

与场景中的 a相关联的图形叠加层GameObject ,并显示在场景视图中。移动工具等内置场景工具,您可以使用纹理或脚本Gizmos 创建自定义。Gizmos有些Gizmos仅在GameObject选择 时绘制,而另Gizmos一些则由编辑器绘制,无论GameObject选择哪个 s。


如评论中所述,您根本没有组件,GameObject因此在 Gameview 中没有呈现任何内容。

当然,现在您也可以通过Gizmos切换为 GameView 启用 Gizmos

在此处输入图像描述

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


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

您可能必须将 Pin TextureTextureType更改为Sprite (2D and UI).

在此处输入图像描述


一般来说,我还建议创建一个 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
}

推荐阅读