关闭

Unity3d 实现用鼠标拖拽鼠标

标签: vectorclassupc
7189人阅读 评论(1) 收藏 举报
分类:

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

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

}


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:630957次
    • 积分:6795
    • 等级:
    • 排名:第3675名
    • 原创:53篇
    • 转载:190篇
    • 译文:3篇
    • 评论:82条
    最新评论