Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active May 17, 2017 08:06
Show Gist options
  • Save tsubaki/adf522126051dd227f36fe3830a67e8a to your computer and use it in GitHub Desktop.
Save tsubaki/adf522126051dd227f36fe3830a67e8a to your computer and use it in GitHub Desktop.
シェーダーを実行時に更新
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using System.Linq;
#endif
public class UpdateShader : MonoBehaviour {
[SerializeField] Material[] mats;
#if UNITY_EDITOR
void Awake () {
// 登録したマテリアルのシェーダーを更新
foreach (var mat in mats) {
mat.shader = Shader.Find (mat.shader.name);
}
}
void Reset ()
{
// シーン内のマテリアルを収集する
List<Material> matList = new List<Material> ();
matList.AddRange( GameObject.FindObjectsOfType<MeshRenderer> ().SelectMany( c => c.sharedMaterials) );
matList.AddRange (GameObject.FindObjectsOfType<SkinnedMeshRenderer> ().SelectMany (c => c.sharedMaterials));
mats = matList.Distinct().ToArray ();
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment