From 6a3a40575325428c4b8d7e2476ae68981a10b6e0 Mon Sep 17 00:00:00 2001 From: Felix Boucher Date: Thu, 15 Jun 2023 12:30:41 -0400 Subject: [PATCH] cached and refactored a bit problem : spelling errors and lisibility issues with some parts of the code solution : - used GetAllComponents generic nature to eliminate the need for casting - cached sprite renderers in the Placeholder - put hardcoded string in a const variable --- .../Scripts/Drag&Drop/DraggablePlaceholder.cs | 13 ++++++++++--- .../Drag&Drop/GameObjectPlacementButton.cs | 18 +++++++++++------- Assets/Scripts/General/Extensions.cs | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Drag&Drop/DraggablePlaceholder.cs b/Assets/Scripts/Drag&Drop/DraggablePlaceholder.cs index 90ccfe0..7e610f7 100644 --- a/Assets/Scripts/Drag&Drop/DraggablePlaceholder.cs +++ b/Assets/Scripts/Drag&Drop/DraggablePlaceholder.cs @@ -4,6 +4,8 @@ using UnityEngine; public abstract class DraggablePlaceholder : MonoBehaviour { + protected const string OutlineColor = "_OutlineColor"; + [SerializeField] protected Color _validColor = Color.green; [SerializeField] @@ -12,6 +14,12 @@ public abstract class DraggablePlaceholder : MonoBehaviour protected Camera _mainCamCache; protected Rect _lvlBoundsCache; protected bool _isOnValidPosition; + + private List _outlineRenderers = new List(); + public List OutlineRenderers + { + get => _outlineRenderers; + } /// /// calculate level boundaries and finds main camera. @@ -79,10 +87,9 @@ public abstract class DraggablePlaceholder : MonoBehaviour { Color getColor() => _isOnValidPosition ? _validColor : _invalidColor; - foreach (var child in GetComponentsInChildren(true)) + foreach (var child in _outlineRenderers) { - if (!child.material.HasProperty("_OutlineColor")) continue; - child.material.SetColor("_OutlineColor", getColor()); + child.material.SetColor(OutlineColor, getColor()); } } public abstract void Place(); diff --git a/Assets/Scripts/Drag&Drop/GameObjectPlacementButton.cs b/Assets/Scripts/Drag&Drop/GameObjectPlacementButton.cs index a4c1d7c..56c4285 100644 --- a/Assets/Scripts/Drag&Drop/GameObjectPlacementButton.cs +++ b/Assets/Scripts/Drag&Drop/GameObjectPlacementButton.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System.Linq; +using UnityEngine; public class GameObjectPlacementButton : UnitPlacementButton { @@ -18,19 +19,22 @@ public class GameObjectPlacementButton : UnitPlacementButton foreach (var coll in instance.transform.GetAllComponents()) Destroy(coll); foreach (var script in instance.transform.GetAllComponents()) Destroy(script); - //assign outline material tou all renderers of the placeholder + var placeholder = instance.AddComponent(); + placeholder.Prefab = _prefab; + + //assign outline material to all renderers of the placeholder foreach (var rend in instance.transform.GetAllComponents()) { - var color = (rend as SpriteRenderer).color; + + var color = rend.color; color.a = 0.6f; - (rend as SpriteRenderer).color = color; + rend.color = color; - (rend as SpriteRenderer).material = _outlineMaterial; + rend.material = _outlineMaterial; + placeholder.OutlineRenderers.Add(rend); } - var placeholder = instance.AddComponent(); - placeholder.Prefab = _prefab; CreateRange(placeholder, detectionRect); } diff --git a/Assets/Scripts/General/Extensions.cs b/Assets/Scripts/General/Extensions.cs index 22d7cd2..c938c49 100644 --- a/Assets/Scripts/General/Extensions.cs +++ b/Assets/Scripts/General/Extensions.cs @@ -7,7 +7,7 @@ using GatherAndDefend.LevelEditor; public static class Extensions { - public static Component[] GetAllComponents(this Component component) where T : Component + public static T[] GetAllComponents(this Component component) where T : Component { List comps = new List(); comps.AddRange(component.GetComponents());