Last active
January 22, 2020 13:53
-
-
Save ogrew/f3a58868f9974e127aa8eed8d284f4e0 to your computer and use it in GitHub Desktop.
スクリプトからマテリアルの色(Properties)を変更する
This file contains 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.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; | |
} | |
} | |
} |
This file contains 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
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