脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中 [英] GameObject (Prefab object ) created by script does not appear in Game View but appears in Scene View in Unity3d

查看:448
本文介绍了脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a pin object by script attached it to a sphere 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()
    {

    }
}

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

This script is attached to the sphere Object .My sphere Object have shader material of earth image (globe). This Instantiated Prefabs (gameObjectPin) on sphere surface appears on scene but not on game screen ,When I select the camera object in the camera preview also this object does not appear .

场景视图

选择摄像机时的场景视图

Scene View when camera is selected

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

I am new to Unity what should I check or correct to appear my created object on the sphere basically I am trying to add pins to corresponding country and label it .Similar to the globe on this http://kitsdmcc.com/news

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

Gameobject is created when Play is clicked on the sphere object

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

When the Pin Object is selected on play mode

推荐答案

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

Oh now I see it! What you did was only setting its GIZMO via this menu

仅显示在SceneView中.

which is only displayed in the SceneView.

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

This has nothing to do with the rendering in the GameView but is just a way for easier seeing and finding certain types of objects in the SceneView since usually they would be invisible if not selected.

词汇表:

GameObject关联的图形覆盖 在一个场景中 ,并显示在场景视图"中 .内置场景 诸如移动工具之类的工具是Gizmos ,您可以使用纹理或脚本创建自定义Gizmos.某些Gizmos仅在选择GameObject时绘制,而其他Gizmos由编辑器绘制,无论哪个GameObject 被选中.

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上根本没有任何组件,因此Gameview中没有呈现任何内容.


As noted in the comments there is no Component at all on your GameObject so nothing is rendered in the Gameview.

当然,现在您可以通过Gizmos切换

Of course now you could enable Gizmos also for the GameView via the Gizmos toggle

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

but I guess what you rather are trying to achieve is rather rendering that icon in the final App.

您可能想使用例如此处的 SpriteRenderer 组件.只需将您的Icon拖到Sprite属性.

You probably would like to use e.g. the SpriteRenderer component here. And simply drag in your Icon to the Sprite property.

您可能必须将图钉纹理的TextureType 更改为Sprite (2D and UI)

通常,我还建议您创建预制件,而不要使用Resources文件夹在这里.

In general I would also recommend to Create a Prefab instead of using the Resources folder here.

一般来说,我还会对您的代码进行一些更改:

There are also some changes I would do to your code in general:

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
}

这篇关于脚本创建的GameObject(预制对象)不会出现在Game View中,而是出现在Unity3d的Scene View中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆