using UnityEngine;
using System.Collections;
public class ClickUIManager : MonoBehaviour {
private float OldTime =0.0f;
private float NewTime =0.0f;
private Vector3 mPosition;
public GameObject mClickTransfrom;
private float myScreenWidth;
private float myScreenHeight;
private float x =0.0f;
private float y = 0.0f;
private float z=10.0f;
void Start()
{
myScreenWidth = Screen.width;
myScreenHeight = Screen.height;
}
void FixedUpdate () {
if (Input.GetMouseButtonDown (0)) { //首先判断是否点击了鼠标左键
OldTime = Time.realtimeSinceStartup;
//定义一条射线,这条射线从摄像机屏幕射向鼠标所在位置
}
if(Input.GetMouseButtonUp(0))
{
NewTime = Time.realtimeSinceStartup;
if(NewTime - OldTime <0.15f)
{
x = Input.mousePosition.x - myScreenWidth/2;
y= Input.mousePosition.y - myScreenHeight/2;
mClickTransfrom.transform.localPosition =new Vector3(x/myScreenWidth*1080,y/myScreenHeight*1920,-500f);
mClickTransfrom.SetActive(false);
mClickTransfrom.SetActive(true);
}
}
}
}
这里 通过判断时间 来减少频率, Input.mousePosition 是2维屏幕的直接坐标,但是却 以中心点为0,0 根据实际屏幕大小得到的坐标值,所以要对其进行处理后才能正常使用。