unity3d 5.0 点击鼠标动态改变gameObject的material
2017-07-08 18:21
232 查看
在unity3d5.0中 renderer后面不能使用material
需要通过GetComponent来获取组件
所以代码应该这样写:
如果是触摸事件的话,只需要把鼠标改成触摸就行了,如下:
Input.GetTouch(i).phase == TouchPhase.Began是判断触摸按下事件
其他的触摸事件如下:
需要通过GetComponent来获取组件
所以代码应该这样写:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class touch3d : MonoBehaviour { Ray ray; RaycastHit rayhit; // Use this for initialization void Start () { } void Update() { if (Input.GetMouseButtonUp(0)) { int target = LayerMask.GetMask("target"); Debug.Log(target); ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out rayhit, 100f, target)) { //rayhit.collider.gameObject.GetComponent<Renderer>().material.mainTexture = (Texture)Resources.Load("meinv"); rayhit.collider.gameObject.GetComponent<Renderer>().material.color = Color.blue; Debug.Log(rayhit.collider.gameObject.name); } } } }
如果是触摸事件的话,只需要把鼠标改成触摸就行了,如下:
using UnityEngine; using System.Collections; public class example : MonoBehaviour { public GameObject particle; void Update() { if (Input.GetTouch(i).phase == TouchPhase.Began) { int target = LayerMask.GetMask("target"); Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if (Physics.Raycast(ray, out rayhit, 100f, target)) rayhit.collider.gameObject.GetComponent<Renderer>().material.color = Color.blue; } } } }
Input.GetTouch(i).phase == TouchPhase.Began是判断触摸按下事件
其他的触摸事件如下:
TouchPhase Enumeration Describes phase of a finger touch. Values Began A finger touched the screen. Moved A finger moved on the screen. Stationary A finger is touching the screen but hasn't moved. Ended A finger was lifted from the screen. This is the final phase of a touch. Canceled The system cancelled tracking for the touch, as when (for example) the user puts the device to her face or more than five touches happened simultaneously. This is the final phase of a touch.
相关文章推荐
- Unity3d 对Gameobject实现鼠标右键点击
- Unity3d--根据鼠标点击的位置改变物体朝向
- 动态添加class名,改变标签样式(包含鼠标移入、移出及点击事件)
- [脚本] Unity3d 实现鼠标左键点击地形使角色移动到指定地点
- 鼠标移动改变背景色 鼠标点击后给据设定的值添加图片到指定的列
- 让鼠标点击有链接的图片文字时颜色不发生改变
- 百度有啊鼠标移上去图片位置动态改变效果
- DataGrid行颜色随鼠标点击改变
- javascript鼠标点击实现改变CSS样式
- 鼠标拖动动态改变表格的宽度的js脚本 兼容ie/firefox
- vc下动态改变鼠标光标
- winform的treeview控件点击鼠标右键就改变SelectedNode属性的方法
- 现在有动态生成table若干行,每行里会有一个button.当鼠标点击其中一个button时.如何让程序知道是点在第几行的button上
- opencv--用鼠标动态改变显示图像窗口大小
- 鼠标拖动动态改变表格的宽度的js脚本 兼容ie/firefox
- Silverlight学习笔记三(鼠标点击动态画直线|动态设置Ellipse的Canvas.Top与Canvas.Left|动态设置Stroke属性的方法。)
- DataGrid行颜色随鼠标点击改变
- DataGrid行颜色随鼠标点击改变
- DataGrid中鼠标点击、移动后行颜色改变效果
- 关于动态贴图问题不能动态通过texture改变只能重新new 新material