Add task icon, correct show logic, dlg ui feature

This commit is contained in:
HungDK
2025-12-17 11:07:50 +07:00
parent 2aed1dd794
commit fa7b374442
4 changed files with 240 additions and 27 deletions
+100 -22
View File
@@ -63,36 +63,114 @@ public class CECUIManager : MonoSingleton<CECUIManager>
}
/// <summary>
/// Lấy hoặc spawn UI mới nếu chưa có
/// Show UI by name of component ("DlgTask", "EC_InventoryUI")
/// </summary>
public T ShowUI<T>() where T : Component
/// <param name="componentName">name of component ("DlgTask", "EC_InventoryUI")</param>
public void ShowUI(string componentName)
{
var type = typeof(T);
if (string.IsNullOrEmpty(componentName) || canvasDlg == null)
{
if (canvasDlg == null) Debug.LogError("canvasDlg chưa được gán");
return;
}
// Nếu đã spawn rồi thì bật lại
if (_spawnedUIs.TryGetValue(type, out var uiGo))
var type = FindTypeByName(componentName);
if (TryShowCachedUI(type)) return;
if (FindUIByName(componentName, type)) return;
if (FindUIByType(type)) return;
Debug.LogWarning($"Không tìm thấy UI {componentName} đã spawn trong canvasDlg. Type found: {(type != null ? type.FullName : "null")}");
}
private System.Type FindTypeByName(string componentName)
{
string[] namespacePrefixes = {
"", // No namespace
"BrewMonster.Scripts.Task.UI.",
"BrewMonster.UI.",
"BrewMonster.Scripts.UI.",
"BrewMonster."
};
// Try with common namespace prefixes
foreach (var prefix in namespacePrefixes)
{
string fullTypeName = string.IsNullOrEmpty(prefix) ? componentName : prefix + componentName;
var type = System.Type.GetType(fullTypeName);
if (type != null) return type;
}
// Search in all assemblies
foreach (var assembly in System.AppDomain.CurrentDomain.GetAssemblies())
{
var type = assembly.GetType(componentName);
if (type != null) return type;
foreach (var prefix in namespacePrefixes)
{
if (string.IsNullOrEmpty(prefix)) continue;
type = assembly.GetType(prefix + componentName);
if (type != null) return type;
}
}
return null;
}
private bool TryShowCachedUI(System.Type type)
{
if (type != null && _spawnedUIs.TryGetValue(type, out var uiGo))
{
uiGo.SetActive(true);
return uiGo.GetComponent<T>();
return true;
}
// Tìm prefab phù hợp
var prefab = uiPrefabs.Find(p => p.GetComponent<T>() != null);
if (prefab == null)
{
Debug.LogError($"Không tìm thấy prefab chứa component {type.Name}");
return null;
}
// Spawn mới
var instance = Instantiate(prefab, uiRoot ? uiRoot : transform);
instance.name = $"{type.Name}_UI";
_spawnedUIs[type] = instance;
instance.SetActive(true);
return instance.GetComponent<T>();
return false;
}
private bool FindUIByName(string componentName, System.Type type)
{
for (int i = 0; i < canvasDlg.transform.childCount; i++)
{
var child = canvasDlg.transform.GetChild(i);
if (!child.name.Contains(componentName) && child.name != componentName) continue;
if (type != null)
{
var foundComponent = child.GetComponentInChildren(type, true);
if (foundComponent != null)
{
ActivateAndCacheUI(foundComponent.gameObject, type);
return true;
}
}
ActivateAndCacheUI(child.gameObject, type);
return true;
}
return false;
}
private bool FindUIByType(System.Type type)
{
if (type == null) return false;
var foundComponent = canvasDlg.GetComponentInChildren(type, true);
if (foundComponent != null)
{
ActivateAndCacheUI(foundComponent.gameObject, type);
return true;
}
return false;
}
private void ActivateAndCacheUI(GameObject uiGameObject, System.Type type)
{
uiGameObject.SetActive(true);
if (type != null) _spawnedUIs[type] = uiGameObject;
}
/// <summary>
/// Ẩn UI (disable thay vì destroy)
/// </summary>