refactor modules
This commit is contained in:
71
Assets/Station46/Scripts/Emission.cs
Normal file
71
Assets/Station46/Scripts/Emission.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using NaughtyAttributes;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Station46.Runtime
|
||||
{
|
||||
/// <summary>
|
||||
/// A general component for controlling the emission of an object.
|
||||
/// </summary>
|
||||
public class Emission : MonoBehaviour
|
||||
{
|
||||
private static readonly int EmissionColorNameID = Shader.PropertyToID("_EmissionColor");
|
||||
|
||||
[Tooltip("The colour of the emission.")]
|
||||
[ColorUsage(false, true)]
|
||||
public Color color;
|
||||
[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;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_material = emissionRenderer.material;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
ChangedToggle();
|
||||
ChangedColor();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_previousActive != active)
|
||||
{
|
||||
ChangedToggle();
|
||||
_previousActive = active;
|
||||
}
|
||||
|
||||
if (!_previousColor.Equals(color))
|
||||
{
|
||||
ChangedColor();
|
||||
_previousColor = color;
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangedToggle()
|
||||
{
|
||||
if (active)
|
||||
{
|
||||
_material.EnableKeyword("_EMISSION");
|
||||
}
|
||||
else
|
||||
{
|
||||
_material.DisableKeyword("_EMISSION");
|
||||
}
|
||||
}
|
||||
|
||||
private void ChangedColor()
|
||||
{
|
||||
_material.SetColor(EmissionColorNameID, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user