少年修仙传客户端基础资源
client_Wu Xijin
2018-10-23 6c398e30b6ca7e83b05cab3743fde8e8e6c07d89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 * Tencent is pleased to support the open source community by making xLua available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * 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.
*/
 
using UnityEngine;
using System.Collections;
using System;
using XLua;
using System.Collections.Generic;
 
namespace Tutorial
{
    [LuaCallCSharp]
    public class TestClass
    {
        public static void Test123()
        {
            Debug.Log("test 123");
        }
    }
    
    [LuaCallCSharp]
    public class BaseClass
    {
        public static void BSFunc()
        {
            Debug.Log("Driven Static Func, BSF = "+ BSF);
        }
 
        public static int BSF = 1;
 
        public void BMFunc()
        {
            Debug.Log("Driven Member Func, BMF = " + BMF);
        }
 
        public int BMF { get; set; }
    }
 
    public struct Param1
    {
        public int x;
        public string y;
    }
 
    [LuaCallCSharp]
    public enum TestEnum
    {
        E1,
        E2
    }
 
    [LuaCallCSharp]
    public class DrivenClass : BaseClass
    {
        [LuaCallCSharp]
        public enum TestEnumInner
        {
            E3,
            E4
        }
 
        public void DMFunc()
        {
            Debug.Log("Driven Member Func, DMF = " + DMF);
        }
 
        public int DMF { get; set; }
 
        public double ComplexFunc(Param1 p1, ref int p2, out string p3, Action luafunc, out Action csfunc)
        {
            Debug.Log("P1 = {x=" + p1.x + ",y=" + p1.y + "},p2 = "+ p2);
            luafunc();
            p2 = p2 * p1.x;
            p3 = "hello " + p1.y;
            csfunc = () =>
            {
                Debug.Log("csharp callback invoked!");
            };
            return 1.23;
        }
 
        public void TestFunc(int i)
        {
            Debug.Log("TestFunc(int i)");
        }
 
        public void TestFunc(string i)
        {
            Debug.Log("TestFunc(string i)");
        }
 
        public static DrivenClass operator +(DrivenClass a, DrivenClass b)
        {
            DrivenClass ret = new DrivenClass();
            ret.DMF = a.DMF + b.DMF;
            return ret;
        }
 
        public void DefaultValueFunc(int a = 100, string b = "cccc", string c = null)
        {
            UnityEngine.Debug.Log("DefaultValueFunc: a=" + a + ",b=" + b + ",c=" + c);
        }
 
        public void VariableParamsFunc(int a, params string[] strs)
        {
            UnityEngine.Debug.Log("VariableParamsFunc: a =" + a);
            foreach (var str in strs)
            {
                UnityEngine.Debug.Log("str:" + str);
            }
        }
 
        public TestEnum EnumTestFunc(TestEnum e)
        {
            Debug.Log("EnumTestFunc: e=" + e);
            return TestEnum.E2;
        }
 
        public Action<string> TestDelegate = (param) =>
        {
            Debug.Log("TestDelegate in c#:" + param);
        };
 
        public event Action TestEvent;
 
        public void CallEvent()
        {
            TestEvent();
        }
 
        public ulong TestLong(long n)
        {
            return (ulong)(n + 1);
        }
 
        class InnerCalc : ICalc
        {
            public int add(int a, int b)
            {
                return a + b;
            }
 
            public int id = 100;
        }
 
        public ICalc GetCalc()
        {
            return new InnerCalc();
        }
 
        public void GenericMethod<T>()
        {
            Debug.Log("GenericMethod<" + typeof(T) + ">");
        }
    }
 
    [LuaCallCSharp]
    public interface ICalc
    {
        int add(int a, int b);
    }
 
    [LuaCallCSharp]
    public static class DrivenClassExtensions
    {
        public static int GetSomeData(this DrivenClass obj)
        {
            Debug.Log("GetSomeData ret = " + obj.DMF);
            return obj.DMF;
        }
 
        public static int GetSomeBaseData(this BaseClass obj)
        {
            Debug.Log("GetSomeBaseData ret = " + obj.BMF);
            return obj.BMF;
        }
 
        public static void GenericMethodOfString(this DrivenClass obj)
        {
            obj.GenericMethod<string>();
        }
    }
}
 
public class LuaCallCs : MonoBehaviour {
    LuaEnv luaenv = null;
    string script = @"
        function demo()
            --new C#对象
            local newGameObj = CS.UnityEngine.GameObject()
            local newGameObj2 = CS.UnityEngine.GameObject('helloworld')
            print(newGameObj, newGameObj2)
 
            CS.Tutorial.TestClass.Test123()        
            --访问静态属性,方法
            local GameObject = CS.UnityEngine.GameObject
            print('UnityEngine.Time.deltaTime:', CS.UnityEngine.Time.deltaTime) --读静态属性
            CS.UnityEngine.Time.timeScale = 0.5 --写静态属性
            print('helloworld', GameObject.Find('helloworld')) --静态方法调用
 
            --访问成员属性,方法
            local DrivenClass = CS.Tutorial.DrivenClass
            local testobj = DrivenClass()
            testobj.DMF = 1024--设置成员属性
            print(testobj.DMF)--读取成员属性
            testobj:DMFunc()--成员方法
 
            --基类属性,方法
            print(DrivenClass.BSF)--读基类静态属性
            DrivenClass.BSF = 2048--写基类静态属性
            DrivenClass.BSFunc();--基类静态方法
            print(testobj.BMF)--读基类成员属性
            testobj.BMF = 4096--写基类成员属性
            testobj:BMFunc()--基类方法调用
 
            --复杂方法调用
            local ret, p2, p3, csfunc = testobj:ComplexFunc({x=3, y = 'john'}, 100, function()
               print('i am lua callback')
            end)
            print('ComplexFunc ret:', ret, p2, p3, csfunc)
            csfunc()
 
           --重载方法调用
           testobj:TestFunc(100)
           testobj:TestFunc('hello')
 
           --操作符
           local testobj2 = DrivenClass()
           testobj2.DMF = 2048
           print('(testobj + testobj2).DMF = ', (testobj + testobj2).DMF)
 
           --默认值
           testobj:DefaultValueFunc(1)
           testobj:DefaultValueFunc(3, 'hello', 'john')
 
           --可变参数
           testobj:VariableParamsFunc(5, 'hello', 'john')
 
           --Extension methods
           print(testobj:GetSomeData()) 
           print(testobj:GetSomeBaseData()) --访问基类的Extension methods
           testobj:GenericMethodOfString()  --通过Extension methods实现访问泛化方法
 
           --枚举类型
           local e = testobj:EnumTestFunc(CS.Tutorial.TestEnum.E1)
           print(e, e == CS.Tutorial.TestEnum.E2)
           print(CS.Tutorial.TestEnum.__CastFrom(1), CS.Tutorial.TestEnum.__CastFrom('E1'))
           print(CS.Tutorial.DrivenClass.TestEnumInner.E3)
           assert(CS.Tutorial.BaseClass.TestEnumInner == nil)
 
           --Delegate
           testobj.TestDelegate('hello') --直接调用
           local function lua_delegate(str)
               print('TestDelegate in lua:', str)
           end
           testobj.TestDelegate = lua_delegate + testobj.TestDelegate --combine,这里演示的是C#delegate作为右值,左值也支持
           testobj.TestDelegate('hello')
           testobj.TestDelegate = testobj.TestDelegate - lua_delegate --remove
           testobj.TestDelegate('hello')
 
           --事件
           local function lua_event_callback1() print('lua_event_callback1') end
           local function lua_event_callback2() print('lua_event_callback2') end
           testobj:TestEvent('+', lua_event_callback1)
           testobj:CallEvent()
           testobj:TestEvent('+', lua_event_callback2)
           testobj:CallEvent()
           testobj:TestEvent('-', lua_event_callback1)
           testobj:CallEvent()
           testobj:TestEvent('-', lua_event_callback2)
 
           --64位支持
           local l = testobj:TestLong(11)
           print(type(l), l, l + 100, 10000 + l)
 
           --typeof
           newGameObj:AddComponent(typeof(CS.UnityEngine.ParticleSystem))
 
           --cast
           local calc = testobj:GetCalc()
           print('assess instance of InnerCalc via reflection', calc:add(1, 2))
           assert(calc.id == 100)
           cast(calc, typeof(CS.Tutorial.ICalc))
           print('cast to interface ICalc', calc:add(1, 2))
           assert(calc.id == nil)
 
       end
 
       demo()
 
       --协程下使用
       local co = coroutine.create(function()
           print('------------------------------------------------------')
           demo()
       end)
       assert(coroutine.resume(co))
    ";
 
    // Use this for initialization
    void Start () {
        luaenv = new LuaEnv();
        luaenv.DoString(script);
    }
    
    // Update is called once per frame
    void Update () {
        if (luaenv != null)
        {
            luaenv.Tick();
        }
    }
 
    void OnDestroy()
    {
        luaenv.Dispose();
    }
}