Slua
u3d下的代碼 轉化到lua下可以直接使用
通過添加[CustomLuaClassAttribute]把自定義類 轉化到lua下可以直接使用
Assets/Slua/LuaObject/下面的所有文件 都是自動生成的c#到lua u3d中的類可以在lua中直接使用
單例模式
using UnityEngine;
using System.Collections;using SLua;////// 單例組件
[CustomLuaClassAttribute]
public class SingletonMonoBehaviour: MonoBehaviour where T : SingletonMonoBehaviour{
private static T _instance;
private static bool _init;
protected virtual void Awake()
{
_instance = this as T;
_init = true;
}
protected virtual void OnDestroy()
{
_instance = null; //_init = false; }
public static T instance {
get
{
if (_init == false)
{
_init = true;GameObject managerGo = GameObject.Find("Singleton");
if (managerGo == null)
{
managerGo = new GameObject("Singleton");
DontDestroyOnLoad(managerGo);
}
string managerName = typeof(T).ToString();
_instance = managerGo.GetComponentInChildren();
if (_instance == null)
{
GameObject go= new GameObject(managerName);
go.transform.parent = managerGo.transform;
_instance = go.AddComponent();}
}
return SingletonMonoBehaviour._instance;
}
}
public static bool hasInstance()
{
return _instance != null;
}
}