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
This commit is contained in:
Felix Boucher 2023-06-15 12:30:41 -04:00
parent 5244bbcfbb
commit 6a3a405753
3 changed files with 22 additions and 11 deletions

View File

@ -4,6 +4,8 @@ using UnityEngine;
public abstract class DraggablePlaceholder : MonoBehaviour public abstract class DraggablePlaceholder : MonoBehaviour
{ {
protected const string OutlineColor = "_OutlineColor";
[SerializeField] [SerializeField]
protected Color _validColor = Color.green; protected Color _validColor = Color.green;
[SerializeField] [SerializeField]
@ -13,6 +15,12 @@ public abstract class DraggablePlaceholder : MonoBehaviour
protected Rect _lvlBoundsCache; protected Rect _lvlBoundsCache;
protected bool _isOnValidPosition; protected bool _isOnValidPosition;
private List<SpriteRenderer> _outlineRenderers = new List<SpriteRenderer>();
public List<SpriteRenderer> OutlineRenderers
{
get => _outlineRenderers;
}
/// <summary> /// <summary>
/// calculate level boundaries and finds main camera. /// calculate level boundaries and finds main camera.
/// </summary> /// </summary>
@ -79,10 +87,9 @@ public abstract class DraggablePlaceholder : MonoBehaviour
{ {
Color getColor() => _isOnValidPosition ? _validColor : _invalidColor; Color getColor() => _isOnValidPosition ? _validColor : _invalidColor;
foreach (var child in GetComponentsInChildren<SpriteRenderer>(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(); public abstract void Place();

View File

@ -1,4 +1,5 @@
using UnityEngine; using System.Linq;
using UnityEngine;
public class GameObjectPlacementButton : UnitPlacementButton public class GameObjectPlacementButton : UnitPlacementButton
{ {
@ -18,19 +19,22 @@ public class GameObjectPlacementButton : UnitPlacementButton
foreach (var coll in instance.transform.GetAllComponents<Collider2D>()) Destroy(coll); foreach (var coll in instance.transform.GetAllComponents<Collider2D>()) Destroy(coll);
foreach (var script in instance.transform.GetAllComponents<MonoBehaviour>()) Destroy(script); foreach (var script in instance.transform.GetAllComponents<MonoBehaviour>()) Destroy(script);
//assign outline material tou all renderers of the placeholder var placeholder = instance.AddComponent<ObjectPlaceholder>();
placeholder.Prefab = _prefab;
//assign outline material to all renderers of the placeholder
foreach (var rend in instance.transform.GetAllComponents<SpriteRenderer>()) foreach (var rend in instance.transform.GetAllComponents<SpriteRenderer>())
{ {
var color = (rend as SpriteRenderer).color;
color.a = 0.6f;
(rend as SpriteRenderer).color = color;
(rend as SpriteRenderer).material = _outlineMaterial; var color = rend.color;
color.a = 0.6f;
rend.color = color;
rend.material = _outlineMaterial;
placeholder.OutlineRenderers.Add(rend);
} }
var placeholder = instance.AddComponent<ObjectPlaceholder>();
placeholder.Prefab = _prefab;
CreateRange(placeholder, detectionRect); CreateRange(placeholder, detectionRect);
} }

View File

@ -7,7 +7,7 @@ using GatherAndDefend.LevelEditor;
public static class Extensions public static class Extensions
{ {
public static Component[] GetAllComponents<T>(this Component component) where T : Component public static T[] GetAllComponents<T>(this Component component) where T : Component
{ {
List<T> comps = new List<T>(); List<T> comps = new List<T>();
comps.AddRange(component.GetComponents<T>()); comps.AddRange(component.GetComponents<T>());