using System.Collections.Generic; using UnityEngine; namespace CryingSnow.FarmingIsland { public class SiloInfo : MonoBehaviour { [Tooltip("Offset for the display position relative to the silo.")] [SerializeField] private Vector3 displayOffset = new Vector3(0f, 7f, 0f); [Tooltip("Prefab for displaying crop information.")] [SerializeField] private CropInfo cropInfoPrefab; private List cropInfos = new List(); // List to track instantiated CropInfo components. private Camera mainCamera; // Reference to the main camera for screen-space conversion. private Vector3 displayPosition; // Position in the world where the UI should be displayed. void Awake() { // Cache the main camera reference and initially hide the UI. mainCamera = Camera.main; gameObject.SetActive(false); } void LateUpdate() { // Continuously update the UI's position to follow the designated world position. transform.position = mainCamera.WorldToScreenPoint(displayPosition); } /// /// Displays the crop information UI near the specified silo position. /// /// The world position of the silo. /// List of crops to display. /// Maximum capacity for the crops. public void Show(Vector3 siloPosition, List crops, int capacity) { displayPosition = siloPosition + displayOffset; // Adjust the position based on the offset. // Instantiate crop info UI elements for each crop in the list. foreach (var crop in crops) { var cropInfo = Instantiate(cropInfoPrefab, transform); cropInfo.UpdateInfo(crop, capacity); cropInfos.Add(cropInfo); } // Enable the UI after setting up all crop information. gameObject.SetActive(true); } /// /// Hides the crop information UI and clears all child elements. /// public void Hide() { // Destroy all child UI elements and clear the crop info list. foreach (Transform child in transform) Destroy(child.gameObject); cropInfos.Clear(); gameObject.SetActive(false); } /// /// Updates the displayed crop information based on the current crop list and capacity. /// /// Updated list of crops. /// Updated capacity for the crops. public void UpdateCropInfos(List crops, int capacity) { // Update each crop info UI element with the new data. for (int i = 0; i < cropInfos.Count; i++) { cropInfos[i].UpdateInfo(crops[i], capacity); } } } }