关闭

Unity3d 实现用鼠标拖拽鼠标

标签: vectorclassupc
6885人阅读 评论(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

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:635773次
    • 积分:6518
    • 等级:
    • 排名:第3642名
    • 原创:52篇
    • 转载:190篇
    • 译文:3篇
    • 评论:80条
    最新评论