標籤:

滑鼠事件(屏幕坐標和世界坐標的互相轉換)

public class Mousefunction : MonoBehaviour {

Color originalColor;

MeshRenderer mRnderer;

float z;

Vector3 offset;

// Use this for initialization

void Start () {

mRnderer = GetComponent<MeshRenderer>();

originalColor = mRnderer.material.color;

}

// Update is called once per frame

void Update () {

}

void OnMouseEnter()

{

print("進入物體");

//把將要拖拽的物體的世界坐標轉換為屏幕坐標

z = Camera.main.ScreenToWorldPoint(transform.position).z;

}

void OnMouseDown()

{

transform.localScale += new Vector3(1, 0, 0);

print("點擊了物體");

Vector3 point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, z);

point = Camera.main.ScreenToWorldPoint(point);

//計算偏移量

offset = transform.position - point;

}

void OnMouseUp()

{

print("彈起了滑鼠");

}

void OnMouseOver()

{

mRnderer.material.color = Color.yellow;

print("滑鼠在物體上");

}

void OnMouseExit()

{

print("離開物體");

}

void OnMouseUpAsButton()

{

print("在物體上點擊並彈起了滑鼠");

}

void OnMouseDrag()

{

print("拖拽");

//滑鼠位置 Input.mousePosition

Vector3 orgPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, z);

Vector3 point = Camera.main.ScreenToWorldPoint(orgPosition);

transform.position = new Vector3(point.x + offset.x, point.y + offset.y, transform.position.z);

}

}


推薦閱讀:

iPad mini 支持 retina 需要增加多少電池?
屏幕科普三部曲:拖影
今後手機用三星屏幕是趨勢嗎?為什麼?
為什麼早期屏幕是 4:3 比例?
我被一面「牆」圈粉了,不信你看

TAG:屏幕 |