using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.UI; public class LogicTrayUI : MonoBehaviour { [SerializeField] protected List list; [SerializeField] protected GameObject Prefab; [SerializeField] protected int insertIndex = -1; [SerializeField] private Transform content; public void Start() { UpdateDisplay(); } public void OnHoverStart(BagItem item, RectTransform rect) { insertIndex = content.childCount; UpdateDisplay(); } public void OnHoverEnd(BagItem item) { insertIndex = -1; UpdateDisplay(); } public void InsertLogicElement(LogicBlock item) { if (insertIndex >= 0 && insertIndex <= list.Count) { list.Insert(insertIndex, item); } UpdateDisplay(); } public void OnHoverUpdate(BagItem item, RectTransform rect) { foreach(Transform child in content) { RectTransform childRect = child as RectTransform; if (child == null) continue; if (rect.GlobalRect().Overlaps(childRect.GlobalRect())) { if (insertIndex != child.GetSiblingIndex()) { insertIndex = child.GetSiblingIndex(); UpdateDisplay(); } break; } } } [ContextMenu("Update Display")] public void UpdateDisplay() { int destroyedCount = content.childCount; foreach (Transform child in content) Destroy(child.gameObject); foreach(LogicBlock element in list) { GameObject elementObject = Instantiate(Prefab, content); elementObject.GetComponentInChildren().text = element.name; } if (insertIndex != -1) { GameObject elementObject = Instantiate(Prefab, content); elementObject.GetComponentInChildren().text = "insert"; elementObject.GetComponentInChildren().color = new Color(0.5f, 0.5f, 0.5f, 0.25f); elementObject.transform.SetSiblingIndex(destroyedCount + insertIndex); } } }