日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

unity获取ugui上鼠标位置

發布時間:2023/11/29 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unity获取ugui上鼠标位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class GetMousePos : MonoBehaviour {public Canvas canvas;//畫布private RectTransform rectTransform;//坐標void Start(){canvas = GameObject.Find("Canvas").GetComponent<Canvas>();rectTransform = canvas.transform as RectTransform; //也可以寫成this.GetComponent<RectTransform>(),但是不建議;}void Update(){if (Input.GetMouseButtonDown(0)){Vector2 pos;if (RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, canvas.worldCamera, out pos)){rectTransform.anchoredPosition = pos;Debug.Log(pos);}}} }

  新建場景,在場景中拖一個畫布(Canvas),然后隨便找個地方掛上這個腳本就好了。

  RectTransformUtility:矩陣變換工具

  RectTransformUtility.ScreenPointToLocalPointInRectangle 從屏幕點到矩形內的本地點

Parameters 參數

rectThe RectTransform to find a point inside.
camThe camera associated with the screen space position.
screenPointScreen space position.
localPointPoint 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 PointerEventData.enterEventData (for hover functionality) or PointerEventData.pressEventCamera (for click functionality). This will automatically use the correct camera (or null) for the given event.

當ScreenPointToLocalPointInRectangle從事件處理器內部提供一個PointerEventData對象被使用時,相機可以通過使用PointerEventData.enterEventData(為懸停功能)或者 PointerEventData.pressEventCamera(為單擊功能)被獲取。該函數將會自動對指定事件使用正確的相機(或者空)。?

?

RectTransform矩形變換

RectTransform.anchoredPosition 錨點位置

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.

錨點參考點是錨點的位置。如果錨點不在一起,四個錨點的位置是根據布置的中心點的位置插值替換的。

轉載于:https://www.cnblogs.com/lanrenqilanming/p/6610712.html

總結

以上是生活随笔為你收集整理的unity获取ugui上鼠标位置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。