Skip to content

Commit 0a75642

Browse files
committed
例子及教程都加上名字空间,fix Tencent#556
1 parent 9f6f0d4 commit 0a75642

File tree

22 files changed

+1007
-924
lines changed

22 files changed

+1007
-924
lines changed

Assets/XLua/Examples/01_Helloworld/Helloworld.cs

+18-12
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,22 @@
99
using UnityEngine;
1010
using XLua;
1111

12-
public class Helloworld : MonoBehaviour {
13-
// Use this for initialization
14-
void Start () {
15-
LuaEnv luaenv = new LuaEnv();
16-
luaenv.DoString("CS.UnityEngine.Debug.Log('hello world')");
17-
luaenv.Dispose();
18-
}
19-
20-
// Update is called once per frame
21-
void Update () {
22-
23-
}
12+
namespace XLuaTest
13+
{
14+
public class Helloworld : MonoBehaviour
15+
{
16+
// Use this for initialization
17+
void Start()
18+
{
19+
LuaEnv luaenv = new LuaEnv();
20+
luaenv.DoString("CS.UnityEngine.Debug.Log('hello world')");
21+
luaenv.Dispose();
22+
}
23+
24+
// Update is called once per frame
25+
void Update()
26+
{
27+
28+
}
29+
}
2430
}

Assets/XLua/Examples/02_U3DScripting/LuaBehaviour.cs

+69-65
Original file line numberDiff line numberDiff line change
@@ -12,90 +12,94 @@
1212
using XLua;
1313
using System;
1414

15-
[System.Serializable]
16-
public class Injection
15+
namespace XLuaTest
1716
{
18-
public string name;
19-
public GameObject value;
20-
}
17+
[System.Serializable]
18+
public class Injection
19+
{
20+
public string name;
21+
public GameObject value;
22+
}
2123

22-
[LuaCallCSharp]
23-
public class LuaBehaviour : MonoBehaviour {
24-
public TextAsset luaScript;
25-
public Injection[] injections;
24+
[LuaCallCSharp]
25+
public class LuaBehaviour : MonoBehaviour
26+
{
27+
public TextAsset luaScript;
28+
public Injection[] injections;
2629

27-
internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only!
28-
internal static float lastGCTime = 0;
29-
internal const float GCInterval = 1;//1 second
30+
internal static LuaEnv luaEnv = new LuaEnv(); //all lua behaviour shared one luaenv only!
31+
internal static float lastGCTime = 0;
32+
internal const float GCInterval = 1;//1 second
3033

31-
private Action luaStart;
32-
private Action luaUpdate;
33-
private Action luaOnDestroy;
34+
private Action luaStart;
35+
private Action luaUpdate;
36+
private Action luaOnDestroy;
3437

35-
private LuaTable scriptEnv;
38+
private LuaTable scriptEnv;
3639

37-
void Awake()
38-
{
39-
scriptEnv = luaEnv.NewTable();
40+
void Awake()
41+
{
42+
scriptEnv = luaEnv.NewTable();
4043

41-
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
42-
LuaTable meta = luaEnv.NewTable();
43-
meta.Set("__index", luaEnv.Global);
44-
scriptEnv.SetMetaTable(meta);
45-
meta.Dispose();
44+
// 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
45+
LuaTable meta = luaEnv.NewTable();
46+
meta.Set("__index", luaEnv.Global);
47+
scriptEnv.SetMetaTable(meta);
48+
meta.Dispose();
4649

47-
scriptEnv.Set("self", this);
48-
foreach (var injection in injections)
49-
{
50-
scriptEnv.Set(injection.name, injection.value);
51-
}
50+
scriptEnv.Set("self", this);
51+
foreach (var injection in injections)
52+
{
53+
scriptEnv.Set(injection.name, injection.value);
54+
}
5255

53-
luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
56+
luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
5457

55-
Action luaAwake = scriptEnv.Get<Action>("awake");
56-
scriptEnv.Get("start", out luaStart);
57-
scriptEnv.Get("update", out luaUpdate);
58-
scriptEnv.Get("ondestroy", out luaOnDestroy);
58+
Action luaAwake = scriptEnv.Get<Action>("awake");
59+
scriptEnv.Get("start", out luaStart);
60+
scriptEnv.Get("update", out luaUpdate);
61+
scriptEnv.Get("ondestroy", out luaOnDestroy);
5962

60-
if (luaAwake != null)
61-
{
62-
luaAwake();
63+
if (luaAwake != null)
64+
{
65+
luaAwake();
66+
}
6367
}
64-
}
6568

66-
// Use this for initialization
67-
void Start ()
68-
{
69-
if (luaStart != null)
70-
{
71-
luaStart();
72-
}
73-
}
74-
75-
// Update is called once per frame
76-
void Update ()
77-
{
78-
if (luaUpdate != null)
69+
// Use this for initialization
70+
void Start()
7971
{
80-
luaUpdate();
72+
if (luaStart != null)
73+
{
74+
luaStart();
75+
}
8176
}
82-
if (Time.time - LuaBehaviour.lastGCTime > GCInterval)
77+
78+
// Update is called once per frame
79+
void Update()
8380
{
84-
luaEnv.Tick();
85-
LuaBehaviour.lastGCTime = Time.time;
81+
if (luaUpdate != null)
82+
{
83+
luaUpdate();
84+
}
85+
if (Time.time - LuaBehaviour.lastGCTime > GCInterval)
86+
{
87+
luaEnv.Tick();
88+
LuaBehaviour.lastGCTime = Time.time;
89+
}
8690
}
87-
}
8891

89-
void OnDestroy()
90-
{
91-
if (luaOnDestroy != null)
92+
void OnDestroy()
9293
{
93-
luaOnDestroy();
94+
if (luaOnDestroy != null)
95+
{
96+
luaOnDestroy();
97+
}
98+
luaOnDestroy = null;
99+
luaUpdate = null;
100+
luaStart = null;
101+
scriptEnv.Dispose();
102+
injections = null;
94103
}
95-
luaOnDestroy = null;
96-
luaUpdate = null;
97-
luaStart = null;
98-
scriptEnv.Dispose();
99-
injections = null;
100104
}
101105
}

Assets/XLua/Examples/04_LuaObjectOrented/InvokeLua.cs

+59-56
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,31 @@
1010
using UnityEngine;
1111
using XLua;
1212

13-
public class PropertyChangedEventArgs : EventArgs
13+
namespace XLuaTest
1414
{
15-
public string name;
16-
public object value;
17-
}
15+
public class PropertyChangedEventArgs : EventArgs
16+
{
17+
public string name;
18+
public object value;
19+
}
1820

19-
public class InvokeLua : MonoBehaviour
20-
{
21-
[CSharpCallLua]
22-
public interface ICalc
21+
public class InvokeLua : MonoBehaviour
2322
{
24-
event EventHandler<PropertyChangedEventArgs> PropertyChanged;
23+
[CSharpCallLua]
24+
public interface ICalc
25+
{
26+
event EventHandler<PropertyChangedEventArgs> PropertyChanged;
2527

26-
int Add(int a, int b);
27-
int Mult { get; set; }
28+
int Add(int a, int b);
29+
int Mult { get; set; }
2830

29-
object this[int index] { get; set; }
30-
}
31+
object this[int index] { get; set; }
32+
}
3133

32-
[CSharpCallLua]
33-
public delegate ICalc CalcNew(int mult, params string[] args);
34+
[CSharpCallLua]
35+
public delegate ICalc CalcNew(int mult, params string[] args);
3436

35-
private string script = @"
37+
private string script = @"
3638
local calc_mt = {
3739
__index = {
3840
Add = function(self, a, b)
@@ -83,44 +85,45 @@ public interface ICalc
8385
end
8486
}
8587
";
86-
// Use this for initialization
87-
void Start()
88-
{
89-
LuaEnv luaenv = new LuaEnv();
90-
Test(luaenv);//调用了带可变参数的delegate,函数结束都不会释放delegate,即使置空并调用GC
91-
luaenv.Dispose();
92-
}
93-
94-
void Test(LuaEnv luaenv)
95-
{
96-
luaenv.DoString(script);
97-
CalcNew calc_new = luaenv.Global.GetInPath<CalcNew>("Calc.New");
98-
ICalc calc = calc_new(10, "hi", "john"); //constructor
99-
Debug.Log("sum(*10) =" + calc.Add(1, 2));
100-
calc.Mult = 100;
101-
Debug.Log("sum(*100)=" + calc.Add(1, 2));
102-
103-
Debug.Log("list[0]=" + calc[0]);
104-
Debug.Log("list[1]=" + calc[1]);
105-
106-
calc.PropertyChanged += Notify;
107-
calc[1] = "dddd";
108-
Debug.Log("list[1]=" + calc[1]);
109-
110-
calc.PropertyChanged -= Notify;
111-
112-
calc[1] = "eeee";
113-
Debug.Log("list[1]=" + calc[1]);
114-
}
115-
116-
void Notify(object sender, PropertyChangedEventArgs e)
117-
{
118-
Debug.Log(string.Format("{0} has property changed {1}={2}", sender, e.name, e.value));
119-
}
120-
121-
// Update is called once per frame
122-
void Update()
123-
{
124-
88+
// Use this for initialization
89+
void Start()
90+
{
91+
LuaEnv luaenv = new LuaEnv();
92+
Test(luaenv);//调用了带可变参数的delegate,函数结束都不会释放delegate,即使置空并调用GC
93+
luaenv.Dispose();
94+
}
95+
96+
void Test(LuaEnv luaenv)
97+
{
98+
luaenv.DoString(script);
99+
CalcNew calc_new = luaenv.Global.GetInPath<CalcNew>("Calc.New");
100+
ICalc calc = calc_new(10, "hi", "john"); //constructor
101+
Debug.Log("sum(*10) =" + calc.Add(1, 2));
102+
calc.Mult = 100;
103+
Debug.Log("sum(*100)=" + calc.Add(1, 2));
104+
105+
Debug.Log("list[0]=" + calc[0]);
106+
Debug.Log("list[1]=" + calc[1]);
107+
108+
calc.PropertyChanged += Notify;
109+
calc[1] = "dddd";
110+
Debug.Log("list[1]=" + calc[1]);
111+
112+
calc.PropertyChanged -= Notify;
113+
114+
calc[1] = "eeee";
115+
Debug.Log("list[1]=" + calc[1]);
116+
}
117+
118+
void Notify(object sender, PropertyChangedEventArgs e)
119+
{
120+
Debug.Log(string.Format("{0} has property changed {1}={2}", sender, e.name, e.value));
121+
}
122+
123+
// Update is called once per frame
124+
void Update()
125+
{
126+
127+
}
125128
}
126-
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
using UnityEngine;
22
using XLua;
33

4-
public class CoroutineTest : MonoBehaviour {
5-
LuaEnv luaenv = null;
6-
// Use this for initialization
7-
void Start()
4+
namespace XLuaTest
5+
{
6+
public class CoroutineTest : MonoBehaviour
87
{
9-
luaenv = new LuaEnv();
10-
luaenv.DoString("require 'coruntine_test'");
11-
}
8+
LuaEnv luaenv = null;
9+
// Use this for initialization
10+
void Start()
11+
{
12+
luaenv = new LuaEnv();
13+
luaenv.DoString("require 'coruntine_test'");
14+
}
1215

13-
// Update is called once per frame
14-
void Update()
15-
{
16-
if (luaenv != null)
16+
// Update is called once per frame
17+
void Update()
1718
{
18-
luaenv.Tick();
19+
if (luaenv != null)
20+
{
21+
luaenv.Tick();
22+
}
1923
}
20-
}
2124

22-
void OnDestroy()
23-
{
24-
luaenv.Dispose();
25+
void OnDestroy()
26+
{
27+
luaenv.Dispose();
28+
}
2529
}
2630
}

0 commit comments

Comments
 (0)