在画布上实例化精灵

在画布上实例化精灵

本文介绍了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.

画布仅用于UI显示.它用于显示诸如UI文本,按钮,滑块,滚动条和图像之类的东西.您不应将Rigidbody2D和Colliders附加到其或其子对象.

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下显示图像,请使用 Image ,或 RawImage 组件,因为它们是UI系统的一部分.这应该可以,但是不要SpriteRenderer设为Canvas的子代.如果必须使用SpriteRenderer,请将其设为自己的对象或在另一个对象下,但不应置于画布"下.您可能会发现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#在画布上实例化精灵.找不到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:05