98 lines
3.1 KiB
C#
98 lines
3.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AssetUpdate
|
|
{
|
|
public class LiteConfirmWin : LiteWindow
|
|
{
|
|
private const string _path = "Prefab/LiteConfirmWin";
|
|
private LiteConfirmButtonData[] _buttonData;
|
|
private RectTransform _buttonRoot;
|
|
private Button[] _buttons;
|
|
private Text _content;
|
|
private Text _title;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
_title = panelRoot.Find("TitleBar/Text").GetComponent<Text>();
|
|
_content = panelRoot.Find("Text").GetComponent<Text>();
|
|
var buttonRoot = panelRoot.Find("Buttons");
|
|
var count = buttonRoot.childCount;
|
|
_buttons = new Button[count];
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
_buttons[i] = buttonRoot.GetChild(i).GetComponent<Button>();
|
|
_buttons[i].gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void Init(string title, string content, params LiteConfirmButtonData[] buttons)
|
|
{
|
|
_title.text = title;
|
|
_content.text = content;
|
|
_buttonData = buttons;
|
|
for (var i = 0; i < _buttons.Length; i++)
|
|
if (i < buttons.Length)
|
|
{
|
|
var index = i;
|
|
_buttons[i].onClick.AddListener(() => OnClick(index));
|
|
_buttons[i].gameObject.SetActive(true);
|
|
var text = _buttons[i].GetComponentInChildren<Text>();
|
|
text.text = buttons[i].text;
|
|
}
|
|
else
|
|
{
|
|
_buttons[i].gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void OnClick(int index)
|
|
{
|
|
Close();
|
|
if (index < _buttonData.Length)
|
|
{
|
|
var callback = _buttonData[index].callBack;
|
|
if (callback != null)
|
|
callback();
|
|
}
|
|
}
|
|
|
|
public static void Open(string title, string content, params LiteConfirmButtonData[] buttons)
|
|
{
|
|
var canvas = FindObjectOfType<Canvas>();
|
|
if (!canvas)
|
|
{
|
|
Debug.LogError("Cannot find canvas in current scene!");
|
|
}
|
|
else
|
|
{
|
|
var prefab = Resources.Load<GameObject>(_path);
|
|
if (!prefab)
|
|
Debug.LogError(string.Format("Cannot load window from {0}", _path));
|
|
{
|
|
var instance = Instantiate(prefab, canvas.transform);
|
|
var component = instance.AddComponent<LiteConfirmWin>();
|
|
component.Init(title, content, buttons);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class LiteConfirmButtonData
|
|
{
|
|
public readonly UnityAction callBack;
|
|
public readonly string text;
|
|
|
|
public LiteConfirmButtonData(string content)
|
|
{
|
|
text = content;
|
|
}
|
|
|
|
public LiteConfirmButtonData(string content, UnityAction callBack) : this(content)
|
|
{
|
|
this.callBack = callBack;
|
|
}
|
|
}
|
|
} |