将 Vector2 转换为 Vector2Int
Converting Vector2 to Vector2Int
我正在尝试将 Vector2 转换为 Vector2Int,因为这就是我在游戏中用作光标的内容。我希望它捕捉到像素而不是像现在这样自由移动,我不知道如何转换当前的 Vector2,所以也许有人可以暗示要做什么,或者直接告诉我。我尝试时总是出错。
public RectTransform rect;
private Canvas myCanvas;
void Awake()
{
this.myCanvas = base.GetComponent<Canvas>();
}
// Update is called once per frame
void Update()
{
Vector2Int pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out pos);
rect.position = myCanvas.transform.TransformPoint(pos);
}
如果有人能帮助我,那就太好了!我已经坚持了好一阵子了。
很简单,用Vector2就可以了,不需要Vector2Int,看我的例子:
RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out Vector2 pos);
pos.x = (int)pos.x;
pos.y = (int)pos.y;
rect.position = myCanvas.transform.TransformPoint(pos);
您可能还想看看一些舍入方法:https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html
如有任何疑问,请随时提问...
我正在尝试将 Vector2 转换为 Vector2Int,因为这就是我在游戏中用作光标的内容。我希望它捕捉到像素而不是像现在这样自由移动,我不知道如何转换当前的 Vector2,所以也许有人可以暗示要做什么,或者直接告诉我。我尝试时总是出错。
public RectTransform rect;
private Canvas myCanvas;
void Awake()
{
this.myCanvas = base.GetComponent<Canvas>();
}
// Update is called once per frame
void Update()
{
Vector2Int pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out pos);
rect.position = myCanvas.transform.TransformPoint(pos);
}
如果有人能帮助我,那就太好了!我已经坚持了好一阵子了。
很简单,用Vector2就可以了,不需要Vector2Int,看我的例子:
RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out Vector2 pos);
pos.x = (int)pos.x;
pos.y = (int)pos.y;
rect.position = myCanvas.transform.TransformPoint(pos);
您可能还想看看一些舍入方法:https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html
如有任何疑问,请随时提问...