问题
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:
In your empty scene, create a 3D Cube.
After that, create a
UI Button
object.Now that you have the button and the Canvas, drag the Canvas to become a child of the cube, like this:
Now you have to set the Canvas to
World Space
render mode, remove theCanvas Scaler
component, and setWidth = Height = 1
and allPos = 0
.Final step, setup the Button: set
Width = Height = 100
, allScale = 0.01
andPos 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:
- 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.
- 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