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().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//go.GetComponent().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//Camera.main.WorldToScreenPoint(Input.mousePosition);
go.GetComponent().position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
}
}
因为
Input.mousePosition
go.GetComponent().position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
注意这里 的Image的 位置不能是 localPosition,会产生 偏差。
正确的是position。
你会发现,是Image图片的 中点,跟随 鼠标进行移动。
----------------------------------------
go.GetComponent
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().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//go.GetComponent().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//Camera.main.WorldToScreenPoint(Input.mousePosition);
go.GetComponent().position = new Vector3(Input.mousePosition.x- (go.GetComponent().sizeDelta.x/2),
Input.mousePosition.y - (go.GetComponent().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().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//go.GetComponent().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//Camera.main.WorldToScreenPoint(Input.mousePosition);
go.GetComponent().position = new Vector3(Input.mousePosition.x + (go.GetComponent().sizeDelta.x/2),
Input.mousePosition.y + (go.GetComponent().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().sizeDelta = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
//go.GetComponent().localPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
//Camera.main.WorldToScreenPoint(Input.mousePosition);
go.GetComponent().position = new Vector3(Input.mousePosition.x + (go.GetComponent().sizeDelta.x/2),
Input.mousePosition.y - (go.GetComponent().sizeDelta.y / 2)
, 0);
}
}
----------------------------------------
参考文章:
1.UGUI之RectTransform知识点 图解
http://www.manew.com/thread-41633-1-1.html
(出处: -【游戏蛮牛】-ar增强现实,虚拟现实,unity3d,unity3d教程下载首选u3d,unity3d官网)
2.
3.
相关文章:
1.【Unity&NGUI】UI精灵跟随鼠标移动
2.
3.