Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using UnityEngine;
- [ExecuteInEditMode]
- public class SetMaterial : MonoBehaviour
- {
- public bool Recursive = true;
- public GameObject[] RecursiveWhitelist;
- [SerializeField]
- private bool _visible = true;
- public bool Visible
- {
- get { return _visible; }
- set
- {
- if (_visible != value)
- {
- ApplyVisibility(value);
- }
- }
- }
- [Space]
- [SerializeField]
- public bool OverrideMaterial = false;
- [SerializeField]
- private Material _material = null;
- public Material Material
- {
- get { return _material; }
- set
- {
- if (value != _material)
- {
- OverrideMaterial = true;
- ApplyMaterial(value);
- }
- }
- }
- [Space]
- [SerializeField]
- public bool OverrideColor = false;
- [SerializeField]
- private Color _color = Color.white;
- public Color Color
- {
- get { return _color; }
- set
- {
- if (value != _color)
- {
- OverrideColor = true;
- if (OverrideAlpha)
- {
- value = new Color(value.r, value.g, value.b, _alpha);
- }
- ApplyColor(value);
- }
- }
- }
- [Space]
- [SerializeField]
- public bool OverrideAlpha = false;
- [SerializeField]
- private float _alpha = 1f;
- public float Alpha {
- get { return _alpha; }
- set
- {
- float diff = value - _alpha;
- if (diff < -0.001f || diff > 0.001f)
- {
- OverrideAlpha = true;
- if (OverrideColor)
- {
- _alpha = value;
- _color = new Color(_color.r, _color.g, _color.b, _alpha);
- ApplyColor(_color);
- }
- else
- {
- ApplyAlpha(value);
- }
- }
- }
- }
- protected ICollection<MeshRenderer> GetRenderers()
- {
- if (Recursive)
- {
- if (RecursiveWhitelist != null && RecursiveWhitelist.Length > 0)
- {
- List<MeshRenderer> renderers = new List<MeshRenderer>();;
- foreach (GameObject gameObject in RecursiveWhitelist)
- {
- renderers.AddRange(gameObject.GetComponentsInChildren<MeshRenderer>());
- }
- return renderers;
- }
- return GetComponentsInChildren<MeshRenderer>();
- }
- else
- return new[] { GetComponent<MeshRenderer>() };
- }
- public void ApplyVisibility(bool visible)
- {
- _visible = visible;
- foreach (MeshRenderer mesh in GetRenderers())
- mesh.enabled = _visible;
- }
- public void ApplyMaterial(Material mat)
- {
- _material = mat;
- foreach (MeshRenderer mesh in GetRenderers())
- if(mesh.material != mat)
- mesh.material = mat;
- if (OverrideColor)
- {
- ApplyColor(_color);
- }
- else if (OverrideAlpha)
- {
- ApplyAlpha(_alpha);
- }
- }
- public void ApplyColor(Color color)
- {
- _color = color;
- foreach (MeshRenderer mesh in GetRenderers())
- mesh.material.color = color;
- }
- public void ApplyAlpha(float alpha)
- {
- _alpha = alpha;
- foreach (MeshRenderer mesh in GetRenderers())
- mesh.material.color = new Color(
- mesh.material.color.r,
- mesh.material.color.g,
- mesh.material.color.b,
- alpha);
- }
- private void ApplyAll()
- {
- if (OverrideMaterial)
- {
- ApplyMaterial(_material);
- }
- if (OverrideColor)
- {
- Color c = _color;
- if (OverrideAlpha)
- {
- c = new Color(_color.r, _color.g, _color.b, Alpha);
- }
- ApplyColor(c);
- }else if (OverrideAlpha)
- {
- ApplyAlpha(_alpha);
- }
- }
- void Start ()
- {
- ApplyAll();
- }
- void Update () {
- if (!Application.isPlaying)
- {
- ApplyAll();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement