且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Unity 2D C#在画布上实例化精灵.找不到问题

更新时间:2022-11-10 22:32:27

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.