Skip to content

implement unity StartCoroutine

sonygod edited this page Dec 29, 2014 · 4 revisions

it's very easy use :

update:

there no need to add extra method in TypeCheck.hx anymore,just add an extra class

same define as

LuaMonoBehavior.cs

package unityHelper;

import unityengine.*;

/**
 * LuaMonoBehaviour
 * @author sonygod
 * 
 * same function as LuaMonoBehaviour.cs
 */
extern class   LuaMonoBehaviour extends MonoBehaviour 
{

	public function new():Void;
	public function StopLuaCoroutine():Void;
	public function LuaDestoryFunction():Void;
	public function callLua(func:Dynamic):Void;
	public function UnityCoroutine(ins:YieldInstruction , func:Dynamic ):Void;
	
}

here is example code.

package ;
import luaxe.Lua;
import unityengine.*;
using Reflect;
using unityHelper.TypeCheck;
class Main 
{
	public static var mMain:Main;

	
	public var current:LuaMonoBehaviour;
	public function new() {
		
		
	untyped __call__(SetMain, this);
	}
	@:keep	  function Awake() {
		
		
	 current.callLua(delay);
  
// current.SetTimeout(delay, 2);

 current.LuaDestoryFunction();
   
 current.StopLuaCoroutine();
 current.StopAllCoroutines();
	
current.UnityCoroutine(new WaitForSeconds(2), delay);
			
	}
	
	@:keep public 	function delay() {
		trace("delay10 " );
		
	}
	
	
public static function main()
	{
		
	mMain =	new Main();
	
	}
}

and the LuaMono file add these two method note:not static

public  void UnityCoroutine(YieldInstruction ins, LuaFunction func)
    {
      
     StartCoroutine(doCoroutine(ins, func));
    }

    private  IEnumerator doCoroutine(YieldInstruction ins, LuaFunction func)
    {
        yield return ins;
        func.Call();
    }
Clone this wiki locally