using System.Linq; using UnityEngine; public class GameObjectPlacementButton : UnitPlacementButton { [SerializeField] private GameObject _prefab; protected override bool CanPlace() { var isBuilding = _prefab.GetComponent(); var defaultPopCost = GlobalConfig.Instance.Current.populationCostPerUnit; var hasEnoughPopulation = isBuilding || ResourceManager.Instance.EnoughPopulationFor(defaultPopCost); return ResourceManager.Instance.EnoughFor(_unitCardInformation.Rock, _unitCardInformation.Wood, _unitCardInformation.Food) && hasEnoughPopulation && base.CanPlace(); } protected override DraggablePlaceholder Place() { var instance = Instantiate(_prefab); //we need to fetch the detection size before stripping the object var detectComp = _prefab.GetComponentInChildren(); Rect detectionRect = default; if(detectComp) detectionRect = _prefab.GetComponentInChildren().DetectionRectangle; //strip the object foreach (var r_body in instance.transform.GetAllComponents()) Destroy(r_body); foreach (var coll in instance.transform.GetAllComponents()) Destroy(coll); foreach (var script in instance.transform.GetAllComponents()) Destroy(script); 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.color; color.a = 0.6f; rend.color = color; rend.material = OutlineMaterial; placeholder.OutlineRenderers.Add(rend); } CreateRange(placeholder, detectionRect); return placeholder; } void CreateRange(DraggablePlaceholder placeholder, Rect detectionRect) { if (detectionRect == default) return; var detection = new GameObject("Detection"); var rend = detection.AddComponent(); rend.sprite = _detectionRangeSprite; rend.sortingLayerName = "Character"; rend.sortingOrder = 1; rend.color = new Color(1, 1, 1, 0.2f); detection.transform.SetParent(placeholder.transform); detection.transform.localPosition = detectionRect.position; detection.transform.localScale = detectionRect.size; } }