[Unity][UGUI]獲得滑鼠對應螢幕的座標,圖片跟隨滑鼠移動
阿新 • • 發佈:2019-01-24
Input.mousePosition
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test_Mouse : MonoBehaviour { public GameObject go; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Debug.Log(" "+Input.mousePosition); //go.GetComponent<RectTransform>().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y); //go.GetComponent<RectTransform>().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0); //Camera.main.WorldToScreenPoint(Input.mousePosition); go.GetComponent<RectTransform>().position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); } }
因為
Input.mousePosition
go.GetComponent<RectTransform>().position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
注意這裡 的Image的 位置不能是 localPosition,會產生 偏差。
正確的是position。
你會發現,是Image圖片的 中點,跟隨 滑鼠進行移動。
----------------------------------------
go.GetComponent<RectTransform>().sizeDelta【參考文章1,2】
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test_Mouse : MonoBehaviour { public GameObject go; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Debug.Log(" "+Input.mousePosition); //go.GetComponent<RectTransform>().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y); //go.GetComponent<RectTransform>().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0); //Camera.main.WorldToScreenPoint(Input.mousePosition); go.GetComponent<RectTransform>().position = new Vector3(Input.mousePosition.x- (go.GetComponent<RectTransform>().sizeDelta.x/2), Input.mousePosition.y - (go.GetComponent<RectTransform>().sizeDelta.y / 2) , 0); } }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_Mouse : MonoBehaviour {
public GameObject go;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log(" "+Input.mousePosition);
//go.GetComponent<RectTransform>().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//go.GetComponent<RectTransform>().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//Camera.main.WorldToScreenPoint(Input.mousePosition);
go.GetComponent<RectTransform>().position = new Vector3(Input.mousePosition.x + (go.GetComponent<RectTransform>().sizeDelta.x/2),
Input.mousePosition.y + (go.GetComponent<RectTransform>().sizeDelta.y / 2)
, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test_Mouse : MonoBehaviour {
public GameObject go;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log(" "+Input.mousePosition);
//go.GetComponent<RectTransform>().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//go.GetComponent<RectTransform>().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//Camera.main.WorldToScreenPoint(Input.mousePosition);
go.GetComponent<RectTransform>().position = new Vector3(Input.mousePosition.x + (go.GetComponent<RectTransform>().sizeDelta.x/2),
Input.mousePosition.y - (go.GetComponent<RectTransform>().sizeDelta.y / 2)
, 0);
}
}
----------------------------------------
參考文章:
1.UGUI之RectTransform知識點 圖解
http://www.manew.com/thread-41633-1-1.html
(出處: -【遊戲蠻牛】-ar增強現實,虛擬現實,unity3d,unity3d教程下載首選u3d,unity3d官網)
2.
ugui在執行時改變RectTransform的大小
3.
相關文章:
2.
3.