【问题标题】:GameObject (Prefab object ) created by script does not appear in Game View but appears in Scene View in Unity3d脚本创建的 GameObject(Prefab 对象)在 Unity3d 中不出现在 Game View 中但出现在 Scene View 中
【发布时间】:2019-11-13 09:06:10
【问题描述】:

我通过将其附加到球体对象的脚本创建了一个 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 时创建游戏对象

在播放模式下选择 Pin 对象时

【问题讨论】:

  • 你能添加一个选择了图钉的截图吗?
  • @Camile 我添加了,我能知道为什么它没有出现在游戏屏幕上吗?我为此坐了两天。请帮助提前谢谢
  • 看起来您的对象只是一个空变换?您在编辑器中看到的图钉的图形图像可能实际上是 Canvas 中的一个对象(尝试扩展它),而不是您选择的游戏对象。

标签: unity3d unity-components


【解决方案1】:

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

仅在 SceneView 中显示。

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

来自Glossary

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


正如 cmets 中所述,您的 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
}

【讨论】:

  • 嗨,谢谢,我现在可以在 Globe 中看到一些东西,但看不到我想要的图标 pin 红色背景,如图 pasteboard.co/IGu1m46.png 所示,但是我怎样才能将图像拖到我想要的位置图标图像,在您的示例中,您正在显示一个框,对我来说,当我尝试更改时,它是一个背景渲染器pasteboard.co/IGu0TiX.png
  • 如何将预制件引用到脚本中的代码,我从层次结构窗口创建了一个空游戏对象并将其命名为 gameObjectPinPrefab 并拖动到资产文件夹并单击资产文件夹中的“gameObjectPinPrefab”并添加如图pasteboard.co/IGupodu.png 中所示的组件“Sprite Renderer” 现在我如何将此gameObjectPinPrefab 引用到代码中?我是 Unity 的初学者,这就是为什么这个问题
  • 只需将相应的图标拖放到预制件的Sprite 字段中(您首先必须将Texture's TextureType 更改为Sprite (2D and UI))。然后退出预制编辑模式并将预制拖放到组件检查器中的gameObjectPinPrefab字段中
  • 最后我可以在浏览完这篇文章 (answers.unity.com/questions/1545197/…) 后添加图像/图标,因为我无法将图像拖到 myprefab 的 Sprite 字段。现在如图所示:pasteboard.co/IGuGWkF.png,但我怎样才能删除图标的白色背景?我需要玩图像还是其他任何事情要做?
  • 我可以添加图标,但是即使我尝试了不同的图像,图标的清晰度也不好这是我尝试的最终结果pasteboard.co/IGuOm6y.png
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 2017-07-07
相关资源
最近更新 更多