97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using System.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
public class UIManager : IManager
|
|
{
|
|
private Canvas _canvas;
|
|
private List<string> _uiList = new ();
|
|
public List<string> UIList
|
|
{
|
|
get { return _uiList; }
|
|
set
|
|
{
|
|
_uiList = value ?? new List<string>();
|
|
foreach (var ui in _uiList)
|
|
{
|
|
string path = Define.MapPath(ui);
|
|
CreateObject(path, ui);
|
|
}
|
|
}
|
|
}
|
|
|
|
private readonly Dictionary<string, GameObject> _objects = new ();
|
|
private readonly Dictionary<string, GameObject> _instances = new();
|
|
private readonly Dictionary<string, bool> _switchObjects = new ();
|
|
|
|
public void Init()
|
|
{
|
|
_canvas = GameObject.Find("@Canvas").GetComponent<Canvas>();
|
|
}
|
|
|
|
private void CreateObject(string path, string prefabName)
|
|
{
|
|
GameObject createObj = Manager.Resource.Load<GameObject>(path);
|
|
if (createObj != null)
|
|
{
|
|
_objects.Add(prefabName, createObj);
|
|
_switchObjects.Add(prefabName, false);
|
|
}
|
|
}
|
|
|
|
public T SwitchOnObject<T>(string name, bool isOn) where T : MonoBehaviour
|
|
{
|
|
if (_switchObjects.ContainsKey(name) && (_switchObjects[name] != isOn))
|
|
_switchObjects[name] = isOn;
|
|
else return null;
|
|
|
|
GameObject instance;
|
|
if (!_instances.ContainsKey(name))
|
|
{
|
|
instance = Manager.Resource.Instantiate(_objects[name], _canvas.transform);
|
|
// instance.name = name;
|
|
_instances.Add(name, instance);
|
|
}
|
|
else instance = _instances[name];
|
|
|
|
if (_switchObjects.ContainsKey(name) && _switchObjects[name])
|
|
{
|
|
instance.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
instance.SetActive(false);
|
|
}
|
|
|
|
T component = instance.GetComponent<T>();
|
|
// if (action != null && component is UIButton button) button.Bind(action);
|
|
|
|
// if (component is UIButton button) Manager.Input.Buttons.Add(name, button);
|
|
|
|
return component;
|
|
}
|
|
|
|
#region UI Object 특화 메서드
|
|
|
|
#region ExpBar 메서드
|
|
public void ExclusiveExpBar()
|
|
{
|
|
PlayerController player = GameObject.FindObjectOfType<PlayerController>();
|
|
if (player != null)
|
|
{
|
|
player.OnExpChanged -= UpdateExpBar;
|
|
player.OnExpChanged += UpdateExpBar;
|
|
}
|
|
}
|
|
|
|
private void UpdateExpBar(int level, float currentExp, float maxExp)
|
|
{
|
|
ExpBar expBar = _instances["ExpBar"].GetComponent<ExpBar>();
|
|
expBar.UpdateValue(level, currentExp, maxExp);
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
}
|