[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动


Input.mousePosition


[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动_第1张图片





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);
    }
}



[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动_第2张图片


因为

Input.mousePosition

计算的坐标,是以左下角为 原点。

所以跟随的Image图片的锚点,要设置为左下角。
并且Rect Transform的Pos X ,Pos Y,Pos Z为0,0,0


go.GetComponent().position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);

注意这里 的Image的 位置不能是 localPosition,会产生 偏差。

正确的是position。

你会发现,是Image图片的 中点,跟随 鼠标进行移动。

[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动_第3张图片


----------------------------------------

go.GetComponent().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().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);
    }
}
[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动_第4张图片



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);
    }
}


[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动_第5张图片



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);
    }
}


[Unity][UGUI]获得鼠标对应屏幕的坐标,图片跟随鼠标移动_第6张图片



----------------------------------------




参考文章:

1.UGUI之RectTransform知识点 图解
http://www.manew.com/thread-41633-1-1.html
(出处: -【游戏蛮牛】-ar增强现实,虚拟现实,unity3d,unity3d教程下载首选u3d,unity3d官网)

2.

ugui在运行时改变RectTransform的大小

3.


相关文章:

1.【Unity&NGUI】UI精灵跟随鼠标移动

2. 

[Unity][UGUI]Tooltip提示框

3.








你可能感兴趣的:(Unity,UGUI)