Files
HauntedBloodlines/Assets/Scripts/Lights/ChangeLightColour.cs
2025-05-29 22:31:40 +03:00

126 lines
4.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeLightColour : MonoBehaviour
{
public Color _lightColour;
public Renderer[] _lampRenderer;
public Material _lampRendererMaterial;
public Renderer[] _lightRend;
public Material _lightRendererMaterial;
[Tooltip("If is left null then it will automatically try to find the lights in the children")]
public Light[] _light;
[Tooltip("Optional! If left null then if the light you changed the color of has a switch handle the emssive color will not be updated resulting in false colors when you interact with the light switch")]
public LightInteractableData lightInteractableData;
private float _lampIntensity;
// Array of indices for materials to replace in _lampRenderer
[SerializeField] private int[] _lampRendererMaterialIndices;
// Array of indices for materials to replace in _lightRend
[SerializeField] private int[] _lightRendererMaterialIndices;
private void Awake()
{
if (_light == null)
{
_light = transform.GetComponentsInChildren<Light>();
}
}
public void ChangeColour()
{
#region Material change
if (_lampRenderer != null)
{
foreach (Renderer lampRend in _lampRenderer)
{
lampRend.material = _lampRendererMaterial;
if (lightInteractableData != null)
{
lightInteractableData.UpdateLightEmissionAndExposureWeightOnRuntime();
}
}
}
if (_lightRend != null)
{
foreach (Renderer lightRend in _lightRend)
{
lightRend.material = _lightRendererMaterial;
if (lightInteractableData != null)
{
lightInteractableData.UpdateLightEmissionAndExposureWeightOnRuntime();
}
}
}
#endregion
foreach (Light light in _light)
{
light.color = _lightColour;
}
}
public void ChangeColorBasedOnIndices()
{
#region Material change
if (_lampRenderer != null && _lampRendererMaterial != null && _lampRendererMaterialIndices != null)
{
foreach (Renderer lampRend in _lampRenderer)
{
Material[] materials = lampRend.materials;
foreach (int index in _lampRendererMaterialIndices)
{
if (index >= 0 && index < materials.Length)
{
materials[index] = _lampRendererMaterial;
lampRend.materials = materials;
if (lightInteractableData != null)
{
lightInteractableData.UpdateLightEmissionAndExposureWeightOnRuntimeBasedOnIndices(index);
}
}
}
//lampRend.materials = materials;
//if (lightInteractableData != null)
//{
// lightInteractableData.UpdateLightEmissionAndExposureWeightOnRuntimeBasedOnIndices(index);
//}
}
}
if (_lightRend != null && _lightRendererMaterial != null && _lightRendererMaterialIndices != null)
{
foreach (Renderer lightRend in _lightRend)
{
Material[] materials = lightRend.materials;
foreach (int index in _lightRendererMaterialIndices)
{
if (index >= 0 && index < materials.Length)
{
materials[index] = _lightRendererMaterial;
lightRend.materials = materials;
if (lightInteractableData != null)
{
lightInteractableData.UpdateLightEmissionAndExposureWeightOnRuntimeBasedOnIndices(index);
}
}
}
//lightRend.materials = materials;
//if (lightInteractableData != null)
//{
// lightInteractableData.UpdateLightEmissionAndExposureWeightOnRuntimeBasedOnIndices(index);
//}
}
}
#endregion
foreach (Light light in _light)
{
light.color = _lightColour;
}
}
}