Files
AppleHillsProduction/Assets/Scripts/Interactions/GlowOutline.cs
2025-12-11 22:36:08 +01:00

80 lines
2.2 KiB
C#

using AppleHills;
using System.Collections.Generic;
using UnityEngine;
using Core.Lifecycle;
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;
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>();
// childrenMaterials = GetComponentsInChildren<Material>();
// Set the color overlay of the sprites to colour our outline with the colors set in Interaction Settings
foreach (GlowOutlineData data in outlineColors)
{
if (data.interaction == interactionType)
{
outlineColor = data.outlineColour;
}
}
// Find the spriterenderer in parent gameobject, but ignore our own spriterenderer
// 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)
{
childSprite.sprite = itemSprite.sprite;
childSprite.sharedMaterial.color = outlineColor;
}
}
#if UNITY_EDITOR
// Update outline in editor
private void OnValidate()
{
SetupOutline();
}
#endif
}