using UnityEngine; using System.Collections.Generic; public class GlowObject : MonoBehaviour { public Color GlowColor; public float LerpFactor = 10; public Renderer[] Renderers { get; private set; } public Color CurrentColor { get { return _currentColor; } } private List _materials = new List(); private Color _currentColor; private Color _targetColor; protected virtual void Start() { Renderers = GetComponentsInChildren(); foreach (var renderer in Renderers) { _materials.AddRange(renderer.materials); } } protected void OnMouseEnter() { _targetColor = GlowColor; enabled = true; } protected void OnMouseExit() { _targetColor = Color.black; _targetColor.a = 0.0f; enabled = true; } /// /// Loop over all cached materials and update their color, disable self if we reach our target color. /// protected void Update() { _currentColor = Color.Lerp(_currentColor, _targetColor, Time.deltaTime * LerpFactor); for (int i = 0; i < _materials.Count; i++) { _materials[i].SetColor("_GlowColor", _currentColor); } if (_currentColor.Equals(_targetColor)) { enabled = false; } } }