require 'Class' --添加依赖项 不然引用会报错
local EventManager = require "EventManager"
local EventType = require "EventType"
function Awake()
local a= string.format('PLAGame.Client.ClientBattle/MirrorObj/AnchorObj/10/',1)
print(a)
print("Lua Awake")
EventManager.AddListener(EventType.Test,function(arg) print("Test"..tostring(arg)) end)
end
function Start()
print("Lua Start")
EventManager.TriggerListener(EventType.Test,"6666")
local image = CS.Assets.LuaHelper.GetGameObject("Image")
image.transform.parent = CS.UnityEngine.GameObject.Find("Canvas").transform
image.transform.localPosition = CS.UnityEngine.Vector3.zero
local testee = {transform="111"}
print(testee.transform)
print(testee["transform"])
local testee1 = {["transform"]=111}
print(testee1.transform)
print(testee1["transform"])
--第六步超简单MVVM
--model
local ACLoadingModel = {
ImgPlayerBox_transform = {},
}
--view
local options = {
{path = 'ImgPlayerBox', components = {'RectTransform',}, binding = 'ImgPlayerBox_transform' },
}
--获取所有实际的脚本信息 path transform gameObject element[compName] binding
local elements = GetElement(options,image)
print(elements)
--开始绑定字段
for i=1,#elements do
local ele = elements[i].binding
ACLoadingModel[tostring(ele)] = elements[i]
end
print_table(elements)
print_table(ACLoadingModel)
--ViewModel 只需要得到Mode就可以进行操作
local data = ACLoadingModel
data.ImgPlayerBox_transform.transform.localScale = CS.UnityEngine.Vector3.one * 1
print(data.ImgPlayerBox_transform.transform.localScale)
end
function GetElement( options,go )
local elements = {}
for i=1,#options do
local option = options[i]
local path = option.path
local element = {}
element.path = path
element.transform = go.transform:Find(path)
if not element.transform then
print("can't find ui node:"..path)
else
element.gameObject = element.transform.gameObject
for index = 1 ,#option.components do
local compName = option.components[index]
if compName == "RectTransform" then
element[compName] = element.gameObject.transform:GetComponent(typeof(CS.UnityEngine[compName]))
else
element[compName] = element.gameObject.transform:GetComponent(typeof(CS.UnityEngine.UI[compName]))
end
end
element.binding = option.binding
elements[i] = element
end
end
return elements
end
function table_tostring(node)
-- to make output beautiful
local function tab(amt)
local str = ""
for i=1,amt do
str = str .. "\t"
end
return str
end
local cache, stack, output = {},{},{}
local depth = 1
local output_str = "{\n"
while true do
local size = 0
for k,v in pairs(node) do
size = size + 1
end
local cur_index = 1
for k,v in pairs(node) do
if (cache[node] == nil) or (cur_index >= cache[node]) then
if (string.find(output_str,"}",output_str:len())) then
output_str = output_str .. ",\n"
elseif not (string.find(output_str,"\n",output_str:len())) then
output_str = output_str .. "\n"
end
-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output,output_str)
output_str = ""
local key
if (type(k) == "number" or type(k) == "boolean") then
key = "["..tostring(k).."]"
else
key = "['"..tostring(k).."']"
end
if (type(v) == "number" or type(v) == "boolean") then
output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
elseif (type(v) == "table") then
output_str = output_str .. tab(depth) .. key .. " = {\n"
table.insert(stack,node)
table.insert(stack,v)
cache[node] = cur_index+1
break
else
output_str = output_str .. tab(depth) .. key .. " = '"..tostring(v).."'"
end
if (cur_index == size) then
output_str = output_str .. "\n" .. tab(depth-1) .. "}"
else
output_str = output_str .. ","
end
else
-- close the table
if (cur_index == size) then
output_str = output_str .. "\n" .. tab(depth-1) .. "}"
end
end
cur_index = cur_index + 1
end
if (size == 0) then
output_str = output_str .. "\n" .. tab(depth-1) .. "}"
end
if (#stack > 0) then
node = stack[#stack]
stack[#stack] = nil
depth = cache[node] == nil and depth + 1 or depth - 1
else
break
end
end
-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output,output_str)
output_str = table.concat(output)
return output_str
end
function print_table(node)
output_str = table_tostring(node)
print(output_str)
end
--长时间函数
function Update()
--print("Lua Update")
end
function OnDestroy()
print("Lua OnDestory")
end
function SceneLoaded(name)
print("Lua SceneLoaded "..name)
end
function SceneUnloaded(name)
print("Lua SceneUnloaded "..name)
end
function OnApplicationFocus(focus)
print("Lua OnApplicationFocus "..tostring(focus))
end
function OnApplicationPause(pause)
print("Lua OnApplicationPause "..tostring(pause))
end
function OnApplicationQuit()
print("Lua OnApplicationQuit ")
end
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XLua;
using UnityEngine;
using System.IO;
namespace Assets
{
public class LuaHelper
{
public static GameObject GetGameObject(string path)
{
#if UNITY_EDITOR
var resPath = Path.Combine("Assets/GameRes/Prefab", path);
if (!resPath.Contains(".prefab"))
{
resPath += ".prefab";
}
GameObject gameObject = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(resPath);
#else
GameObject gameObject = loadAssetFromAssetBundle<GameObject>(path);
#endif
if (gameObject == null)
{
Debug.LogErrorFormat("load res failed at path {0}", path);
}
return GameObject.Instantiate<GameObject>(gameObject);
}
}
}