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;
}
}
网友评论