Unity 2D C# 在画布上实例化精灵.找不到问题所在 [英] Unity 2D C# Instantiate sprite on canvas. Can't find what's wrong

查看:30
本文介绍了Unity 2D C# 在画布上实例化精灵.找不到问题所在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了很多关于此的问题,但我仍然找不到我的问题是什么...我正在尝试在画布上实例化一个预制件.它由一个按钮和一个精灵组成.按钮看起来不错,但精灵在游戏中不可见(但在场景中可见).

I read many questions about this, but I still can't find what my problem is... I'm trying to instantiate a prefab at the canvas. It is compose of a button and a sprite. The button looks ok, but the sprite is not visible at the Game (but is visible at the Scene).

我做错了什么,但我看不到什么......

I'm doing something wrong, but I can't see what...

 [SerializeField] GameObject finishedLevel;

     private void Start()
 {
     finishedLevel = Instantiate(finishedLevel, transform.position, transform.rotation);
     finishedLevel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);

 }

推荐答案

SpriteRenderer 不能与 Canvas 一起使用.您混淆并误用了两者.

SpriteRenderer is not made to be used with the Canvas. You are confusing and misusing the two.

SpriteRenderer 用于显示 2D 对象,如 2D 动画角色或 2D 环境.您可以将 Rigidbody2DColliders 附加到 SpriteRenderer.

SpriteRenderer is used for displaying 2D Objects like a 2D animated character or a 2D environment. You can attach Rigidbody2D and Colliders to SpriteRenderer.

Canvas 仅用于 UI 显示.它用于显示 UI 文本、按钮、滑块、滚动条和图像等内容.您不应将 Rigidbody2D 和碰撞体附加到它或其子对象上.

Canvas is used for UI display only. It is used for displaying things such as UI texts, buttons, sliders, scrollbars and images. You shouldn't attach Rigidbody2D and Colliders to it or its child objects.

通过上面的解释,您应该能够确定使用哪一个.如果您只需要在 Canvas 下显示图像,请使用 ImageRawImage 组件,因为它们是 UI 系统的一部分.这应该可以工作,但不要使 SpriteRenderer 成为 Canvas 的子级.如果您必须使用 SpriteRenderer,请将其设为自己的对象或在另一个对象下,但不应在 Canvas 下.您可能会发现 Unity 的 UI 教程很有用.

With the explanation above, you should be able to determine which one to use. If you just need to display image under a Canvas, use the Image, or RawImage component since they are part of the UI system. This should work but do not make SpriteRenderer a child of a Canvas. If you have to use SpriteRenderer, make it its own object or under another object but it should not be under a Canvas. You may find Unity's UI tutorial useful.

这篇关于Unity 2D C# 在画布上实例化精灵.找不到问题所在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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