PJ/Assets/scripts/dotfs_scripts/UI_QuestBoard.cs

123 lines
No EOL
4.1 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class UI_QuestBoard : UI_WindowBase
{
[Header("Quest Board Specific")]
[SerializeField] private GameObject questPrefab;
[SerializeField] private Transform container;
private List<UI_QuestItem> _instantiatedItems = new List<UI_QuestItem>();
protected override void Awake()
{
base.Awake();
// Проверка ссылок в инспекторе
if (questPrefab == null) Debug.LogError($"[UI_QuestBoard] {gameObject.name}: Quest Prefab is MISSING!", this);
if (container == null) Debug.LogError($"[UI_QuestBoard] {gameObject.name}: Container Transform is MISSING!", this);
}
private void OnEnable()
{
MANAGER_RemoteData.OnDataLoaded += RefreshBoard;
MANAGER_GameEvents.OnUIUpdateNeeded += UpdateVisuals;
// Проверяем данные при включении
if (MANAGER_WindowsBase.Instance != null && MANAGER_WindowsBase.Instance.RemoteData.currentData != null)
{
Debug.Log("[UI_QuestBoard] Data already exists on Enable, refreshing...");
RefreshBoard();
}
}
private void OnDisable()
{
MANAGER_RemoteData.OnDataLoaded -= RefreshBoard;
MANAGER_GameEvents.OnUIUpdateNeeded -= UpdateVisuals;
}
public override void OpenWindow()
{
Debug.Log("[UI_QuestBoard] OpenWindow called.");
base.OpenWindow();
RefreshBoard();
}
private void RefreshBoard()
{
Debug.Log("[UI_QuestBoard] Refreshing Board...");
// 1. Очистка
int countBefore = _instantiatedItems.Count;
foreach (var item in _instantiatedItems)
{
if (item != null) Destroy(item.gameObject);
}
_instantiatedItems.Clear();
Debug.Log($"[UI_QuestBoard] Cleared {countBefore} old items.");
// 2. Проверка менеджеров
if (MANAGER_WindowsBase.Instance == null)
{
Debug.LogError("[UI_QuestBoard] MANAGER_WindowsBase.Instance is NULL!");
return;
}
var data = MANAGER_WindowsBase.Instance.RemoteData.currentData;
if (data == null)
{
Debug.LogWarning("[UI_QuestBoard] currentData is NULL. Waiting for data...");
return;
}
var quests = data.quests;
if (quests == null || quests.Count == 0)
{
Debug.LogWarning("[UI_QuestBoard] Quests list is NULL or EMPTY in data.");
return;
}
Debug.Log($"[UI_QuestBoard] Found {quests.Count} quests in data. Starting Instantiation...");
// 3. Спавн
foreach (var questData in quests)
{
if (questData == null)
{
Debug.LogWarning("[UI_QuestBoard] Encountered null questData in list.");
continue;
}
GameObject go = Instantiate(questPrefab, container);
// Проверка на корректность RectTransform (частая проблема UI)
if (go.transform.localScale != Vector3.one) go.transform.localScale = Vector3.one;
UI_QuestItem script = go.GetComponent<UI_QuestItem>();
if (script != null)
{
script.Setup(questData);
_instantiatedItems.Add(script);
Debug.Log($"[UI_QuestBoard] Successfully spawned quest: {questData.title}");
}
else
{
Debug.LogError($"[UI_QuestBoard] Prefab '{go.name}' is missing UI_QuestItem component!", go);
}
}
// 4. Форсируем обновление UI
LayoutRebuilder.ForceRebuildLayoutImmediate(container.GetComponent<RectTransform>());
Debug.Log("[UI_QuestBoard] Refresh Complete.");
}
private void UpdateVisuals()
{
Debug.Log("[UI_QuestBoard] Updating Visuals for all items...");
foreach (var item in _instantiatedItems)
{
if (item != null) item.UpdateUI();
}
}
}