12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using UnityEngine;
- public class MaterialColorChanger : MonoBehaviour
- {
- private void Start()
- {
- this.m_mats = new Material[this.m_renders.Length];
- this.nPropID = Shader.PropertyToID(this.m_strColorPropName);
- for (int i = 0; i < this.m_renders.Length; i++)
- {
- this.m_mats[i] = this.m_renders[i].sharedMaterials[0];
- }
- if (this.m_bCopy)
- {
- this.m_matCopySrc = this.m_goCopySrc.GetComponent<Renderer>().sharedMaterials[0];
- }
- }
- private void Update()
- {
- if (Time.frameCount % this.m_nFrameSkip == 0 && this.m_mats != null && this.m_mats.Length != 0)
- {
- if (this.m_bCopy)
- {
- for (int i = 0; i < this.m_mats.Length; i++)
- {
- this.m_mats[i].SetColor(this.nPropID, this.m_matCopySrc.GetColor(this.nPropID));
- }
- }
- else
- {
- for (int j = 0; j < this.m_mats.Length; j++)
- {
- this.m_mats[j].SetColor(this.nPropID, this.m_color);
- }
- }
- }
- }
- [SerializeField]
- [Header("対象オブジェクトグループ")]
- public Renderer[] m_renders;
- [SerializeField]
- [Header("固定Colorを指定する場合場合")]
- public Color m_color;
- [SerializeField]
- [Header("別オブジェクトからコピーする場合ON")]
- public bool m_bCopy;
- [SerializeField]
- [Header("別オブジェクトからコピーする場合の元")]
- public GameObject m_goCopySrc;
- [SerializeField]
- [Header("対象のプロパティ名(Shaderプロパティ)")]
- public string m_strColorPropName = "_TintColor";
- [SerializeField]
- [Header("負荷軽減")]
- public int m_nFrameSkip = 3;
- private Material[] m_mats;
- private int nPropID;
- private Material m_matCopySrc;
- }
|