简体   繁体   English

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

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

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 is not made to be used with the Canvas. SpriteRenderer不能与Canvas一起使用。 You are confusing and misusing the two. 您正在混淆和误用两者。

SpriteRenderer is used for displaying 2D Objects like a 2D animated character or a 2D environment. SpriteRenderer用于显示2D对象,例如2D动画角色或2D环境。 You can attach Rigidbody2D and Colliders to SpriteRenderer . 您可以将Rigidbody2DColliders附加到SpriteRenderer

Canvas is used for UI display only. 画布仅用于UI显示。 It is used for displaying things such as UI texts, buttons, sliders, scrollbars and images. 它用于显示诸如UI文本,按钮,滑块,滚动条和图像之类的内容。 You shouldn't attach Rigidbody2D and Colliders to it or its child objects. 不应将Rigidbody2D和Colliders附加到其或其子对象。

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. 如果只需要在Canvas下显示图像,请使用ImageRawImage组件,因为它们是UI系统的一部分。 This should work but do not make SpriteRenderer a child of a Canvas. 这应该可以,但不要使SpriteRenderer成为Canvas的子级。 If you have to use SpriteRenderer , make it its own object or under another object but it should not be under a Canvas. 如果必须使用SpriteRenderer ,则使其成为自己的对象或在另一个对象下,但不应在Canvas下。 You may find Unity's UI tutorial useful. 您可能会发现Unity的UI 教程很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM