using UnityEngine; using UnityEngine.UI; namespace GeometryTD.UI { public class TowerRepoItem : RepoItem { private static readonly Vector2 DefaultTowerDragGhostSize = new Vector2(64f, 64f); public new TowerRepoItemContext Context => base.Context as TowerRepoItemContext; public void OnInit(TowerRepoItemContext context) { base.OnInit(context); } protected override bool CreateDragGhost() { DestroyDragGhost(); if (DragRoot == null) { return false; } TowerIconArea towerIconArea = IconArea as TowerIconArea; if (towerIconArea == null) { return base.CreateDragGhost(); } _dragGhostObject = new GameObject("TowerRepoItemDragGhost", typeof(RectTransform), typeof(CanvasGroup)); _dragGhostObject.layer = gameObject.layer; _dragGhostRect = _dragGhostObject.GetComponent(); _dragGhostRect.SetParent(DragRoot, false); _dragGhostRect.anchorMin = new Vector2(0.5f, 0.5f); _dragGhostRect.anchorMax = new Vector2(0.5f, 0.5f); _dragGhostRect.pivot = new Vector2(0.5f, 0.5f); Vector2 iconSize = IconArea != null ? IconArea.CurrentIconSize : Vector2.zero; if (iconSize.x <= 0f || iconSize.y <= 0f) { iconSize = BackgroundImage != null ? BackgroundImage.rectTransform.rect.size : DefaultTowerDragGhostSize; } if (iconSize.x <= 0f || iconSize.y <= 0f) { iconSize = DefaultTowerDragGhostSize; } _dragGhostRect.sizeDelta = iconSize; bool hasAnyLayer = false; hasAnyLayer |= TryCreateLayer("Base", towerIconArea.BaseIconSprite, towerIconArea.BaseIconColor, towerIconArea.BaseIconMaterial, _dragGhostRect, 0); hasAnyLayer |= TryCreateLayer("Bearing", towerIconArea.BearingIconSprite, towerIconArea.BearingIconColor, towerIconArea.BearingIconMaterial, _dragGhostRect, 1); hasAnyLayer |= TryCreateLayer("Muzzle", towerIconArea.MuzzleIconSprite, towerIconArea.MuzzleIconColor, towerIconArea.MuzzleIconMaterial, _dragGhostRect, 2); if (!hasAnyLayer) { DestroyDragGhost(); return base.CreateDragGhost(); } CanvasGroup ghostCanvasGroup = _dragGhostObject.GetComponent(); ghostCanvasGroup.blocksRaycasts = false; ghostCanvasGroup.interactable = false; _dragGhostObject.transform.SetAsLastSibling(); return true; } private static bool TryCreateLayer( string layerName, Sprite sprite, Color color, Material material, RectTransform parent, int siblingIndex) { if (parent == null || sprite == null) { return false; } GameObject layerObject = new GameObject($"TowerGhost{layerName}", typeof(RectTransform), typeof(Image)); layerObject.layer = parent.gameObject.layer; RectTransform layerRect = layerObject.GetComponent(); layerRect.SetParent(parent, false); layerRect.anchorMin = Vector2.zero; layerRect.anchorMax = Vector2.one; layerRect.offsetMin = Vector2.zero; layerRect.offsetMax = Vector2.zero; layerRect.SetSiblingIndex(siblingIndex); Image layerImage = layerObject.GetComponent(); layerImage.raycastTarget = false; layerImage.sprite = sprite; layerImage.color = color; layerImage.material = material; layerImage.preserveAspect = true; return true; } } }