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

版权声明:本文为博主原创文章,未经博主允许不得转载。该博客所用图片资源均作学习分享用途,请勿用于商业行为。传播者自负。 如果本博客所写文章侵犯到您的权益,请主动联系留言,我们将及时删除相关内容。 https://blog.csdn.net/BuladeMian/article/details/79963041


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

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

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


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.


相关文章:

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

2. 

[Unity][UGUI]Tooltip提示框

3.








阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页