Advertisement
Pro_Unit

InEditorMaterialPropertyBlockSetter

Apr 5th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using UnityEngine;
  5.  
  6. namespace GameCore
  7. {
  8.     [RequireComponent (typeof (Renderer))]
  9.     [ExecuteInEditMode]
  10.     public class InEditorMaterialPropertyBlockSetter : MonoBehaviour
  11.     {
  12.         [SerializeField] List<FloatProperty> floats;
  13.         [SerializeField] List<ColorProperty> colors;
  14.         [SerializeField] List<TextureProperty> textures;
  15.         [HideInInspector]
  16.         [SerializeField] Renderer rend;
  17.         private void OnValidate ()
  18.         {
  19.             ValidateRenderer ();
  20.             ValidateFloats ();
  21.             ValidateColors ();
  22.             ValidateTextures ();
  23.         }
  24.  
  25.         private void ValidateRenderer ()
  26.         {
  27.             if (rend == null)
  28.                 rend = GetComponent<Renderer> ();
  29.         }
  30.  
  31.         private void ValidateColors ()
  32.         {
  33.             foreach (var prop in colors)
  34.                 prop.OnValidte ();
  35.         }
  36.         private void ValidateFloats ()
  37.         {
  38.             foreach (var prop in floats)
  39.                 prop.OnValidte ();
  40.         }
  41.         private void ValidateTextures ()
  42.         {
  43.             foreach (var prop in textures)
  44.                 prop.OnValidte ();
  45.         }
  46.  
  47.         private void Update ()
  48.         {
  49.             if (!Application.isPlaying)
  50.             {
  51.                 UpdateFloats ();
  52.                 UpdateColors ();
  53.                 UpdateTextures ();
  54.             }
  55.         }
  56.  
  57.         private void UpdateColors ()
  58.         {
  59.             foreach (var prop in colors)
  60.                 prop.OnUpdate (rend);
  61.         }
  62.         private void UpdateFloats ()
  63.         {
  64.             foreach (var prop in floats)
  65.                 prop.OnUpdate (rend);
  66.         }
  67.         private void UpdateTextures ()
  68.         {
  69.             foreach (var prop in textures)
  70.                 prop.OnUpdate (rend);
  71.         }
  72.  
  73.         [ContextMenu ("Add Flaot")]
  74.         void AddFlaot ()
  75.         {
  76.             floats.Add (PropertyBlockInfo.CreateInstance<FloatProperty> ());
  77.         }
  78.  
  79.         [ContextMenu ("Add Color")]
  80.         void AddColor ()
  81.         {
  82.             colors.Add (PropertyBlockInfo.CreateInstance<ColorProperty> ());
  83.         }
  84.  
  85.         [ContextMenu ("Add Texture")]
  86.         void AddTexture ()
  87.         {
  88.             textures.Add (PropertyBlockInfo.CreateInstance<TextureProperty> ());
  89.         }
  90.     }
  91.     public class PropertyBlockInfo : ScriptableObject
  92.     {
  93.         protected MaterialPropertyBlock propBlock = null;
  94.         [HideInInspector]
  95.         [SerializeField] protected int namehash = 0;
  96.         [SerializeField] protected string Name;
  97.         public void OnValidte ()
  98.         {
  99.             ValidateHash ();
  100.             ValidateBlock ();
  101.         }
  102.         private void ValidateHash ()
  103.         {
  104.             if (namehash == 0)
  105.                 namehash = Shader.PropertyToID (Name);
  106.         }
  107.  
  108.         protected void ValidateBlock ()
  109.         {
  110.             if (propBlock == null)
  111.                 propBlock = new MaterialPropertyBlock ();
  112.         }
  113.  
  114.         public virtual void OnUpdate (Renderer Renderer)
  115.         {
  116.             ValidateBlock ();
  117.         }
  118.     }
  119.     public abstract class PropertyBlockInfo<T> : PropertyBlockInfo
  120.     {
  121.         [SerializeField] protected T value = default (T);
  122.     }
  123.  
  124.     [CreateAssetMenu (fileName = "FlaotProperty", menuName = "GameCore/PropertyBlockInfo/Flaot")]
  125.     public class FloatProperty : PropertyBlockInfo<float>
  126.     {
  127.         public override void OnUpdate (Renderer rend)
  128.         {
  129.             base.OnUpdate (rend);
  130.             rend.GetPropertyBlock (propBlock);
  131.             propBlock.SetFloat (namehash, value);
  132.             rend.SetPropertyBlock (propBlock);
  133.         }
  134.     }
  135.  
  136.     [CreateAssetMenu (fileName = "ColorProperty", menuName = "GameCore/PropertyBlockInfo/Color")]
  137.     public class ColorProperty : PropertyBlockInfo<Color>
  138.     {
  139.         public override void OnUpdate (Renderer rend)
  140.         {
  141.             base.OnUpdate (rend);
  142.             rend.GetPropertyBlock (propBlock);
  143.             propBlock.SetColor (namehash, value);
  144.             rend.SetPropertyBlock (propBlock);
  145.         }
  146.     }
  147.  
  148.     [CreateAssetMenu (fileName = "TextureProperty", menuName = "GameCore/PropertyBlockInfo/Texture")]
  149.     public class TextureProperty : PropertyBlockInfo<Texture>
  150.     {
  151.         public override void OnUpdate (Renderer rend)
  152.         {
  153.             base.OnUpdate (rend);
  154.             rend.GetPropertyBlock (propBlock);
  155.             propBlock.SetTexture (namehash, value);
  156.             rend.SetPropertyBlock (propBlock);
  157.         }
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement