2023-08-22 19:08:45 +08:00
|
|
|
|
using System;
|
2023-11-21 13:31:06 +08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
using Framework;
|
|
|
|
|
|
using PhxhSDK;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using UnityEngine.Serialization;
|
|
|
|
|
|
using Object = System.Object;
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class UIWindow : MonoBehaviour
|
|
|
|
|
|
{
|
2023-11-21 13:31:06 +08:00
|
|
|
|
private const string DEFAULT_SHOW_ANIM = "show";
|
|
|
|
|
|
private const string DEFAULT_HIDE_ANIM = "hide";
|
2023-08-22 19:08:45 +08:00
|
|
|
|
private string mWindowName;
|
|
|
|
|
|
|
|
|
|
|
|
// 是否播放默认的打开对话框的声音
|
|
|
|
|
|
public bool isPlayDefaultOpenAudio = true;
|
|
|
|
|
|
// 是否播放默认的关闭对话框的声音
|
|
|
|
|
|
public bool isPlayDefaultCloseAudio = true;
|
|
|
|
|
|
|
|
|
|
|
|
public string WindowName
|
|
|
|
|
|
{
|
|
|
|
|
|
set { mWindowName = value; }
|
|
|
|
|
|
get { return mWindowName; }
|
|
|
|
|
|
}
|
2023-11-21 13:31:06 +08:00
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
[HideInInspector]
|
|
|
|
|
|
public UIWindowLayer uiWindowLayer;
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
[HideInInspector]
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public bool isOpen = false;
|
|
|
|
|
|
|
|
|
|
|
|
void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
OnAwake();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-16 15:32:40 +08:00
|
|
|
|
public void OpenWindow(object data)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
Show();
|
2023-11-16 15:32:40 +08:00
|
|
|
|
if (data == null)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-11-16 15:32:40 +08:00
|
|
|
|
OnOpenWindow();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
OnOpenWindow(data);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (isPlayDefaultOpenAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager3.Instance.PlaySound("");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-21 13:31:06 +08:00
|
|
|
|
public void CloseWindow(UIWindowCloseType closeType = UIWindowCloseType.OnlyHide)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2023-11-21 13:31:06 +08:00
|
|
|
|
OnCloseWindow();
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
if (isPlayDefaultCloseAudio && isOpen)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager3.Instance.PlaySound("");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-21 13:31:06 +08:00
|
|
|
|
if (closeType == UIWindowCloseType.HideAndDestroy)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
Destroy();
|
2023-11-21 13:31:06 +08:00
|
|
|
|
UIManager.Instance.RemoveWindow(mWindowName);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Hide();
|
2023-11-21 13:31:06 +08:00
|
|
|
|
if (closeType == UIWindowCloseType.HideAndPush)
|
|
|
|
|
|
{
|
|
|
|
|
|
UIManager.Instance.PushWindow(this);
|
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-27 16:47:42 +08:00
|
|
|
|
|
2023-11-21 13:31:06 +08:00
|
|
|
|
public virtual bool OnBack()
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
|
2023-11-21 13:31:06 +08:00
|
|
|
|
CloseWindow(UIWindowCloseType.OnlyHide);
|
|
|
|
|
|
return true;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ReloadWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
Show();
|
|
|
|
|
|
OnReloadWindow();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
public void ReloadWindow(object data)
|
|
|
|
|
|
{
|
|
|
|
|
|
Show();
|
|
|
|
|
|
OnReloadWindow(data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
private void Show()
|
|
|
|
|
|
{
|
|
|
|
|
|
isOpen = true;
|
|
|
|
|
|
if (gameObject == null)
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("UI已被销毁:{0}", WindowName);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
gameObject.SetActive(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Hide()
|
|
|
|
|
|
{
|
|
|
|
|
|
isOpen = false;
|
|
|
|
|
|
gameObject.SetActive(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Destroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
isOpen = false;
|
|
|
|
|
|
Destroy(gameObject);
|
2023-10-19 15:00:19 +08:00
|
|
|
|
AssetManager.Instance.Unload(string.Format(UIManager.UI_PREFAB_PATH, mWindowName));
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 打开界面时调用
|
|
|
|
|
|
/// </summary>
|
2023-10-19 15:00:19 +08:00
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
protected virtual void OnOpenWindow(object data) { }
|
2023-08-22 19:08:45 +08:00
|
|
|
|
protected virtual void OnOpenWindow() { }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 关闭界面时调用
|
|
|
|
|
|
/// </summary>
|
2023-11-21 13:31:06 +08:00
|
|
|
|
protected virtual void OnCloseWindow() { }
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新加载界面时调用
|
|
|
|
|
|
/// </summary>
|
2023-10-19 15:00:19 +08:00
|
|
|
|
protected virtual void OnReloadWindow(object data) { }
|
2023-08-22 19:08:45 +08:00
|
|
|
|
protected virtual void OnReloadWindow() { }
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 私有Awake方法,会在基类Awake执行后调用
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract void OnAwake();
|
|
|
|
|
|
|
|
|
|
|
|
public GameObject BindButton(string target, Action action, bool playAudio = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
GameObject obj = transform.Find(target)?.gameObject;
|
|
|
|
|
|
if (obj != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Button button = obj.GetComponent<Button>();
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.AddListener
|
|
|
|
|
|
(
|
|
|
|
|
|
delegate ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (playAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
|
|
|
|
|
|
}
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, target);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//增加一个可绑定对应变量的点击事件
|
|
|
|
|
|
//传入GameObject
|
|
|
|
|
|
public GameObject BindButton(GameObject obj, Action action, bool playAudio = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Button button = obj.GetComponent<Button>();
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.AddListener
|
|
|
|
|
|
(
|
|
|
|
|
|
delegate ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (playAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
|
|
|
|
|
|
}
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, obj.name);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//传入参数
|
|
|
|
|
|
public GameObject BindButton<T>(GameObject obj, Action<T> action, T parameter, bool playAudio = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (obj != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Button button = obj.GetComponent<Button>();
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.AddListener
|
|
|
|
|
|
(
|
|
|
|
|
|
delegate ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (playAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
|
|
|
|
|
|
}
|
|
|
|
|
|
action?.Invoke(parameter);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, obj.name);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//直接绑按钮
|
|
|
|
|
|
public GameObject BindButton(Button button, Action action, bool playAudio = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Button button = button.GetComponent<Button>();
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.AddListener
|
|
|
|
|
|
(
|
|
|
|
|
|
delegate ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (playAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
|
|
|
|
|
|
}
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
|
|
|
|
|
|
//DebugUtil.LogError($"未找到 {gameObject.name}/{button.name}");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return button.gameObject;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//可传入参数
|
|
|
|
|
|
public GameObject BindButton<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
//Button button = obj.GetComponent<Button>();
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.AddListener
|
|
|
|
|
|
(
|
|
|
|
|
|
delegate ()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (playAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (parameter == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("=== 传入参数为空! ===");
|
|
|
|
|
|
}
|
|
|
|
|
|
action?.Invoke(parameter);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return button.gameObject;
|
|
|
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
|
|
|
|
|
|
|
public void ClearBind(Button button)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.RemoveAllListeners();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ClearBind(GameObject go)
|
|
|
|
|
|
{
|
|
|
|
|
|
var button = go.GetComponent<Button>();
|
|
|
|
|
|
if (button != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
button.onClick.RemoveAllListeners();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
public GameObject FindObj(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
GameObject obj = transform.Find(path)?.gameObject;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
if (obj == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("{0}未找到 {1}/{2}", mWindowName, gameObject.name, path);
|
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
return obj;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T GetComponent<T>(string path) where T : Behaviour
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
var component = transform.Find(path)?.GetComponent<T>();
|
|
|
|
|
|
if (component == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("{0}未找到组件{1}: {2}", mWindowName, typeof(T).Name, path);
|
|
|
|
|
|
}
|
|
|
|
|
|
return component;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void HideWindow()
|
|
|
|
|
|
{
|
|
|
|
|
|
Hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-21 13:31:06 +08:00
|
|
|
|
public UniTask PlayAnimation(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return UniTask.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public UniTask AnimShow()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PlayAnimation(DEFAULT_SHOW_ANIM);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public UniTask AnimHide()
|
|
|
|
|
|
{
|
|
|
|
|
|
return PlayAnimation(DEFAULT_HIDE_ANIM);
|
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|