脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中

我通过将销钉对象附加到球体对象上的脚本创建了该销钉对象。

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

    }
}

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

场景视图

脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中

选择摄像机时的场景视图

脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中

我是Unity新手,应该检查或纠正将我创建的对象显示在球体上的内容 基本上,我正在尝试将销钉添加到相应的国家/地区并标记它。类似于此http://kitsdmcc.com/news

在球体对象上单击“播放”时,将创建游戏对象

脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中

在播放模式下选择“固定对象”时

脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中

tongxin8855 回答:脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中

哦,现在我明白了!您所做的只是通过此菜单设置了GIZMO

enter image description here

仅显示在SceneView中。

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

来自Glossary

  

GameObject关联的图形叠加层   在一个场景中   ,并显示在“场景视图”中   。内置场景   诸如移动工具之类的工具为Gizmos   ,然后您可以使用纹理或脚本来创建自定义Gizmos。某些Gizmos仅在选择GameObject时绘制,而其他Gizmos由编辑者绘制,无论哪个GameObject   被选中。


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

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

enter image description here

但是我想您想实现的是在最终应用中渲染该图标。


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

您可能必须将引脚Texture's TextureType更改为Sprite (2D and UI)

enter image description here


通常,我也建议您使用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,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
}
本文链接:https://www.f2er.com/3111547.html

大家都在问