using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///让在编辑器模式下也可以调用生命周期的方法
[ExecuteInEditMode]
public class OverrideMaterial : MonoBehaviour
{
private Renderer This_renderer;
private Renderer GetRenderer
{
get
{
if (This_renderer == null)
{
This_renderer = GetComponent<Renderer>();
}
return This_renderer;
}
}
public bool IsTrueVector1;
public OverrideVector1[] overrideVector1s;
public bool IsTrueColor;
public OverrideColor[] overrideColors;
public bool IsTrueTex;
public OverrideTex[] overrideTices;
private void Start()
{
OverriderMaterOn();
}
void OverriderMaterOn()
{
bool active = false;
MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
if (IsTrueVector1)
{
for (int i = 0; i < overrideVector1s.Length; i++)
{
materialPropertyBlock.SetFloat(overrideVector1s[i].Name, overrideVector1s[i].Value);
}
active = true;
}
if (IsTrueColor)
{
for (int i = 0; i < overrideColors.Length; i++)
{
materialPropertyBlock.SetColor(overrideColors[i].Name, overrideColors[i].Value);
}
active = true;
}
if (IsTrueTex)
{
for (int i = 0; i < overrideTices.Length; i++)
{
materialPropertyBlock.SetTexture(overrideTices[i].Name, overrideTices[i].Value);
}
active = true;
}
if (active)
{
GetRenderer.SetPropertyBlock(materialPropertyBlock);
}
}
#if UNITY_EDITOR //方便编辑器模式的调试
private void Update()
{
OverriderMaterOn();
}
#endif
[System.Serializable]
public struct OverrideVector1
{
public string Name;
public float Value;
}
[System.Serializable]
public struct OverrideColor
{
public string Name;
public Color32 Value;
}
[System.Serializable]
public struct OverrideTex
{
public string Name;
public Texture2D Value;
}
}