using UnityEngine;
using LuaInterface;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.UI;
namespace LuaFramework {
public class LuaBehaviour : View {
private string data = null;
private Dictionary<string, LuaFunction> buttons = new Dictionary<string, LuaFunction>();
protected void Awake() {
Util.CallMethod(name, "Awake", gameObject);
}
protected void Start() {
Util.CallMethod(name, "Start");
}
protected void OnClick() {
Util.CallMethod(name, "OnClick");
}
protected void OnClickEvent(GameObject go) {
Util.CallMethod(name, "OnClick", go);
}
/// <summary>
/// 添加单击事件
/// </summary>
public void AddClick(GameObject go, LuaFunction luafunc) {
if (go == null || luafunc == null) return;
buttons.Add(go.name, luafunc);
go.GetComponent<Button>().onClick.AddListener(
delegate() {
luafunc.Call(go);
}
);
}
/// <summary>
/// 给 Toggle 组件添加监听
/// </summary>
public void AddToggle(GameObject go, LuaFunction luafunc)
{
if (go == null || luafunc == null) return;
buttons.Add(go.name, luafunc);
go.GetComponent<Toggle>().onValueChanged.AddListener(
delegate (bool select) {
luafunc.Call(go, select);
}
);
}
// 给输入组件 (InputField) 添加结束编辑 (OnEndEdit) 监听
public static void AddInputFieldEndEditHandler(GameObject go, LuaFunction luafunc)
{
if (go == null || luafunc == null) return;
InputField input = go.GetComponent<InputField>();
if (input == null)
{
Debug.LogError(go.name + "找不到 InputField 组件");
return;
}
go.GetComponent<InputField>().onEndEdit.AddListener(
delegate (string text) {
luafunc.Call(text);
}
);
}
/// <summary>
/// 删除单击事件
/// </summary>
/// <param name="go"></param>
public void RemoveClick(GameObject go) {
if (go == null) return;
LuaFunction luafunc = null;
if (buttons.TryGetValue(go.name, out luafunc)) {
luafunc.Dispose();
luafunc = null;
buttons.Remove(go.name);
}
}
/// <summary>
/// 清除单击事件
/// </summary>
public void ClearClick() {
foreach (var de in buttons) {
if (de.Value != null) {
de.Value.Dispose();
}
}
buttons.Clear();
}
//-----------------------------------------------------------------
protected void OnDestroy() {
ClearClick();
#if ASYNC_MODE
string abName = name.ToLower().Replace("panel", "");
ResManager.UnloadAssetBundle(abName + AppConst.ExtName);
#endif
Util.ClearMemory();
Debug.Log("~" + name + "was destroy!");
}
}
}
来源: https://www.cnblogs.com/imteach/p/10960188.html