xLua——动态热更(三)

作者:追风剑情 发布于:2018-1-16 18:09 分类:Lua

示例

HotfixSystem.cs

using UnityEngine;
using System.Text;
using System.Collections;
using XLua;

namespace Hotfix {

    [Hotfix]
    public class HotfixSystem : MonoBehaviour
    {
        private LuaEnv luaenv;
        private byte[] luabytes;
        private string luascript;

        private int tick = 0;

        void Awake()
        {
            luaenv = new LuaEnv();
            luaenv.AddLoader(OnCustomLoader);
        }

        void OnDestroy()
        {
            luaenv = null;
        }

        byte[] OnCustomLoader(ref string filepath)
        {
            Debug.Log("*** OnCustomLoader() filepath=" + filepath);
            //TODO:: 可以在这里执行解密操作

            return luabytes;
        }

        IEnumerator LoadFromWWW(string url)
        {
            Debug.Log(url);
            using (WWW www = new WWW(url))
            {
                yield return www;
                while (!www.isDone)
                    yield return www;
                if (string.IsNullOrEmpty(www.error))
                {
                    TextAsset textAsset = www.assetBundle.mainAsset as TextAsset;
                    luabytes = textAsset.bytes;
                    string luascript = textAsset.text;

                    //触发OnCustomLoader
                    luaenv.DoString(@"
                        require('HotfixSystem.lua')
                    ");

                    //也可以直接执行lua代码
                    //luaenv.DoString(luascript);
                }
                else
                {
                    Debug.LogError(www.error);
                }
            }
        }

        void Update()
        {
            if (++tick % 50 == 0)
            {
                Debug.Log(">>>>>>>>Update in C#, tick = " + tick);
            }
        }

        void OnGUI()
        {
            if (GUI.Button(new Rect(10, 100, 300, 150), "Hotfix"))
            {
                //用WWW从streamingAssets目录加载lua AssetBundle文件
                string luafile = "file://" + Application.streamingAssetsPath + "/HotfixSystem.lua";
                StartCoroutine(LoadFromWWW(luafile));
            }
        }
    }
}

MenuEditor.cs

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;

public class MenuEditor : MonoBehaviour {

	[MenuItem("Tools/Create AssetBunldes")]
    static void CreateAssetBunldes()
    {
        Object selectAsset = Selection.activeObject;
        string selectPath = AssetDatabase.GetAssetPath(selectAsset);
        string savePath = string.Format("{0}/{1}", Application.streamingAssetsPath, Path.GetFileNameWithoutExtension(selectPath));

        if (BuildPipeline.BuildAssetBundle(selectAsset, null, savePath, BuildAssetBundleOptions.CollectDependencies))
            Debug.Log("Success");
        else
            Debug.Log("Failure");
        AssetDatabase.Refresh();
    }
}

HotfixSystem.lua

xlua.private_accessible(CS.Hotfix.HotfixSystem)
xlua.hotfix(CS.Hotfix.HotfixSystem, 'Update', function(self)
    self.tick = self.tick + 1
	if (self.tick % 50) == 0 then
		print('<<<<<<<<Update in lua, tick = ' .. self.tick)
	end
end)

测试前需要先执行下面两个菜单命令

2222.png

运行测试

1111111.png

标签: xLua

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号