夜空的流星的专栏

嵌入式、数字图像/数字信号处理、DSP/ARM、Linux、VC/MFC

Unity3d 实现用鼠标拖拽鼠标

实现用鼠标拖拽鼠标  鼠标进入物体改变物体颜色

using UnityEngine;
using System.Collections;

public class clickObj : MonoBehaviour {
	
	public Texture2D[] pic = new Texture2D[4];
	private Color c;
	private Texture origPic;
	private int index;
	
	private Vector3 screenPoint;
	private Vector3 scanPos;
	private Vector3 offset;
	private Vector3 origPos;
	
	static public bool bDrag;
	// Use this for initialization
	void Start () {
		c = renderer.material.color;
		origPic = renderer.material.mainTexture;
		//pic = new Texture2D[4];
		index = 0;
		scanPos = this.transform.position;
		origPos = scanPos;
		bDrag = false;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	void OnMouseDown(){
		print("Mouse down");
		renderer.material.color = c;
		renderer.material.mainTexture = pic[index];
		index++;
		if (index == 3)
			index = 0;
		
		/////
		screenPoint = Camera.main.WorldToScreenPoint(scanPos);
  	    offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
		Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
	    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
		//print(curPosition);
	    transform.position = curPosition;
	}
	void OnMouseEnter(){
		print("Mouse enter");
		index = 0;
		renderer.material.color = Color.red;
	}
	void OnMouseExit(){
		print("Mouse exit");
		renderer.material.color = c;
		renderer.material.mainTexture = origPic;
	}
	
	void OnMouseDrag()
	{
		bDrag = true;
	    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
	    Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
	    transform.position = curPosition;
		print("curPosition"+curPosition);
	}
	
	void OnMouseUp(){
		print("mouse up");
		//transform.TransformPoint(origPos);
		bDrag = false;
		transform.position = origPos;
	}

}


 

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xgx198831/article/details/7410942
文章标签: vector class up c
个人分类: Unity3D
想对作者说点什么? 我来说一句

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

不良信息举报

Unity3d 实现用鼠标拖拽鼠标

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭