且构网

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

将文本附加到游戏对象

更新时间:2023-08-26 19:22:34

您可以使用 UI Button 游戏对象和 3D 立方体来做到这一点.步骤如下:

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

  1. 在您的空场景中,创建一个 3D 立方体.

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

之后,创建一个 UI Button 对象.

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:

现在您必须将 Canvas 设置为 World Space 渲染模式,移除 Canvas Scaler 组件,并设置 Width = Height = 1 和所有 Pos = 0.

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.

最后一步,设置按钮:设置Width = Height = 100,所有Scale = 0.01Pos Z = -0.501>.

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