Skip to content

Instantly share code, notes, and snippets.

@ruccho
Last active October 25, 2020 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruccho/49f89c4dbc5c5e262eabcdeb7d5435f7 to your computer and use it in GitHub Desktop.
Save ruccho/49f89c4dbc5c5e262eabcdeb7d5435f7 to your computer and use it in GitHub Desktop.
uGUI ImageとかのマテリアルパラメータをAnimatorで制御したいときにSerializeFieldに露出するのに便利なクラス。本体 (MaterialExposition.cs) と使用サンプル (MaterialExpositionSample.cs)
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);
}
}
}
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