Unity+XLua+阿里云热更代码

Unity+AssetsBundel+XLua+阿里云

Unity热更新的步骤:AssetsBundle打包好资源——上传到服务器——运行项目——检测是否需要更新——1.需要更新2.不需要更新——1.需要更新——从服务器下载资源到本地——本地加载资源包——运行。

一、阿里云

Unity+XLua+阿里云热更代码_第1张图片

Unity+XLua+阿里云热更代码_第2张图片

Unity+XLua+阿里云热更代码_第3张图片

Unity+XLua+阿里云热更代码_第4张图片

操作完了之后就下载阿里云提供的 .Net SDK,解压后把Aliyun.OSS.dll导入到Unity 的 Plugins 文件夹下,需要把Unity .Net框架改为4.6。

 

二、XLua

1.配置XLua配置

2.编写脚本

Xlua:

Load.lua

local UnityEngine=CS.UnityEngine
xlua.hotfix(CS.LuaHotfixTest,'OnClickOne',function(self)
            UnityEngine.Object.Destroy(self.Cube.gameObject)
end)
 
 
xlua.hotfix(CS.LuaHotfixTest,'OnClickTwo',function(self)
      		UnityEngine.Object.Destroy(self.Sphere.gameObject)
end)

Dispose.lua

xlua.hotfix(CS.Test,'OnClickOne',nil)
 
xlua.hotfix(CS.Test,'OnClickTwo',nil)

C#:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using XLua;

[Hotfix] // lua需要热更新的脚本
public class LuaHotfixTest : MonoBehaviour
{

    /// 
    /// 方块预制体
    /// 
    public Transform Cube;

    /// 
    /// 圆球预制体
    /// 
    public Transform Sphere;

    /// 
    /// 按钮1
    /// 
    public Button BtnOne;

    /// 
    /// 按钮2
    /// 
    public Button BtnTwo;


    // Use this for initialization
    void Start()
    {

        BtnOne.onClick.AddListener(OnClickOne);
        BtnTwo.onClick.AddListener(OnClickTwo);

    }

    // Update is called once per frame
    void Update()
    {

    }

    /// 
    /// 按钮一点击方法
    /// 
    [LuaCallCSharp]
    private void OnClickOne()
    {

    }

    /// 
    /// 按钮二点击方法
    /// 
    [LuaCallCSharp]
    private void OnClickTwo()
    {

    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Aliyun.OSS;
using Aliyun.OSS.Common;
using UnityEngine;

/// 
/// 下载 最新 Lua 文件
/// 
public class DownLoadLua : MonoBehaviour {

    string filePath;
    string savePath;
    Thread thread;
    Action GetObjectSuccessCallback;


    OssClient ossClient;
    // Start is called before the first frame update
    void Start()
    {
        ossClient = new OssClient(Config.EndPoint, Config.AccessKeyId, Config.AccessKeySecret);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GetObjectByThread(() =>
            {
                Debug.Log("下载成功");
            },
            "luascript.assetsbundle",
             @"F:\GaoSaiFi\UnityTools\XLua\xLua-master\Assets\StreamingAssets\luascript.assetsbundle"
            );
        }
    }

    /// 
    /// 
    /// 
    /// 
    /// 阿里云上传的文件名+后缀
    /// 下载之后保存的路径
    public void GetObjectByThread(Action action, string filePath, string savePath)
    {
        this.GetObjectSuccessCallback = action;
        this.filePath = filePath;
        this.savePath = savePath;
        thread = new Thread(GetObject);
        thread.Start();
    }

    void GetObject()
    {
        try
        {
            OssObject result = ossClient.GetObject(Config.Bucket, filePath);
            using (var resultStream = result.Content)
            {
                using (var fs = File.Open(savePath, FileMode.OpenOrCreate))
                {
                    int length = (int)resultStream.Length;
                    byte[] bytes = new byte[length];
                    do
                    {
                        length = resultStream.Read(bytes, 0, length);
                        fs.Write(bytes, 0, length);

                    } while (length != 0);
                    this.GetObjectSuccessCallback();
                }
            }
        }
        catch (OssException e)
        {
            print("下载文件出错:" + e);
        }
        catch (Exception e)
        {
            print("下载文件出错:" + e);
        }
        finally
        {

            thread.Abort();
            this.GetObjectSuccessCallback = null;
        }

    }

}
public class Config
{
    public const string AccessKeyId = "填写自己的AccessKeyId";
    public const string AccessKeySecret = "填写自己的AccessKeySecret";
    public const string EndPoint = "oss-cn-beijing.aliyuncs.com";
    public const string Bucket = "文件夹-gf-test-0";

}

using System.Collections;
using UnityEngine;
using XLua;


/// 
/// 加载下载好的 Lua 文件
/// 
public class LoadLua : MonoBehaviour
{

    private LuaEnv _luaEnv;
    /// 
    /// AB包
    /// 
    private AssetBundle ab;
    /// 
    /// 加载Lua文件并执行
    /// 
    private void Awake()
    {
        _luaEnv = new LuaEnv();
        StartCoroutine(LoadABPackage6("Load"));

    }

    /// 
    /// 释放lua方法
    /// 
    private void OnDisable()
    {
        StartCoroutine(LoadABPackage6("Dispose"));
    }

    /// 
    /// 释放Lua虚拟机
    /// 
    private void OnDestroy()
    {
        _luaEnv.Dispose();
    }

    IEnumerator LoadABPackage6(string fileName)
    {

        AssetBundle obj = AssetBundle.LoadFromFile(@"F:\GaoSaiFi\UnityTools\XLua\xLua-master\Assets\StreamingAssets\luascript.assetsbundle"); //加载我们的AB包
        TextAsset ta = obj.LoadAsset(fileName + ".lua.txt") as TextAsset;
        yield return 1;
        _luaEnv.DoString(ta.text);

    }
}

 

三、AsstesBundle

Editor文件夹下添加脚本

using UnityEditor;
using System.IO;
using UnityEngine;

public class LoadAB
{

    private static readonly string AbPath = Application.streamingAssetsPath;
    [MenuItem("Tools/CreateAB")]
    static void CreateAbPack()
    {
        BuildPipeline.BuildAssetBundles(AbPath, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
        AssetDatabase.Refresh();
    }
}

Unity+XLua+阿里云热更代码_第5张图片

Unity+XLua+阿里云热更代码_第6张图片

把打包出来的文件上传阿里云即可。

你可能感兴趣的:(XLua,Unity,XLua,Unity,热更新,AssetsBundle,阿里云)