Official ulua case

Source: Internet
Author: User

1. helloworld

Using luainterface; public class helloworld: monobehaviour {void start () {luastate L = new luastate (); // use Lua L in C. dostring ("Print ('Hello world World ')");}}

2. creategameobject

Using luainterface; public class creategameobject: monobehaviour {// use C # private string script = @ "luanet. load_assembly ('unityengine ') // first, you need to load an Assembly that contains the specified type gameobject = luanet. import_type ('unityengine. gameobject') // to obtain the reference of the specified type // use the reference local newgameobj = gameobject ('newobj ') newgameobj: addcomponent ('deletesystem ')"; // use luavoid start () {luastate L = new luastate (); L. dostring (SCRIPT); // execute Lua script }}

3. accessingluavariables

Using luainterface; public class accessingluavariables: monobehaviour {private string script = @ "// Lua script Lua uses C # luanet. load_assembly ('unityengine ') // first, you need to load an Assembly that contains the specified type gameobject = luanet. import_type ('unityengine. gameobject') // to obtain the specified type of reference '.. tostring (I) Local PS = newgameobj: addcomponent ('samples') PS: Stop () table. insert (participant, PS) end var2read = 42 "; // use luavoid start () {luastate L = new luastate () in C (); // assign to global scope variables as if they're keys in a dictionary (they are really) L ["objs2spawn"] = 5; L. dostring (SCRIPT); // read print ("read from Lua:" + L ["var2read"] from the global range. tostring (); // obtain the Lua table as the luatable object. values) {ps. play ();}}}

4. scriptsfromfile

Lua script file 04_scriptsfromfile.lua:

-- Lua script Lua uses C # print ("this is a script from a file world ")

C # file:

Using luainterface; public class scriptsfromfile: monobehaviour {public textasset scriptfile; void start () {luastate L = new luastate (); // C # Run Lua script L. dostring (scriptfile. text );}}

5. callluafunction

Using luainterface; public class callluafunction: monobehaviour {// Lua script Lua uses C # private string script = @ "function luafunc (Message) print (Message) return 42 end "; void start () {luastate L = new luastate (); // C # Run The Lua script L. dostring (SCRIPT); // obtain the function object luafunction F = L. getfunction ("luafunc"); // call it object [] r = f. call ("I called a Lua function! "); // The Lua function can return variable variables, So we store those as C # object arrays, and print the first element in this case 42 print (R [0]);}

6. luaco routines

Using luainterface; public class luaco routines: monobehaviour {// This example prints the Fibonacci series (the current number = the sum of the first two numbers) 1 ~ 10 bits, each iteration is 1 second interval // use C # private string script = @ "luanet. load_assembly ('unityengine ') -- first, you need to load an Assembly that contains the specified type of local time = luanet. import_type ('unityengine. time') -- to get a reference of a specified type -- in each frame of the yields, the global game time is still less than the timestamp function waitseconds (t) Local timestamp = time. time + T while time. time <timestamp do coroutine. yield () end function fib (n) Local a, B = 0, 1 while n> 0 do a, B = B, A + B n = n-1 end return a end function myfunc () print ('ututine started') Local I = 0 for I = 0, 10, 1 do print (FIB (I) waitseconds (1) end print ('ututine enabled') End "; private luathread CO = NULL; // Lua thread void start () {luastate L = new luastate (); // C # Run The Lua script L. dostring (SCRIPT); // obtain the function object luafunction F = L. getfunction ("myfunc"); // create a paused Lua coroutine collaborative object from the parent State and function CO = new luathread (L, f );/ /Coroutine needs to be restored at each frame because it is in yield at each frame, or you can only restore it in C # With the opposite condition Co. start ();} void Update () {If (! Co. isdead () {Co. resume (); // restore} else {print ("coroutine has exited. "); // only allow C # To clear the encapsulated object CO = NULL in order to destroy the collaboration (but not in the Lua function, but in the collaborative stack instance ;}}}


 

 

 

 

Official ulua case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.