Unity 2D C# Instantiate sprite on canvas. Can't find what's wrong

前端 未结 1 2034
孤城傲影
孤城傲影 2020-12-20 01:34

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. Th

相关标签:
1条回答
  • 2020-12-20 02:31

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

    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 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.

    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.

    0 讨论(0)
提交回复
热议问题