Files
2023-04-12 12:18:04 +02:00

77 lines
1.9 KiB
C#

using NaughtyAttributes;
using UnityEngine;
namespace Station46.Scripts
{
/// <summary>
/// A general component for controlling the emission of an object.
/// </summary>
public class Emission : MonoBehaviour
{
protected static readonly int EmissionColorNameID = Shader.PropertyToID("_EmissionColor");
[Tooltip("The colour of the emission.")]
[ColorUsage(false, true)]
public Color color;
public bool startActive;
[BoxGroup("Internal")] [Required]
public MeshRenderer emissionRenderer;
/// <summary>
/// Whether the emission is on.
/// </summary>
internal bool active;
private bool _previousActive;
private Color _previousColor;
private Material _material;
protected virtual void Awake()
{
_material = emissionRenderer.material;
}
private void Start()
{
if (startActive)
{
active = true;
}
ChangedToggle();
ChangedColor();
}
private void Update()
{
if (_previousActive != active)
{
ChangedToggle();
_previousActive = active;
}
if (!_previousColor.Equals(color))
{
ChangedColor();
_previousColor = color;
}
}
protected virtual void ChangedToggle()
{
if (active)
{
_material.EnableKeyword("_EMISSION");
}
else
{
_material.DisableKeyword("_EMISSION");
}
}
protected virtual void ChangedColor()
{
_material.SetColor(EmissionColorNameID, color);
}
}
}