1. ExpBar를 UIBar 로 변경해서 UIButton과 같이 여러곳에서 공용으로 사용할 수 있게 변경 2. EventBus 를 만들어서 Button같이 자기 스스로 동작을 시작할 수 있는 오브젝트들 말고 수동적인 오브젝트들이 값을 받는 방식을 관리하게 설정 - PlayerController, UIManager, UIBar, EventBus를 연결함 3. Events 아래에 Player의 이벤트에 연관된 코드를 PlayerEvents 로 만들어서 관리함 - 경험치 전달 양식이 여기 존재 - 체력바나 마나바 같은 양식도 여기서 만들 예정
98 lines
2.8 KiB
C#
98 lines
2.8 KiB
C#
using System;
|
|
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 ();
|
|
|
|
private Dictionary<string, Action<string, float, float>> _registeredBarActions = 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 Bar 메서드
|
|
public void SubscribeToBarAction(string name, Action<string, float, float> action)
|
|
{
|
|
if (!_registeredBarActions.ContainsKey(name)) _registeredBarActions[name] = null;
|
|
_registeredBarActions[name] -= action;
|
|
_registeredBarActions[name] += action;
|
|
}
|
|
|
|
public Action<string, float, float> GetBarAction(string name)
|
|
{
|
|
_registeredBarActions.TryGetValue(name, out var action);
|
|
return action;
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
}
|