提交 d011ee4d 编写于 作者: Y Yong 提交者: chexiongsheng

完善示例程序 (#306)

* 添加运行场景,优化注释
说明文档中部分引用改为md文件,方便预览

* 精简不必要的代码
上级 b9aad836
......@@ -5,14 +5,18 @@
-- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
local speed = 10
local lightCpnt = nil
function start()
print("lua start...")
print("injected object", lightObject)
lightCpnt= lightObject:GetComponent(typeof(CS.UnityEngine.Light))
end
function update()
local r = CS.UnityEngine.Vector3.up * CS.UnityEngine.Time.deltaTime * speed
self.transform:Rotate(r)
lightCpnt.color = CS.UnityEngine.Color(CS.UnityEngine.Mathf.Sin(CS.UnityEngine.Time.time) / 2 + 0.5, 0, 0, 1)
end
function ondestroy()
......
......@@ -77,12 +77,10 @@ namespace XLuaTest
CustomValueTypeParam f3;
EnumParam f4;
DecimalParam f5;
ArrayAccess farr;
ArrayAccess farr;
Action flua;
IExchanger ie;
LuaFunction add;
[NonSerialized]
......@@ -169,6 +167,7 @@ namespace XLuaTest
luaenv.Global.Get("id", out f3);
luaenv.Global.Get("id", out f4);
luaenv.Global.Get("id", out f5);
luaenv.Global.Get("array_exchange", out farr);
luaenv.Global.Get("lua_access_csharp", out flua);
luaenv.Global.Get("exchanger", out ie);
......@@ -189,17 +188,15 @@ namespace XLuaTest
{
// c# call lua function with value type but no gc (using delegate)
f1(1); // primitive type
Vector3 v3 = new Vector3(1, 2, 3); // vector3
f2(v3);
MyStruct mystruct = new MyStruct(5, 6); // custom complex value type
f3(mystruct);
f4(MyEnum.E1); //enum
decimal d = -32132143143100109.00010001010M;
decimal dr = f5(d);
System.Diagnostics.Debug.Assert(d == dr);
f2(new Vector3(1, 2, 3)); // vector3
MyStruct mystruct1 = new MyStruct(5, 6);
f3(mystruct1); // custom complex value type
f4(MyEnum.E1); //enum
decimal dec1 = -32132143143100109.00010001010M;
f5(dec1); //decimal
// using LuaFunction.Func<T1, T2, TResult>
System.Diagnostics.Debug.Assert(add.Func<int, int, int>(34, 56) == (34 + 56)); // LuaFunction.Func<T1, T2, TResult>
add.Func<int, int, int>(34, 56); // LuaFunction.Func<T1, T2, TResult>
// lua access c# value type array no gc
farr(a1); //primitive value type array
......@@ -218,25 +215,20 @@ namespace XLuaTest
luaenv.Global.Set("g_int", 456);
int i;
luaenv.Global.Get("g_int", out i);
System.Diagnostics.Debug.Assert(i == 456);
luaenv.Global.Set(123.0001, mystruct);
luaenv.Global.Set(123.0001, mystruct1);
MyStruct mystruct2;
luaenv.Global.Get(123.0001, out mystruct2);
System.Diagnostics.Debug.Assert(mystruct2.b == mystruct.b);
decimal dr2 = 0.0000001M;
luaenv.Global.Set((byte)12, d);
luaenv.Global.Get((byte)12, out dr2);
System.Diagnostics.Debug.Assert(d == dr2);
decimal dec2 = 0.0000001M;
luaenv.Global.Set((byte)12, dec1);
luaenv.Global.Get((byte)12, out dec2);
int gdata = luaenv.Global.Get<int>("GDATA");
luaenv.Global.SetInPath("GDATA", gdata + 1);
System.Diagnostics.Debug.Assert(luaenv.Global.Get<int>("GDATA") == gdata + 1);
int abc = luaenv.Global.GetInPath<int>("A.B.C");
luaenv.Global.SetInPath("A.B.C", abc + 1);
System.Diagnostics.Debug.Assert(luaenv.Global.GetInPath<int>("A.B.C") == abc + 1);
luaenv.Tick();
}
......
......@@ -25,22 +25,24 @@ public class MessageBox : MonoBehaviour{
alertPanel.SetParent(GameObject.Find("Canvas").transform);
alertPanel.localPosition = new Vector3(-6f, -6f, 0f);
}
alertPanel.Find("title").GetComponent<Text>().text = title;
alertPanel.Find("message").GetComponent<Text>().text = message;
alertPanel.gameObject.SetActive(true);
if (onFinished != null)
var button = alertPanel.Find("alertBtn").GetComponent<Button>();
UnityAction onclick = () =>
{
var button = alertPanel.Find("alertBtn").GetComponent<Button>();
UnityAction onclick = null;
onclick = () =>
if (onFinished != null)
{
onFinished();
alertPanel.gameObject.SetActive(false);
button.onClick.RemoveListener(onclick);
};
}
button.onClick.RemoveAllListeners();
button.onClick.AddListener(onclick);
}
alertPanel.gameObject.SetActive(false);
};
//防止消息框未关闭时多次被调用
button.onClick.RemoveAllListeners();
button.onClick.AddListener(onclick);
alertPanel.gameObject.SetActive(true);
}
public static void ShowConfirmBox(string message, string title, Action<bool> onFinished = null)
......@@ -53,39 +55,43 @@ public class MessageBox : MonoBehaviour{
confirmPanel.SetParent(GameObject.Find("Canvas").transform);
confirmPanel.localPosition = new Vector3(-8f, -18f, 0f);
}
confirmPanel.Find("confirmTitle").GetComponent<Text>().text = title;
confirmPanel.Find("conmessage").GetComponent<Text>().text = message;
confirmPanel.gameObject.SetActive(true);
if (onFinished != null)
var confirmBtn = confirmPanel.Find("confirmBtn").GetComponent<Button>();
var cancelBtn = confirmPanel.Find("cancelBtn").GetComponent<Button>();
Action cleanup = () =>
{
var confirmBtn = confirmPanel.Find("confirmBtn").GetComponent<Button>();
var cancelBtn = confirmPanel.Find("cancelBtn").GetComponent<Button>();
UnityAction onconfirm = null;
UnityAction oncancel = null;
Action cleanup = () =>
{
confirmBtn.onClick.RemoveListener(onconfirm);
cancelBtn.onClick.RemoveListener(oncancel);
confirmPanel.gameObject.SetActive(false);
};
confirmBtn.onClick.RemoveAllListeners();
cancelBtn.onClick.RemoveAllListeners();
confirmPanel.gameObject.SetActive(false);
};
onconfirm = () =>
UnityAction onconfirm = () =>
{
if (onFinished != null)
{
onFinished(true);
cleanup();
};
}
cleanup();
};
oncancel = () =>
UnityAction oncancel = () =>
{
if (onFinished != null)
{
onFinished(false);
cleanup();
};
confirmBtn.onClick.RemoveAllListeners();
cancelBtn.onClick.RemoveAllListeners();
confirmBtn.onClick.AddListener(onconfirm);
cancelBtn.onClick.AddListener(oncancel);
}
}
cleanup();
};
//防止消息框未关闭时多次被调用
confirmBtn.onClick.RemoveAllListeners();
confirmBtn.onClick.AddListener(onconfirm);
cancelBtn.onClick.RemoveAllListeners();
cancelBtn.onClick.AddListener(oncancel);
confirmPanel.gameObject.SetActive(true);
}
}
......
......@@ -16,15 +16,16 @@ end
local recharge = util.async_to_sync(async_recharge)
-------------------------async_recharge end----------------------------
local buy = function()
message_box.alert("余额提醒","您余额不足,请充值!")
if message_box.confirm("确认充值10元吗?","确认框" ) then
message_box.alert("您余额不足,请充值!", "余额提醒")
if message_box.confirm("确认充值10元吗?", "确认框") then
local r1, r2 = recharge(10)
print('recharge result', r1, r2)
message_box.alert("提示","充值成功!")
print('recharge result:', r1, r2)
message_box.alert("充值成功!", "提示")
else
print('cancel')
message_box.alert("提示","取消充值!")
message_box.alert("取消充值!", "提示")
end
print('recharge finished')
end
--将按钮监听点击事件,绑定buy方法
CS.UnityEngine.GameObject.Find("Button"):GetComponent("Button").onClick:AddListener(util.coroutine_call(buy))
......
......@@ -11,15 +11,12 @@ local sync_confirm = util.async_to_sync(CS.MessageBox.ShowConfirmBox)
--构造alert和confirm函数
return {
alert = function(title, message)
if not message then
title, message = message, title
end
sync_alert(message,title)
alert = function(message, title)
sync_alert(message, title)
end;
confirm = function(title, message)
local ret = sync_confirm(title,message)
confirm = function(message, title)
local ret = sync_confirm(message, title)
return ret == true
end;
}
......
fileFormatVersion: 2
guid: 338b384cc2d92bf42b28fbe2f172c4ec
timeCreated: 1520415330
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
......@@ -18,7 +18,7 @@ public class ReImplementInLua : MonoBehaviour {
//例子1:改造Vector3
//沿用Vector3原来的映射方案Vector3 -> userdata,但是把Vector3的方法实现改为lua实现,通过xlua.genaccessor实现不经过C#直接操作内存
//改为不经过C#的好处是性能更高,而且你可以省掉相应的生成代码以达成省text段的效果
//映射仍然沿用的好处是userdata比table更省内存,但操作字段比table性能稍低,当然,你也可以结合例子2的思路,把Vector3也改为映射到table
//仍然沿用映射方案的好处是userdata比table更省内存,但操作字段比table性能稍低,当然,你也可以结合例子2的思路,把Vector3也改为映射到table
luaenv.DoString(@"
function test_vector3(title, v1, v2)
print(title)
......
fileFormatVersion: 2
guid: c4695949a6b369345a5ecdcc82c946c1
timeCreated: 1520415435
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
......@@ -99,10 +99,10 @@ Debug.Log("max:" + max(32, 12));
## 文档
* [XLua教程.doc](Assets/XLua/Doc/XLua教程.doc):教程,其配套代码[这里](Assets/XLua/Tutorial/)
* [XLua教程](Assets/XLua/Doc/XLua教程.md):教程,其配套代码[这里](Assets/XLua/Tutorial/)
* [XLua的配置](Assets/XLua/Doc/configure.md):介绍如何配置xLua。
* [XLua增加删除第三方lua库.doc](Assets/XLua/Doc/XLua增加删除第三方lua库.doc):如何增删第三方lua扩展库。
* [XLua API.doc](Assets/XLua/Doc/XLua_API.doc):API文档。
* [XLua增加删除第三方lua库](Assets/XLua/Doc/XLua增加删除第三方lua库.md):如何增删第三方lua扩展库。
* [XLua API](Assets/XLua/Doc/XLua_API.md):API文档。
* [生成引擎二次开发指南](Assets/XLua/Doc/custom_generate.md):介绍如何做生成引擎的二次开发。
* [热补丁操作指南](Assets/XLua/Doc/hotfix.md):介绍如何使用热补丁特性。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册