This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
project-undercover/Project Undercover/Assets/Scripts/Glow/GlowObject.cs

65 lines
1.2 KiB
C#
Raw Normal View History

2017-10-03 04:15:13 -05:00
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<Material> _materials = new List<Material>();
private Color _currentColor;
private Color _targetColor;
2017-10-03 04:15:13 -05:00
protected virtual void Start()
2017-10-03 04:15:13 -05:00
{
Renderers = GetComponentsInChildren<Renderer>();
foreach (var renderer in Renderers)
{
_materials.AddRange(renderer.materials);
}
}
protected void OnMouseEnter()
2017-10-03 04:15:13 -05:00
{
_targetColor = GlowColor;
enabled = true;
}
protected void OnMouseExit()
2017-10-03 04:15:13 -05:00
{
_targetColor = Color.black;
_targetColor.a = 0.0f;
enabled = true;
}
/// <summary>
/// Loop over all cached materials and update their color, disable self if we reach our target color.
/// </summary>
protected void Update()
2017-10-03 04:15:13 -05:00
{
_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;
}
}
}