Files
JJBB/Assets/Project/Script/LuaScripts/UI/LuaUIItemSelect.cs
2024-08-23 15:49:34 +08:00

316 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using XLua;
using Module.Log;
using UnityEngine.EventSystems;
public class LuaUIItemSelect : UIItemSelect {
public delegate void voidObject(object obj);
public delegate void voidLuaUIItemSelect(LuaUIItemSelect obj);
public string luaScript;
public LuaParamGO[] injectionGos;
internal static float lastGCTime = 0;
internal const float GCInterval = 1; //1 second
private Action luaAwake;
private Action luaStart;
private Action luaUpdate;
private Action luaFixedUpdate;
private Action luaOnEnable;
private Action luaOnDisable;
private Action luaOnDestroy;
private Action luaRefresh;
private voidObject luaRefreshWithTab;
private Action luaOnPointerClick;
private Action luaOnItemClick;
private Action luaShow;
private voidObject luaShowWithTab;
private Action luaSelect;
private Action luaUnSelect;
private bool hasAwake = false;
protected LuaTable scriptEnv;
public virtual void SetValue(string paramName, object value)
{
if (scriptEnv != null)
{
scriptEnv.Set(paramName, value);
}
}
public virtual void SetValueAtPath(string paramName, object value)
{
CallScriptFuncAtPath("SetValue", paramName, value);
}
public virtual object GetValueAtPath(string valueName)
{
if (scriptEnv != null)
{
return scriptEnv.GetInPath<object>(luaScript + "." + valueName);
}
else
{
return null;
}
}
public virtual object GetValue(string valueName)
{
if (scriptEnv != null)
{
return scriptEnv.Get<object>(valueName);
}
else
{
return null;
}
}
public override void Init()
{
if (luaScript == null)
return;
if (scriptEnv == null)
{
scriptEnv = LuaMain.luaEnv.NewTable();
}
LuaTable meta = LuaMain.luaEnv.NewTable();
meta.Set("__index", LuaMain.luaEnv.Global);
scriptEnv.SetMetaTable(meta);
meta.Dispose();
scriptEnv.Set("self", this.gameObject);
foreach (var injection in injectionGos)
{
scriptEnv.Set(injection.name, injection.value);
}
TextAsset script = LoadAssetBundle.Instance.LoadScriptAsset(luaScript);
LuaMain.luaEnv.DoString(script.text, luaScript, scriptEnv);
// 生命周期
luaAwake = scriptEnv.GetInPath<Action>(luaScript + ".Awake");
luaStart = scriptEnv.GetInPath<Action>(luaScript + ".Start");
luaUpdate = scriptEnv.GetInPath<Action>(luaScript + ".Update");
luaFixedUpdate = scriptEnv.GetInPath<Action>(luaScript + ".FixedUpdate");
luaOnEnable = scriptEnv.GetInPath<Action>(luaScript + ".OnEnable");
luaOnDisable = scriptEnv.GetInPath<Action>(luaScript + ".OnDisable");
luaOnDestroy = scriptEnv.GetInPath<Action>(luaScript + ".OnDestroy");
// UIItembase
luaRefresh = scriptEnv.GetInPath<Action>(luaScript + ".Refresh");
luaRefreshWithTab = scriptEnv.GetInPath<voidObject>(luaScript + ".RefreshWithTab");
luaOnPointerClick = scriptEnv.GetInPath<Action>(luaScript + ".OnPointerClick");
luaOnItemClick = scriptEnv.GetInPath<Action>(luaScript + ".OnItemClick");
luaShow = scriptEnv.GetInPath<Action>(luaScript + ".Show");
luaShowWithTab = scriptEnv.GetInPath<voidObject>(luaScript + ".ShowWithTab");
// UIItemSelect
luaSelect = scriptEnv.GetInPath<Action>(luaScript + ".Select");
luaUnSelect = scriptEnv.GetInPath<Action>(luaScript + ".UnSelect");
}
public override void Awake()
{
if (hasAwake)
{
return;
}
hasAwake = true;
Init();
if (luaAwake != null)
{
luaAwake();
}
}
public virtual void Start()
{
if (luaStart != null)
{
luaStart();
}
}
public virtual void FixedUpdate()
{
if (luaFixedUpdate != null)
{
luaFixedUpdate();
}
}
protected virtual void Update()
{
if (luaUpdate != null)
{
luaUpdate();
}
}
protected virtual void OnEnable()
{
#if UNITY_EDITOR
// 编辑器模式下重新打开UI重新加载脚本便于调试
//Init();
//hasAwake = true;
//if (luaAwake != null)
//{
// luaAwake();
//}
#endif
if (luaOnEnable != null)
{
luaOnEnable();
}
}
protected virtual void OnDisable()
{
if (luaOnDisable != null)
{
luaOnDisable();
}
}
protected virtual void OnDestroy()
{
if (luaOnDestroy != null)
{
luaOnDestroy();
}
luaAwake = null;
luaStart = null;
luaUpdate = null;
luaFixedUpdate = null;
luaOnEnable = null;
luaOnDisable = null;
luaOnDestroy = null;
luaRefresh = null;
luaRefreshWithTab = null;
luaOnPointerClick = null;
luaOnItemClick = null;
luaShow = null;
luaShowWithTab = null;
luaSelect = null;
luaUnSelect = null;
scriptEnv.Dispose();
}
public virtual void CallScriptFuncAtPath(string funcName, params object[] funcParam)
{
var func = scriptEnv.GetInPath<LuaFunction>(luaScript + "." + funcName);
if (func == null)
{
LogModule.ErrorLog("there is no script func name:" + funcName);
return;
}
func.Call(funcParam);
}
#region ItemBase
public override void Show()
{
if (luaShow != null)
{
luaShow.Invoke();
}
}
public override void Show(Hashtable hash)
{
System.Object info = hash["InitObj"];
ShowWithTab(info);
}
public virtual void ShowWithTab(System.Object luaData)
{
if (luaShowWithTab != null)
{
_InitInfo = luaData;
luaShowWithTab.Invoke(luaData);
}
}
public override void Refresh()
{
if (luaRefresh != null)
{
luaRefresh.Invoke();
}
}
public virtual void Refresh(System.Object luaData)
{
if (luaRefreshWithTab != null)
{
luaRefreshWithTab.Invoke(luaData);
}
}
public override void OnPointerClick(PointerEventData eventData)
{
OnItemClick();
ButtonPlaySound btnPlay = GetComponent<ButtonPlaySound>();
if (btnPlay != null)
btnPlay.PlaySound();
}
public override void OnItemClick()
{
if (_ClickEvent != null)
{
_ClickEvent(_InitInfo);
}
if (_PanelClickEvent != null)
{
_PanelClickEvent(this);
}
if(luaOnItemClick != null)
{
luaOnItemClick.Invoke();
}
}
#endregion
#region ItemSelect
public override void Selected()
{
if(luaSelect != null)
{
luaSelect();
}
}
public override void UnSelected()
{
if(luaUnSelect != null)
{
luaUnSelect();
}
}
#endregion
}