Unity3d资源自动更新

本文介绍了如何使用Unity3D实现AssetBundle资源的自动更新。通过编写Editor脚本,可以创建AssetBundle并生成MD5配置文件,然后根据配置文件比较本地与服务器资源,实现资源的自动更新。步骤包括创建AssetBundle、设置资源路径、运行脚本更新资源,确保本地资源始终与服务器保持一致。
摘要由CSDN通过智能技术生成

1.创建AssetBudle 通过脚本将一个prefab创建为AssetBudle,将下面代码放入Editor文件夹下面

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class ExportAssetBundles : EditorWindow
{
    [MenuItem("Assets/Build AssetBundle From Selection")]  
    static void ExportResourceRespective()
    { // 打开保存面板,获得用户选择的路径  
        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");

        if (path.Length != 0)
        {
            // 选择的要保存的对象  
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            //打包  
            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
        }  
    }
    [MenuItem("Assets/Save Scene")]
    static void ExportScene()
    {
        // 打开保存面板,获得用户选择的路径  
        string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");

        if (path.Length != 0)
        {
            // 选择的要保存的对象  
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            string[] scenes = { "Assets/scene1.unity" };
            //打包  
            BuildPipeline.BuildPlayer(scenes, path, BuildTarget.StandaloneWindows, BuildOptions.BuildAdditionalStreamedScenes);
        }
    }  
}

2.本地资源文件以及服务器文件的MD5配置文件生成

  public void CreatConfiga(string resPath)
    {
        string[] files = Directory.GetFiles(resPath, "*", SearchOption.AllDirectories);
        System.Text.StringBuilder versions = new System.Text.StringBuilder();
        for (int i = 0; i < files.Length; i++)
        {
            string filePath = files[i];
            string extension = filePath.Substring(files[i].LastIndexOf("."));
            if (extension == ".assetbundle")
            {
                string relativePath = filePath.Replace(resPath, "").Replace("\\", "/");
                string md5 = MD5File(filePath);
                versions.Append(relativePath).Append(",").Append(md5).Append(&
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值