MaterialColorChanger.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using UnityEngine;
  3. public class MaterialColorChanger : MonoBehaviour
  4. {
  5. private void Start()
  6. {
  7. this.m_mats = new Material[this.m_renders.Length];
  8. this.nPropID = Shader.PropertyToID(this.m_strColorPropName);
  9. for (int i = 0; i < this.m_renders.Length; i++)
  10. {
  11. this.m_mats[i] = this.m_renders[i].sharedMaterials[0];
  12. }
  13. if (this.m_bCopy)
  14. {
  15. this.m_matCopySrc = this.m_goCopySrc.GetComponent<Renderer>().sharedMaterials[0];
  16. }
  17. }
  18. private void Update()
  19. {
  20. if (Time.frameCount % this.m_nFrameSkip == 0 && this.m_mats != null && this.m_mats.Length != 0)
  21. {
  22. if (this.m_bCopy)
  23. {
  24. for (int i = 0; i < this.m_mats.Length; i++)
  25. {
  26. this.m_mats[i].SetColor(this.nPropID, this.m_matCopySrc.GetColor(this.nPropID));
  27. }
  28. }
  29. else
  30. {
  31. for (int j = 0; j < this.m_mats.Length; j++)
  32. {
  33. this.m_mats[j].SetColor(this.nPropID, this.m_color);
  34. }
  35. }
  36. }
  37. }
  38. [SerializeField]
  39. [Header("対象オブジェクトグループ")]
  40. public Renderer[] m_renders;
  41. [SerializeField]
  42. [Header("固定Colorを指定する場合場合")]
  43. public Color m_color;
  44. [SerializeField]
  45. [Header("別オブジェクトからコピーする場合ON")]
  46. public bool m_bCopy;
  47. [SerializeField]
  48. [Header("別オブジェクトからコピーする場合の元")]
  49. public GameObject m_goCopySrc;
  50. [SerializeField]
  51. [Header("対象のプロパティ名(Shaderプロパティ)")]
  52. public string m_strColorPropName = "_TintColor";
  53. [SerializeField]
  54. [Header("負荷軽減")]
  55. public int m_nFrameSkip = 3;
  56. private Material[] m_mats;
  57. private int nPropID;
  58. private Material m_matCopySrc;
  59. }