【论】Unity的Input应用(一)
2015-03-27 15:34
351 查看
使用Unity做下列的功能实现:鼠标点击下去,出现抹布,抹布随着鼠标的移动而移动。当抹布擦拭神灯后,会出现灯神。
Notice : 界面摆放时,需要触发事件的UI置于最顶层。
//鼠标版 if (Input.GetMouseButtonDown(0)) { dusterCloth = Instantiate(dusterClothPre) as GameObject; dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition); dusterCloth.transform.parent = clothParent; dusterCloth.transform.localScale = Vector3.one; dusterCloth.transform.localEulerAngles = Vector3.zero; } if (Input.GetMouseButton(0)) { dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition); RaycastHit hit; Ray ray = ca.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.collider.name == "1" || hit.collider.name == "2") { timeMax += Time.deltaTime; if (timeMax > 1f) { timeMax = 0; //触发按钮 Debug.Log("Send Message Trigger"); transform.parent.GetComponent<Sg_Aladdin>().SelectBtnMessage(hit.collider.gameObject); } } } } if (Input.GetMouseButtonUp(0)) { //删掉抹布 Destroy(dusterCloth); timeMax = 0; }
//触屏版 if (Input.touchCount > 0) { var touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: dusterCloth = Instantiate(dusterClothPre) as GameObject; dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position); dusterCloth.transform.parent = clothParent; dusterCloth.transform.localScale = Vector3.one; dusterCloth.transform.localEulerAngles = Vector3.zero; break; case TouchPhase.Moved: dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position); RaycastHit hit; Ray ray = ca.ScreenPointToRay(Input.GetTouch(0).position); if (Physics.Raycast(ray, out hit)) { uilabel.text = hit.collider.name; if (hit.collider.name == "1" || hit.collider.name == "2") { timeMax += Time.deltaTime; if (timeMax > 1f) { timeMax = 0; //触发按钮 } } } break; case TouchPhase.Ended: //删掉抹布 Destroy(dusterCloth); timeMax = 0; break; } }
Notice : 界面摆放时,需要触发事件的UI置于最顶层。
相关文章推荐
- [原创]Enterprise Library深入解析与灵活应用(7):再谈PIAB与Unity之间的集成
- 给input的button 应用默认样式
- Unity 在MVC上的应用(下:ORM)
- Unity Android平台AdMob应用
- android应用中加入admob(unity,eclipse均可)
- Unity骨骼动力学应用
- Unity 在MVC上的应用(扩展篇:JQuery AJAX)
- Unity依赖注入容器的应用
- Flex---TextInput keyDown事件应用
- SAS input 选项的应用实例
- showInputDialog 和 showMessageDialog的简单应用~
- Input按钮的JavaScript多种应用
- Unity 在MVC上的应用(中:Repository)
- 偶得--Unity在asp.net mvc上的基本应用
- 旧题重现:HTML Label标签结合Input的应用
- Enterprise Library深入解析与灵活应用(3):倘若将Unity、PIAB、Exception Handling引入MVP模式.. .. ..
- MVC依赖注入Unity应用学习
- NLineInputFormat 应用
- Unity制作的应用在android平台上的发布步骤
- InputControls的应用