U3D_12_12

作者: 此年此景 | 来源:发表于2017-03-24 11:02 被阅读0次

    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;
    
    }
    
    }
    
    

    相关文章

      网友评论

          本文标题:U3D_12_12

          本文链接:https://www.haomeiwen.com/subject/kqnxottx.html