Last active
October 25, 2020 18:11
-
-
Save ruccho/49f89c4dbc5c5e262eabcdeb7d5435f7 to your computer and use it in GitHub Desktop.
uGUI ImageとかのマテリアルパラメータをAnimatorで制御したいときにSerializeFieldに露出するのに便利なクラス。本体 (MaterialExposition.cs) と使用サンプル (MaterialExpositionSample.cs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
namespace Ruccho.Utilities | |
{ | |
public abstract class MaterialExposition<T> where T : IEquatable<T> | |
{ | |
protected Material target = default; | |
private T tempValue = default; | |
private int propertyKey = default; | |
public void Initialize(Material material, string property, ref T value) | |
{ | |
target = material; | |
propertyKey = Shader.PropertyToID(property); | |
T destValue = GetValue(propertyKey); | |
tempValue = destValue; | |
value = tempValue; | |
} | |
public void Update(ref T value) | |
{ | |
T destValue = GetValue(propertyKey); | |
if (!destValue.Equals(tempValue)) | |
{ | |
tempValue = destValue; | |
value = tempValue; | |
} | |
else if(!tempValue.Equals(value)) | |
{ | |
tempValue = value; | |
SetValue(propertyKey, tempValue); | |
} | |
} | |
protected abstract T GetValue(int key); | |
protected abstract void SetValue(int key, T value); | |
} | |
public class MaterialExpositionFloat : MaterialExposition<float> | |
{ | |
protected override float GetValue(int key) | |
{ | |
return target.GetFloat(key); | |
} | |
protected override void SetValue(int key, float value) | |
{ | |
target.SetFloat(key, value); | |
} | |
} | |
public class MaterialExpositionVector4 : MaterialExposition<Vector4> | |
{ | |
protected override Vector4 GetValue(int key) | |
{ | |
return target.GetVector(key); | |
} | |
protected override void SetValue(int key, Vector4 value) | |
{ | |
target.SetVector(key, value); | |
} | |
} | |
public class MaterialExpositionColor : MaterialExposition<Color> | |
{ | |
protected override Color GetValue(int key) | |
{ | |
return target.GetColor(key); | |
} | |
protected override void SetValue(int key, Color value) | |
{ | |
target.SetColor(key, value); | |
} | |
} | |
public class MaterialExpositionInt : MaterialExposition<int> | |
{ | |
protected override int GetValue(int key) | |
{ | |
return target.GetInt(key); | |
} | |
protected override void SetValue(int key, int value) | |
{ | |
target.SetInt(key, value); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Ruccho.Utilities; | |
using UnityEngine; | |
using UnityEngine.UI; | |
[ExecuteInEditMode] | |
public class MaterialExpositionSample : MonoBehaviour | |
{ | |
[SerializeField] private Image target = default; | |
private MaterialExpositionColor colorProperty = new MaterialExpositionColor(); | |
[SerializeField] private Color colorValue = default; | |
private Material material = default; | |
private void Reset() | |
{ | |
GetMaterial(); | |
} | |
private void GetMaterial() | |
{ | |
if (!target) | |
{ | |
target = GetComponent<Image>(); | |
material = null; | |
} | |
else if (material != target.material) | |
{ | |
material = null; | |
} | |
if (target && !material) | |
{ | |
material = target.material; | |
colorProperty.Initialize(material, "_Color", ref colorValue); | |
} | |
} | |
private void Start() | |
{ | |
} | |
private void Update() | |
{ | |
GetMaterial(); | |
colorProperty.Update(ref colorValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment