简体   繁体   English

由脚本创建的 GameObject (Prefab object) 不会出现在 Game View 中,但会出现在 Unity3d 的 Scene View 中

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

I created a pin object by script attached it to a sphere object.我通过将其附加到球体 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()
    {

    }
}

This script is attached to the sphere Object.My sphere Object have shader material of earth image (globe).此脚本附加到球体 Object。我的球体 Object 具有地球图像(地球)的着色器材质。 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.球体表面上的这个实例化预制件(gameObjectPin)出现在场景上但不在游戏屏幕上,当我 select 相机预览中的相机 object 时,这个 ZA8CFDE6331BD59EB2AC96F8911C4B66Z 也没有出现。

Scene view场景视图

场景视图

Scene View when camera is selected选择相机时的场景视图

选择相机时的场景视图

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我是 Unity 的新手,我应该检查或更正什么以在球体上显示我创建的object

Gameobject is created when Play is clicked on the sphere object在球体 object 上单击 Play 时创建游戏对象

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

When the Pin Object is selected on play mode在播放模式下选择引脚 Object

在播放模式下选择 Pin Object 时

Oh now I see it!哦,现在我明白了! What you did was only setting its GIZMO via this menu你所做的只是通过这个菜单设置它的GIZMO

在此处输入图像描述

which is only displayed in the SceneView.仅在 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.这与 GameView 中的渲染无关,而只是一种更容易在 SceneView 中查看和查找某些类型对象的方法,因为如果不选择它们通常是不可见的。

From the Glossary :词汇表

A graphic overlay associated with a GameObject in a Scene, and displayed in the Scene View.与场景中的GameObject关联的图形叠加层,并显示在场景视图中。 Built-in scene tools such as the move tool are Gizmos , and you can create custom Gizmos using textures or scripting.移动工具等内置场景工具是Gizmos ,您可以使用纹理或脚本创建自定义Gizmos Some Gizmos are only drawn when the GameObject is selected, while other Gizmos are drawn by the Editor regardless of which GameObject s are selected.一些GameObject仅在选择Gizmos时绘制,而其他Gizmos由 Editor 绘制,无论选择哪个GameObject


As noted in the comments there is no Component at all on your GameObject so nothing is rendered in the Gameview.如评论中所述,您的GameObject上根本没有组件,因此在 Gameview 中没有渲染任何内容。

Of course now you could enable Gizmos also for the GameView via the Gizmos toggle当然,现在您也可以通过Gizmos切换为 GameView 启用 Gizmos

在此处输入图像描述

but I guess what you rather are trying to achieve is rather rendering that icon in the final App.但我想您更愿意尝试实现的是在最终应用程序中呈现该图标。


You probably would like to use eg theSpriteRenderer component here.您可能想在这里使用例如SpriteRenderer组件。 And simply drag in your Icon to the Sprite property.只需将您的图标拖到Sprite属性中即可。

You might have to change the Pin Texture's TextureType to Sprite (2D and UI) .您可能需要将 Pin Texture 的TextureType更改为Sprite (2D and UI)

在此处输入图像描述


In general I would also recommend to Create a Prefab instead of using the Resources folder here.一般来说,我还建议在这里创建一个 Prefab而不是使用Resources文件夹。


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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM