using System.Collections; using System.Collections.Generic; using UnityEngine.EventSystems; using UnityEngine; using System; using XLua; using Module.Log; public class LuaUIItemBase : UIItemBase //public class LuaUIItemBase : UIItemBase, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler,IPointerExitHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler { public delegate void voidObject(object obj); public delegate void voidLuaUIItemBase(LuaUIItemBase 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 luaOnPointerDown; private Action luaOnPointerUp; private Action luaOnPointerEnter; private Action luaOnPointerExit; private Action luaOnBeginDrag; private Action luaOnDrag; private Action luaOnEndDrag; private Action luaOnDrop; 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(luaScript + "." + valueName); } else { return null; } } public virtual object GetValue(string valueName) { if (scriptEnv != null) { return scriptEnv.Get(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(luaScript + ".Awake"); luaStart = scriptEnv.GetInPath(luaScript + ".Start"); luaUpdate = scriptEnv.GetInPath(luaScript + ".Update"); luaFixedUpdate = scriptEnv.GetInPath(luaScript + ".FixedUpdate"); luaOnEnable = scriptEnv.GetInPath(luaScript + ".OnEnable"); luaOnDisable = scriptEnv.GetInPath(luaScript + ".OnDisable"); luaOnDestroy = scriptEnv.GetInPath(luaScript + ".OnDestroy"); // UIItembase luaRefresh = scriptEnv.GetInPath(luaScript + ".Refresh"); luaRefreshWithTab = scriptEnv.GetInPath(luaScript + ".RefreshWithTab"); luaOnPointerClick = scriptEnv.GetInPath(luaScript + ".OnPointerClick"); luaOnItemClick = scriptEnv.GetInPath>(luaScript + ".OnItemClick"); luaShow = scriptEnv.GetInPath(luaScript + ".Show"); luaShowWithTab = scriptEnv.GetInPath(luaScript + ".ShowWithTab"); // UnityEngine.EventSystems luaOnPointerDown = scriptEnv.GetInPath>(luaScript + ".OnPointerDown"); luaOnPointerUp = scriptEnv.GetInPath>(luaScript + ".OnPointerUp"); luaOnPointerEnter = scriptEnv.GetInPath>(luaScript + ".OnPointerEnter"); luaOnPointerExit = scriptEnv.GetInPath>(luaScript + ".OnPointerExit"); luaOnBeginDrag = scriptEnv.GetInPath>(luaScript + ".OnBeginDrag"); luaOnDrag = scriptEnv.GetInPath>(luaScript + ".OnDrag"); luaOnEndDrag = scriptEnv.GetInPath>(luaScript + ".OnEndDrag"); luaOnDrop = scriptEnv.GetInPath>(luaScript + ".OnDrop"); } 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(); } } public 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; luaOnPointerDown = null; luaOnPointerUp = null; luaOnPointerEnter = null; luaOnPointerExit = null; luaOnBeginDrag = null; luaOnDrag = null; luaOnEndDrag = null; luaOnDrop = null; scriptEnv.Dispose(); } public virtual void CallScriptFuncAtPath(string funcName, params object[] funcParam) { var func = scriptEnv.GetInPath(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(); if (btnPlay != null) btnPlay.PlaySound(); if (luaOnItemClick != null) { luaOnItemClick(eventData); } } public override void OnItemClick() { if (_ClickEvent != null) { _ClickEvent(_InitInfo); } if (_PanelClickEvent != null) { _PanelClickEvent(this); } } #endregion #region UnityEngine.EventSystems public void OnPointerDown(PointerEventData eventData) { if(luaOnPointerDown != null) { luaOnPointerDown(eventData); } } public void OnPointerUp(PointerEventData eventData) { if (luaOnPointerUp != null) { luaOnPointerUp(eventData); } } public void OnPointerEnter(PointerEventData eventData) { if (luaOnPointerEnter != null) { luaOnPointerEnter(eventData); } } public void OnPointerExit(PointerEventData eventData) { if (luaOnPointerExit != null) { luaOnPointerExit(eventData); } } public void OnBeginDrag(PointerEventData eventData) { if (luaOnBeginDrag != null) { luaOnBeginDrag(eventData); } } public void OnDrag(PointerEventData eventData) { if (luaOnDrag != null) { luaOnDrag(eventData); } } public void OnEndDrag(PointerEventData eventData) { if (luaOnEndDrag != null) { luaOnEndDrag(eventData); } } public void OnDrop(PointerEventData eventData) { if (luaOnDrop != null) { luaOnDrop(eventData); } } #endregion }