unity控制材质颜色或者更换材质

控制材质颜色或者更换材质
(标注部分为主要内容)

unity控制材质颜色或者更换材质

主要知识点:
声明一个数组:
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;