Panel脚本
Panel的PromptPanel.lua和MessagePanel.lua 这两个脚本都是和UI 界面相关的,本身没有实际的代码逻辑,都是查找当前UI 面板的一些游戏物体或者组件,并且存储它们的引用。 [ 1.PromptPanel.lua 脚本:“主面板”的界面脚本.
local transform; –局部变量.
local gameObject; –局部变量.
PromptPanel = {}; –定义一个和当前脚本同名的表.
local this = PromptPanel; –把当前表存储到this局部变量中.
–启动事件–
function PromptPanel.Awake(obj) –在当前表中定义一个Awake方法,并且接受一个”游戏物体对象参数”.
–这里的obj对应的就是PromptPanel.prefab.
gameObject = obj; –将传递来的对象进行保存.
transform = obj.transform; –从对象中获取transform组件.
this.InitPanel(); --调用初始化面板方法.
warn("Awake lua--->>"..gameObject.name); --warn()类似于Debug.Log().
end
–初始化面板–
function PromptPanel.InitPanel()
this.btnOpen = transform:Find(“Open”).gameObject; –查找并持有”Open”游戏物体的对象引用.
this.gridParent = transform:Find(‘ScrollView/Grid’); –查找并持有”Grid”游戏物体的对象引用.
end
–单击事件–
function PromptPanel.OnDestroy()
warn(“OnDestroy—->>>”); –无意义,只是一个输出测试.
end
2.MessagePanel.lua 脚本:“弹出信息面板”的界面脚本
local transform;
local gameObject;
MessagePanel = {};
local this = MessagePanel;
–启动事件–
function MessagePanel.Awake(obj)
gameObject = obj;
transform = obj.transform;
this.InitPanel();
warn("Awake lua--->>"..gameObject.name);
end
–初始化面板–
function MessagePanel.InitPanel()
this.btnClose = transform:Find(“Button”).gameObject;
end
–单击事件–
function MessagePanel.OnDestroy()
warn(“OnDestroy—->>>”);
end
Ctrl脚本
MessageCtrl.lua和PromptCtrl.lua两个Ctrl 脚本是UI界面的逻辑处理脚本,该脚本主要用于实例化游戏物体,处理游戏物体本身的逻辑,并对游戏物体内的按钮绑定相关的事件处理方法,在Ctrl 脚本中会访问使用Panel 脚本中查找到的游戏物体引用。 1.MessageCtrl.lua 脚本:“弹出信息面板”的界面逻辑功能实现脚本.
MessageCtrl = {}; –定义一个和当前脚本同名的表.
local this = MessageCtrl; –把当前表存储到this局部变量中.
–定义需要使用到的变量用local修饰.
local message;
local transform;
local gameObject;
–构建函数–
function MessageCtrl.New()
warn(“MessageCtrl.New—>>”);
return this; –返回当前表.
end
function MessageCtrl.Awake()
warn(“MessageCtrl.Awake—>>”);
–调用PanelManager.cs脚本中的CreatePanel()方法,创建”面板游戏物体”.
–PanelManager.cs是一个”框架脚本”,panel相关的操作方法都被封装到这个脚本中.
–CreatePanel(AB资源的名字, 回调方法).
PanelManager:CreatePanel(‘Message’, this.OnCreate);
end
–启动事件–
function MessageCtrl.OnCreate(obj) –接收一个obj对象.这个obj就是CreatePanel创建出来的游戏物体.
gameObject = obj; –存储该obj对象.
local panel = gameObject:GetComponent('UIPanel'); --获取UIPanel组件并存储.
panel.depth = 10; --设置纵深--
message = gameObject:GetComponent('LuaBehaviour'); --获取LuaBehaviour组件并存储.
--调用message中的AddClick静态方法,给关闭按钮添加相应的处理事件方法.
--MessagePanel.btnClose就是我们在MessagePanel.lua中查找到的游戏物体.
message:AddClick(MessagePanel.btnClose, this.OnClick);
warn("Start lua--->>"..gameObject.name);
end
–单击事件–
function MessageCtrl.OnClick(go)
destroy(gameObject); –销毁当前游戏物体.
end
–关闭事件–
function MessageCtrl.Close()
PanelManager:ClosePanel(CtrlName.Message);
end
PanelManager.cs:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LuaInterface;
namespace SimpleFramework.Manager {
public class PanelManager : View {
private Transform parent;
Transform Parent {
get {
if (parent == null) {
GameObject go = GameObject.FindWithTag("GuiCamera");
if (go != null) parent = go.transform;
}
return parent;
}
}
/// <summary>
/// 创建面板.\[主要是通过名字加载AB资源\]
/// </summary>
/// <param name="type"></param>
public void CreatePanel(string name, LuaFunction func = null) {
//通过名字加载AssetBundle.
AssetBundle bundle = ResManager.LoadBundle(name);
//开启一个协程.
StartCoroutine(StartCreatePanel(name, bundle, func));
Debug.LogWarning("CreatePanel::>> " + name + " " + bundle);
}
/// <summary>
/// 协程方法,使用AB资源,名称,回调方法,完成面板的实例化.
/// </summary>
IEnumerator StartCreatePanel(string name, AssetBundle bundle, LuaFunction func = null) {
name += "Panel";
//加载AB资源中的"MessagePanel预制体资源".
GameObject prefab = Util.LoadAsset(bundle, name);
//协程等待.
yield return new WaitForEndOfFrame();
//安全校验判断.
if (Parent.Find(name) != null || prefab == null) {
yield break;
}
//实例化游戏物体(UI面板).
GameObject go = Instantiate(prefab) as GameObject;
//设置该面板相关的属性.
go.name = name;
go.layer = LayerMask.NameToLayer("Default");
go.transform.parent = Parent;
go.transform.localScale = Vector3.one;
go.transform.localPosition = Vector3.zero;
//协程等待.
yield return new WaitForEndOfFrame();
//给当前的游戏物体添加一个组件"LuaBehaviour".
go.AddComponent<LuaBehaviour>().OnInit(bundle);
//调用回调方法.
if (func != null) func.Call(go);
Debug.Log("StartCreatePanel------>>>>" + name);
}
}
}
2.PromptCtrl.lua 脚本:“主面板”的界面逻辑功能实现脚本
require “Common/define” –require引入需要使用到的lua文件.
require “3rd/pblua/login_pb”
require “3rd/pbc/protobuf”
local sproto = require “3rd/sproto/sproto”
local core = require “sproto.core”
local print_r = require “3rd/sproto/print_r”
PromptCtrl = {};
local this = PromptCtrl;
local panel;
local prompt;
local transform;
local gameObject;
–构建函数–
function PromptCtrl.New()
warn(“PromptCtrl.New—>>”);
return this;
end
function PromptCtrl.Awake()
warn(“PromptCtrl.Awake—>>”);
PanelManager:CreatePanel(‘Prompt’, this.OnCreate);
end
–启动事件–
function PromptCtrl.OnCreate(obj)
gameObject = obj;
transform = obj.transform;
panel = transform:GetComponent('UIPanel');
prompt = transform:GetComponent('LuaBehaviour');
warn("Start lua--->>"..gameObject.name);
this.InitPanel(); --初始化面板--
prompt:AddClick(PromptPanel.btnOpen, this.OnClick);
end
–初始化面板–
function PromptCtrl.InitPanel()
panel.depth = 1; –设置纵深–
local parent = PromptPanel.gridParent;
local itemPrefab = prompt:GetGameObject(‘PromptItem’);
for i = 1, 100 do
local go = newobject(itemPrefab);
go.name = tostring(i);
go.transform.parent = parent;
go.transform.localScale = Vector3.one;
go.transform.localPosition = Vector3.zero;
prompt:AddClick(go, this.OnItemClick);
local goo = go.transform:Find('Label');
goo:GetComponent('UILabel').text = i;
end
local grid = parent:GetComponent('UIGrid');
grid:Reposition();
grid.repositionNow = true;
parent:GetComponent('WrapGrid'):InitGrid();
end
–滚动项单击事件–
function PromptCtrl.OnItemClick(go)
log(go.name);
end
–单击事件–
function PromptCtrl.OnClick(go)
if TestProtoType == ProtocalType.BINARY then
this.TestSendBinary();
end
if TestProtoType == ProtocalType.PB_LUA then
this.TestSendPblua();
end
if TestProtoType == ProtocalType.PBC then
this.TestSendPbc();
end
if TestProtoType == ProtocalType.SPROTO then
this.TestSendSproto();
end
warn(“OnClick—->>>”..go.name);
end