Unity+XLua+阿里云热更代码

5 篇文章 1 订阅

Unity+AssetsBundel+XLua+阿里云

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

一、阿里云

操作完了之后就下载阿里云提供的 .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
{

    /// <summary>
    /// 方块预制体
    /// </summary>
    public Transform Cube;

    /// <summary>
    /// 圆球预制体
    /// </summary>
    public Transform Sphere;

    /// <summary>
    /// 按钮1
    /// </summary>
    public Button BtnOne;

    /// <summary>
    /// 按钮2
    /// </summary>
    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()
    {

    }

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

    }

    /// <summary>
    /// 按钮二点击方法
    /// </summary>
    [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;

/// <summary>
/// 下载 最新 Lua 文件
/// </summary>
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"
            );
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="action"></param>
    /// <param name="filePath">阿里云上传的文件名+后缀</param>
    /// <param name="savePath">下载之后保存的路径</param>
    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;


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

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

    }

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

    /// <summary>
    /// 释放Lua虚拟机
    /// </summary>
    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();
    }
}

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橘长长长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值