unity3d 5.0 点击鼠标动态改变gameObject的material
来源:互联网 发布:sql进阶课程 编辑:程序博客网 时间:2024/05/21 10:22
在unity3d5.0中 renderer后面不能使用material
需要通过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.ValuesBegan 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.
阅读全文
0 0
- unity3d 5.0 点击鼠标动态改变gameObject的material
- Unity3d脚本改变GameObject的Material(一)
- Unity3d脚本改变GameObject的Material(二)
- Unity3d 对Gameobject实现鼠标右键点击
- Unity3D中Gameobject与gameobject的区别
- 【Unity3D】改变鼠标样式
- 【Unity 3D学习】获取鼠标点击所对应的GameObject
- Unity3d中GameObject.active或gameObject.SetActiveRecursively无效的问题
- Material Design实现应用动态改变色调的方法
- Unity中动态改变物体的Material(Shader)
- Unity3D的GameObject的SendMessage相关函数
- Unity3D控制gameobject脚本执行的顺序
- Unity3d中物件(gameobject)的基础操作
- Unity3D如何获取GameObject上的Component
- 动态改变鼠标光标
- 鼠标点击后改变样式
- 改变鼠标贴图,点击变化
- 【Unity3D自学记录】简单的鼠标点击控制人物走动
- 用clang编译器编译OC代码
- 搭建mysql编译环境以及遇到的问题
- 初识IT
- ubuntu--多媒体开发
- 使用opencv实现通过摄像头自动输入阿里云身份宝验证码
- unity3d 5.0 点击鼠标动态改变gameObject的material
- java读代码步骤
- codevs1014 装箱问题
- k-邻近算法(二)
- Android 中EditText的密码明文与密文显示
- jzoj P2151【2017.7.7普及】分数
- Python一些常见的字符串处理函数
- 使用Android Studio 自带的monitors查找App CPU 使用率过高的原因
- Ubuntu 16.04 几个国内更新源