unity3d - 脚本创建的 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 时创建游戏对象
在播放模式下选择 Pin Object 时
解决方案
哦,现在我明白了!你所做的只是通过这个菜单设置它的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
}
推荐阅读
- locust - Locust docker 容器找不到 locust 文件
- django - 使用 Docker 进行 PostgreSQL 备份(cookiecutter)
- ios - 使用 Apple PoseNet 模型方法时的内存泄漏
- r - 是否可以查看或浏览表格以查找单个或多个值大于/小于某个值的行
- file - 如何使用 xamarin 在浏览器中打开本地文件?
- javascript - 如何使用选择框执行实时乘法?
- ruby-on-rails - Rails 6 POST Api 存储用户位置
- python - 如何使用回调函数更改图标?
- flutter - 在 Visual Studio Code 上创建颤振项目时出错
- c++ - 如何在不同的队列上同时使用 Vulkan 图像