[英]Unity - Move Canvas as a 3D GameObject
i have a script with which i can move (on 2 axis) any 3D objects, the script i'm using is this (for mouse and touchscreen) 我有一个脚本,可以使用它在2轴上移动任何3D对象,我正在使用的脚本是这个(用于鼠标和触摸屏)
using UnityEngine;
public class DragInput : MonoBehaviour {
GameObject gObj = null;
Plane objPlane;
Vector3 mO;
Camera cam;
void Awake() {
cam = GameObject.FindGameObjectWithTag("MyCamera").GetComponent<Camera>();
}
Ray GenerateMouseRay()
{
Vector3 mousePosFar = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cam.farClipPlane);
Vector3 mousePosNear = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cam.nearClipPlane);
Vector3 mousePosF = cam.ScreenToWorldPoint(mousePosFar);
Vector3 mousePosN = cam.ScreenToWorldPoint(mousePosNear);
Ray mr = new Ray(mousePosN, mousePosF-mousePosN);
return mr;
}
// Update is called once per frame
void Update ()
{
if(Input.GetMouseButtonDown(0))
{
Ray mouseRay = GenerateMouseRay();
RaycastHit hit;
if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
{
gObj = hit.transform.gameObject;
objPlane = new Plane(cam.transform.forward*-1, gObj.transform.position);
//calc mouse offset
Ray mRay = cam.ScreenPointToRay(Input.mousePosition);
float rayDistance;
objPlane.Raycast(mRay, out rayDistance);
mO = gObj.transform.position - mRay.GetPoint(rayDistance);
}
}
else if(Input.GetMouseButton(0) && gObj)
{
Ray mRay = cam.ScreenPointToRay(Input.mousePosition);
float rayDistance;
if (objPlane.Raycast(mRay, out rayDistance))
gObj.transform.position = mRay.GetPoint(rayDistance) + mO;
}
else if (Input.GetMouseButtonUp(0) && gObj)
{
gObj = null;
}
}
}
If i click on any object i have created (with a box collider) i can freely move this object over the screen. 如果我单击已创建的任何对象(带有对撞机),则可以在屏幕上自由移动该对象。 Unfortunately this will not happen if the object is a World Space UI(canvas). 不幸的是,如果对象是World Space UI(canvas),则不会发生这种情况。 Anyone can gime me some hint and tell me why i'm not able to move the space UI? 任何人都可以给我一些提示,并告诉我为什么我无法移动空间UI?
You can have a look at RectTransform
: 您可以看一下RectTransform
:
What happens is that if you want to move a UI element, this element is moved in relation to it's parent Canvas object. 发生的事情是,如果您要移动UI元素,则该元素将相对于其父Canvas对象进行移动。
It's position is a Vector2 on the Canvas area. 它的位置是“画布”区域上的Vector2。 It's a 2D element, even if it's placed in 3D space. 即使是放置在3D空间中,它也是2D元素。 Also, pay attention to the fact that it's position is also defined in relation to it's Anchor on the Canvas ( anchoredPosition
) : 另外,请注意,它的位置也相对于其在画布上的锚点( anchoredPosition
)进行了定义:
You can parent the Canvas under a regular GameObject if you want to manipulate it's Transform like you would do for any other GameObject in the scene. 如果要操纵画布的“变形”,就可以像在场景中其他任何GameObject一样将Canvas作为父对象。
You can parent the Canvas under a regular GameObject if you want to manipulate it's Transform like you would do for any other GameObject in the scene 您可以将Canvas作为常规GameObject下的父对象,如果要像场景中其他任何GameObject一样操作它的Transform,
It was a good idea but i had to add a 3D collider too to the empty object, 2D collider did not work (and still do not understand why) 这是一个好主意,但我也必须向空对象添加3D碰撞器,2D碰撞器无法正常工作(并且仍然不明白为什么)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.