Skip to content

Instantly share code, notes, and snippets.

@ogrew
Last active January 22, 2020 13:53
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 ogrew/f3a58868f9974e127aa8eed8d284f4e0 to your computer and use it in GitHub Desktop.
Save ogrew/f3a58868f9974e127aa8eed8d284f4e0 to your computer and use it in GitHub Desktop.
スクリプトからマテリアルの色(Properties)を変更する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Sample {
public class MatColorController : MonoBehaviour
{
public Material mat;
[Range(0,1)] public float opacity = 1.0f;
[Range(0,5)] public float coff;
private int id;
void Start()
{
id = Shader.PropertyToID("_Color");
}
void Update()
{
float r = Mathf.Sin(Time.time * coff);
float g = Mathf.Cos(Time.time * coff);
// -1 ~ 1 => 0 ~ 1 にRemap
r = r.Remap(-1,1,0,1);
g = g.Remap(-1,1,0,1);
Color Cd = new Color(r, g, 1.0f, opacity);
mat.SetColor("_Color", Cd);
}
}
public static class MathExtensiton {
public static float Remap(this float value, float from1, float to1, float from2, float to2)
{
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
}
}
Shader "Unlit/Sample Shader"
{
Properties
{
_Color("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags {
"RenderType"="Transparent"
"Queue" = "Transparent"
}
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
// ----------中略----------
fixed4 _Color;
// ----------中略----------
fixed4 frag (v2f i) : SV_Target
{
return _Color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment