因项目的需求,在制作中需要让UGUI中的组件,追随鼠标的运动轨迹。由于个人在以前使用中对于UGUI的坐标系了解不深,在使用中产生了一些问题,在此对UGUI的坐标系做一个简单的记录,以备后面查询。
Unity3D 采用InputmousePosition来获取当前鼠标的位置,获取的位置是相对于屏幕坐标系的,而UGUI使用的坐标系和屏幕坐标系不是同一个坐标系,
在UGUI中组件使用的坐标都是2D在Rect中的一个相对坐标,所以在实际使用中需要进行转换。
<pre>
public class test : MonoBehaviour
{
public RectTransform myRectTransform; // 目标组件
public Canvas myCanvas; // 当前画布
// Update is called once per frame
void Update ()
{
Vector2 vT = Vector2zero;
// 通过此函数,将鼠标坐标,从屏幕坐标,转换到UGUI坐标
RectTransformUtilityScreenPointToLocalPointInRectangle(myCanvastransform as RectTransform, InputmousePosition, myCanvasworldCamera, out vT);
myRectTransformlocalPosition = vT;
}
}
</pre>
#######附带一说,在Unity3D中可以使用函数ScreenshowCursor(老版本)、UnityEngineCursorvisible来显示和隐藏鼠标
using UnityEngine;
using SystemCollections;
using UnityEngineEverySystem;
public class NewBehaviourScript : MonoBehaviour
{
public Canvas canvas;
public RectTransform rectTransform;
void Start()
{
rectTransform=transform as RectTransform; //也可以写成thisGetComponent <RectTransform>(),但是不建议;
canvas=GameObjcetFind("Canvas")GetComponent<Canvas>();
}
void Update()
{
Vector2 pos;
if(RectTransformUtilityScreenPointToLocalPointInRectangle(canvastransf
orm as RectTransform, InputmousePosition, canvasworldCamera, out pos))
{
rectTransformanchoredPosition = pos;
DebugLog(pos);
}
}
}
public class GetMousePos : MonoBehaviour
{
public Canvas canvas;//画布
private RectTransform rectTransform;//坐标
void Start()
{
canvas = GameObjectFind("Canvas")GetComponent<Canvas>();
rectTransform = canvastransform as RectTransform; //也可以写成thisGetComponent<RectTransform>(),但是不建议;
}
void Update()
{
if (InputGetMouseButtonDown(0))
{
Vector2 pos;
if (RectTransformUtilityScreenPointToLocalPointInRectangle(rectTransform, InputmousePosition, canvasworldCamera, out pos))
{
rectTransformanchoredPosition = pos;
DebugLog(pos);
}
}
}
}
新建场景,在场景中拖一个画布(Canvas),然后随便找个地方挂上这个脚本就好了。
1
RectTransformUtility:矩阵变换工具
RectTransformUtilityScreenPointToLocalPointInRectangle 从屏幕点到矩形内的本地点
Parameters 参数
rect The RectTransform to find a point inside
cam The camera associated with the screen space position
screenPoint Screen space position
localPoint Point in local space of the rect transform
Returns
bool Returns true if the plane of the RectTransform is hit, regardless of whether the point is inside the rectangle
Description 描述
Transform a screen space point to a position in the local space of a RectTransform that is on the plane of its rectangle
屏幕空间点转换为矩形变换内部的本地位置,该点在它的矩形平面上。
The cam parameter should be the camera associated with the screen point For a RectTransform in a Canvas set to Screen Space - Overlay mode, the cam parameter should be null
该cam 参数应该是该相机关联的屏幕点。对于在画布上的矩形变换设置该屏幕空间为-Overlay模式,cam 参数应该为空。
When ScreenPointToLocalPointInRectangle is used from within an event handler that provides a PointerEventData object, the correct camera can be obtained by using PointerEventDataenterEventData (for hover functionality) or PointerEventDatapressEventCamera (for click functionality) This will automatically use the correct camera (or null) for the given event
当ScreenPointToLocalPointInRectangle从事件处理器内部提供一个PointerEventData对象被使用时,相机可以通过使用PointerEventDataenterEventData(为悬停功能)或者 PointerEventDatapressEventCamera(为单击功能)被获取。该函数将会自动对指定事件使用正确的相机(或者空)。
RectTransform矩形变换
RectTransformanchoredPosition 锚点位置
The position of the pivot of this RectTransform relative to the anchor reference point
该矩形变换相对于锚点参考点的中心点位置。
The anchor reference point is where the anchors are If the anchors are not together, the four anchor positions are interpolated according to the pivot placement
锚点参考点是锚点的位置。如果锚点不在一起,四个锚点的位置是根据布置的中心点的位置插值替换的。
using
UnityEngine;
using
SystemCollections;
using
UnityEngineUI;
///
<summary>
///
脚本位置:UGUI的
///
</summary>
public
class
test
:
MonoBehaviour
{
//
你的
private
RectTransform
mySprite
;
void
Start
()
{
mySprite
=
gameObjectGetComponent<RectTransform>();
}
void
Update
()
{
//
maxy返回的偏移量是个负数,所以需要乘以-1
DebugLog("Top"
+
":"
+
mySpriteoffsetMaxy
-1);
DebugLog("Bottom"
+
":"
+
mySpriteoffsetMiny
);
}
}
以上就是关于Unity3D UGUI组件跟随鼠标运动全部的内容,包括:Unity3D UGUI组件跟随鼠标运动、unity怎么快速获取两个canvas xyz轴之间的距离、unity UGUI如何获取鼠标指针所在位置的UI对象等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)