702 B
702 B
#unity/日常积累
拿到img和canvas画布对应的游戏实体,在update函数处理就好
public class test : MonoBehaviour
{
public GameObject img;
public GameObject canvas;
private void Update()
{
//得到画布的尺寸
Vector2 uisize = canvas.GetComponent<RectTransform>().sizeDelta;
float X = Input.mousePosition.x - Screen.width / 2f;
float Y = Input.mousePosition.y - Screen.height / 2f;
Vector2 finalPos = new Vector2(X * (uisize.x / Screen.width), Y * (uisize.y / Screen.height));
img.transform.localPosition = finalPos;
}
}