Files
AppleHillsProduction/Assets/Scripts/Interactions/GlowOutline.cs
2025-12-13 18:16:49 +01:00

111 lines
2.8 KiB
C#

using AppleHills;
using System.Collections.Generic;
using UnityEngine;
using Core.Lifecycle;
using JetBrains.Annotations;
public class GlowOutline : ManagedBehaviour
{
private Color outlineColor;
private Transform[] childrenTransforms;
private SpriteRenderer[] childrenSprites;
private List<GlowOutlineData> outlineColors;
public SpriteRenderer itemSprite;
public float thiccness;
public GlowOutlineData.InteractionType interactionType;
public bool animatedSprite;
public Material outlineMaterial;
internal override void OnManagedStart()
{
SetupOutline();
}
private void SetupOutline()
{
// Get references to item sprite, own sprite and the outline settings from Interaction Settings
outlineColors = SettingsAccess.GetInteractionOutlineColors();
// Get the transforms and spriterenderers of children
childrenTransforms = GetComponentsInChildren<Transform>();
childrenSprites = GetComponentsInChildren<SpriteRenderer>();
// Set the color to use in this outline from the colors set in Interaction Settings
foreach (GlowOutlineData data in outlineColors)
{
if (data.interaction == interactionType)
{
outlineColor = data.outlineColour;
}
}
// Set the scale and sprite of each child. Skip first child because that's the outline object.
childrenTransforms[1].localPosition = new Vector3(1, 0, 0) * thiccness;
childrenTransforms[2].localPosition = new Vector3(-1, 0, 0) * thiccness;
childrenTransforms[3].localPosition = new Vector3(0, 1, 0) * thiccness;
childrenTransforms[4].localPosition = new Vector3(0, -1, 0) * thiccness;
foreach (SpriteRenderer childSprite in childrenSprites)
{
if (itemSprite.sprite != null)
{
childSprite.sprite = itemSprite.sprite;
childSprite.material = outlineMaterial;
}
if (childSprite.sharedMaterial != null)
{
childSprite.sharedMaterial.color = outlineColor;
}
if (itemSprite == null)
{
Debug.Log($"Outline {name} is missing an item sprite!");
}
}
}
public void Update()
{
if (animatedSprite)
{
foreach (SpriteRenderer childSprite in childrenSprites)
{
if (itemSprite.sprite != null)
{
childSprite.sprite = itemSprite.sprite;
}
}
}
}
#if UNITY_EDITOR
// Update outline in editor
private void OnValidate()
{
SetupOutline();
}
#endif
}