unity控制材质颜色或者更换材质
2018-01-17 19:30
260 查看
控制材质颜色或者更换材质
(标注部分为主要内容)
主要知识点:
声明一个数组:
public Button[] btns;
public string colors;
获得Resources目录下某个文件夹下的全部材质
Material[] matArr = Resources.LoadAll<Meterial>("Materials/colors");
字典对象的使用
Dictionary<string,Material> matdic = new Dictionary<string,Material>();
Material redmat = matArr[0];
matdic.add("red",redMat);
1.如何使用for循环
2.给按钮添加鼠标事件onClick.AddListener
3.获取物体上面的组件GetComponent方法
4.获取物体的SkinnedmeshRenderer渲染器,修改它的material属性变换材质,或者修改material.color属性改变颜色
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEditorInternal;
public class AttackBoy : MonoBehaviour
{
public Animator animator;
public GameObject boy;
string color;
float height;
float speed;
float rotationSpeed = 30;
Vector3 targetDirection;
float h, v;
//Dictionary
Dictionary<string, Material> matdic = new Dictionary<string, Material>();
//声名一个数组
public Button[] btns;
public string[] colors;
Material[] matArr;
public RuntimeAnimatorController animctrl;
private void Start()
{
height = 0.6f;
InitBoy();
//使用 Resources.LoadAll加载资源目录下的某个文件夹下的所有资源
matArr =
Resources.LoadAll<Material>("Materials/colors");
//如何使用for循环for (int i=0;i<4;i++)
for (int i = 0; i < 4; i++) {
Debug.Log(i);
//一对对把材质存到字典里
matdic.Add(colors[i], matArr[i]);
}
//给按钮添加鼠标事件onClick.AddListener
// 没有名字的方法 ,匿名方法
btns[0].onClick.AddListener(delegate()
{ ChangeColorByMat("blue"); });
btns[1].onClick.AddListener(delegate() { ChangeColorByMat("green"); });
btns[2].onClick.AddListener(delegate() { ChangeColorByMat("red"); });
btns[3].onClick.AddListener(delegate() { ChangeColorByMat("yellow"); });
//btns[1].onClick.AddListener(delegate() { ChangeColorByHex("04FBB4FF");)
}
void ChangeColorByHex(string hex) {
//获取物体上面的组件GetComponent方法
boy.GetComponent<SkinnedMeshRenderer>().material.color = HexToColor(hex);
}
void ChangeColorByMat(string color) {
//获取物体的SkinnedmeshRenderer渲染器,修改他的 material属性
boy.GetComponent<SkinnedMeshRenderer>().material = matdic[color];
}
void OnGUI()
{
if (GUI.Button(new Rect(300, 85, 100, 30), "Attack1"))
{
//执行 boy 攻击的方法
Attack();
}
}
void Update()
{
//获取键盘方向键输入
h =
Input.GetAxis("Horizontal");
v =
Input.GetAxis("Vertical");
//执行 boy 行走的方法
Walk();
}
void InitBoy()
{
Vector3 boyScale = transform.localScale;
transform.localScale = boyScale * height;
boy.GetComponent<SkinnedMeshRenderer>().material.color = HexToColor(color);
}
void Walk() {
if (h!=0||v!=0) {
animator.SetBool("Moving", true);
}
else{
animator.SetBool("Moving", false);
}
RotateTowardMovementDirection();
GetCameraRelativeMovement();
}
void Attack() {
animator.SetTrigger("Attack1Trigger");
}
void GetCameraRelativeMovement()
{
Transform cameraTransform = Camera.main.transform;
Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, 0, -forward.x);
targetDirection = h * right + v * forward;
}
//face character along input direction
void RotateTowardMovementDirection()
{
if (h != 0 || v != 0)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), Time.deltaTime * rotationSpeed);
}
}
private Color32 HexToColor(string hex)
{
hex = hex.Replace("0x", "");//in case the string is formatted 0xFFFFFF
hex = hex.Replace("#", "");//in case the string is formatted #FFFFFF
byte a = 255;//assume fully visible unless specified in hex
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
}
return new Color32(r, g, b, a);
}
}
color = "F92330FF";
forward.y = 0;
(标注部分为主要内容)
主要知识点:
声明一个数组:
public Button[] btns;
public string colors;
获得Resources目录下某个文件夹下的全部材质
Material[] matArr = Resources.LoadAll<Meterial>("Materials/colors");
字典对象的使用
Dictionary<string,Material> matdic = new Dictionary<string,Material>();
Material redmat = matArr[0];
matdic.add("red",redMat);
1.如何使用for循环
2.给按钮添加鼠标事件onClick.AddListener
3.获取物体上面的组件GetComponent方法
4.获取物体的SkinnedmeshRenderer渲染器,修改它的material属性变换材质,或者修改material.color属性改变颜色
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEditorInternal;
public class AttackBoy : MonoBehaviour
{
public Animator animator;
public GameObject boy;
string color;
float height;
float speed;
float rotationSpeed = 30;
Vector3 targetDirection;
float h, v;
//Dictionary
Dictionary<string, Material> matdic = new Dictionary<string, Material>();
//声名一个数组
public Button[] btns;
public string[] colors;
Material[] matArr;
public RuntimeAnimatorController animctrl;
private void Start()
{
height = 0.6f;
InitBoy();
//使用 Resources.LoadAll加载资源目录下的某个文件夹下的所有资源
matArr =
Resources.LoadAll<Material>("Materials/colors");
//如何使用for循环for (int i=0;i<4;i++)
for (int i = 0; i < 4; i++) {
Debug.Log(i);
//一对对把材质存到字典里
matdic.Add(colors[i], matArr[i]);
}
//给按钮添加鼠标事件onClick.AddListener
// 没有名字的方法 ,匿名方法
btns[0].onClick.AddListener(delegate()
{ ChangeColorByMat("blue"); });
btns[1].onClick.AddListener(delegate() { ChangeColorByMat("green"); });
btns[2].onClick.AddListener(delegate() { ChangeColorByMat("red"); });
btns[3].onClick.AddListener(delegate() { ChangeColorByMat("yellow"); });
//btns[1].onClick.AddListener(delegate() { ChangeColorByHex("04FBB4FF");)
}
void ChangeColorByHex(string hex) {
//获取物体上面的组件GetComponent方法
boy.GetComponent<SkinnedMeshRenderer>().material.color = HexToColor(hex);
}
void ChangeColorByMat(string color) {
//获取物体的SkinnedmeshRenderer渲染器,修改他的 material属性
boy.GetComponent<SkinnedMeshRenderer>().material = matdic[color];
}
void OnGUI()
{
if (GUI.Button(new Rect(300, 85, 100, 30), "Attack1"))
{
//执行 boy 攻击的方法
Attack();
}
}
void Update()
{
//获取键盘方向键输入
h =
Input.GetAxis("Horizontal");
v =
Input.GetAxis("Vertical");
//执行 boy 行走的方法
Walk();
}
void InitBoy()
{
Vector3 boyScale = transform.localScale;
transform.localScale = boyScale * height;
boy.GetComponent<SkinnedMeshRenderer>().material.color = HexToColor(color);
}
void Walk() {
if (h!=0||v!=0) {
animator.SetBool("Moving", true);
}
else{
animator.SetBool("Moving", false);
}
RotateTowardMovementDirection();
GetCameraRelativeMovement();
}
void Attack() {
animator.SetTrigger("Attack1Trigger");
}
void GetCameraRelativeMovement()
{
Transform cameraTransform = Camera.main.transform;
Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, 0, -forward.x);
targetDirection = h * right + v * forward;
}
//face character along input direction
void RotateTowardMovementDirection()
{
if (h != 0 || v != 0)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetDirection), Time.deltaTime * rotationSpeed);
}
}
private Color32 HexToColor(string hex)
{
hex = hex.Replace("0x", "");//in case the string is formatted 0xFFFFFF
hex = hex.Replace("#", "");//in case the string is formatted #FFFFFF
byte a = 255;//assume fully visible unless specified in hex
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
if (hex.Length == 8)
{
a = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
}
return new Color32(r, g, b, a);
}
}
color = "F92330FF";
forward.y = 0;
相关文章推荐
- unity,如何手动或者使用代码更换材质
- 【Unity&UGUI&Shader】创建材质球Material&代码控制更换物体材质球
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
- jquery 控制表格或者列表 偶数行的颜色
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
- Unity使用Shader控制物体材质的透明度(Lambert版和非光照版)
- [blender][blender教程].obj.fbx导入Unity的材质无颜色问题以及解决办法
- Unity的基本Shader框架写法&颜色、光照与材质
- unity 2017.3 Tips 控制颜色渐变
- 命令行或者Console程序中文字颜色控制
- unity->C#->组件->点击cube1 可以控制 光源亮度 cube1变大变小 cube2旋转 sphere颜色 cylander走停
- unity学习笔记~材质金属颜色对照表
- css控制,鼠标移动到DIV层上,更换背景色颜色,兼容IE6,IE7,FF
- unity 一个mesh renderer上有两个材质球更换材质球的方法
- Unity控制物体旋转修改物体颜色
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
- Unity控制台控制输出字体的颜色
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
- unity修改材质属性和更换shader
- Unity中通过按键更换材质球纹理图片