Attach text to a gameobject [closed]

北城以北 提交于 2021-01-01 07:39:27

问题


I know there are 2 ways to do it. But none of them works for me.

The first way I tried is to create a gameObject and then put a cube(shape) and a canvas with Text in it. I set the position of shape, canvas(world space) and text to (0,0,0) so that they will stay together. However, Only the cube showed up. And disabling the cube still doesn't make the text display.

So I removed it and added Text Mesh to the shape and this required me to remove Mesh Filter. And now what I can see is only an empty space. Do I have any settings wrong?


回答1:


You can do that with a UI Button game object and a 3D cube. The steps are as follows:

  1. In your empty scene, create a 3D Cube.

  2. After that, create a UI Button object.

  3. Now that you have the button and the Canvas, drag the Canvas to become a child of the cube, like this:

  4. Now you have to set the Canvas to World Space render mode, remove the Canvas Scaler component, and set Width = Height = 1 and all Pos = 0.

  5. Final step, setup the Button: set Width = Height = 100, all Scale = 0.01 and Pos Z = -0.501.

Now you have a perfectly functional button that will stay attached to the front face of the cube, no matter where the cube is positioned or rotated.

You can of course add as many buttons you want, and position them to different faces of the cube (even more than one button to a single face etc.).




回答2:


I believe you are misunderstanding a few things here, let me try to clear them up for you:

  1. The use of Canvas:

A Canvas should (almost) always be a top-level object, meaning that it is not attached to any parent. Rather than that, the canvas serves as a parent to other objects, meaning that other objects are children of the canvas.

Also, having multiple canvas objects could cause you problems aswell, take a look at Panels and Layout Groups.

  1. The difference between Text Mesh and Text:

A Text (or UI-Text) is an object that is inside a canvas to display text on the canvas space. A text mesh on the other hand is an object that is rendered inside your world, in 3D space, meaning it does (and probably should not) be attached to a Canvas object.



来源:https://stackoverflow.com/questions/49469377/attach-text-to-a-gameobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!