在 Unity 中使一个 GameObject 粘到另一个 GameObjects 边框上
我使用 Unity3D 和 MRTK 进行 Hololens 2 开发。
正如您在图片中看到的,我获得了一个 Square
-Gameobject 和一个 AppBar
-Gameobject。 Square
附加了 MRTK BoundsControl
,使其可变形。用户可以使用手柄更改Square
的大小和旋转。
每次更改时,用户的 AppBar
都应该粘在 Square
的左边框上。
对象
- 游戏对象的比例为:X: 1、Y: 1、Z: 1。Square
-游戏对象的比例为:X:0.15,y:0.15,Z:0.009。
当我使用手柄缩放 Square
时,仅 Object
的比例发生变化,Square
的比例始终保持不变。
我已经尝试过这段代码,但它不能正常工作:
using UnityEngine;
public class AppBar : MonoBehaviour
{
public GameObject appBar;
public GameObject MaterialObject;
public void MoveAppBar()
{
var distance = MaterialObject.transform.localScale.x + MaterialObject.transform.GetChild(0).localScale.x - 1;
appBar.transform.position = new Vector3(-distance - 0.032f, 0, 0);
}
}
I'm using Unity3D with MRTK for Hololens 2 development.
As you can see in the picture I gat a Square
-Gameobject and a AppBar
-Gameobject. The Square
hast MRTK BoundsControl
attached to it which make it transformable. The user can change the size and rotation of the Square
with the handles.
On every change the user does the AppBar
should stick to the left border of the Square
.
The Object
-Gameobject has a scale of: X: 1, Y: 1, Z: 1.
The Square
-Gameobject has a scale of: X: 0.15, y: 0.15, Z: 0.009.
When I scale the Square
with the handles only the scale of the Object
changes the scale of the Square
is staying the same all the time.
I already tried this code but it doesn't work properly:
using UnityEngine;
public class AppBar : MonoBehaviour
{
public GameObject appBar;
public GameObject MaterialObject;
public void MoveAppBar()
{
var distance = MaterialObject.transform.localScale.x + MaterialObject.transform.GetChild(0).localScale.x - 1;
appBar.transform.position = new Vector3(-distance - 0.032f, 0, 0);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将一个空对象添加到放大/缩小对象的边缘。它需要位于边缘,以便当主对象被修改时,它将保留在边界上。
将此代码添加到 UI 上的脚本中:
将空对象拖到 tr 槽并指定一个偏移值以指示您希望 UI 与该空对象保持多远。
现在,您应该能够缩放主对象,并且 UI 将保持与边缘的相同距离,而不会进一步移动或进入主对象内部。
更好的方法是通过事件/监听器监听主要对象的修改,这样即使什么也没有发生,你也不会运行更新。
Add an empty object to the edge of the object that gets scale up/down. It needs to be on the edge so that when the main object gets modified, it will remain on the border.
Add this code to a script on the UI:
Drag the empty object to the tr slot and give a value to offset to indicate how far from that empty object you want the UI to remain.
Now you should be able to scale the main object and the UI will stick to that same distance from the edge without getting further or inside the main object.
A better approach would be to listen to the main object modification via event/listener so you don't run the update even when nothing happens.