How to package specific files in unity project

How to package specific files in unity project

Let me declare that this is the first time Ben Xiaobai wrote a blog. I hope to include the shortcomings. Please point out any errors. I am grateful.
Sometimes we get a game project with a lot of rich resources. Sometimes we want to find all the audio files in this project, is it too troublesome to find one by one (but if the original project has been sorted and sorted Ok, then it is not necessary Fighting)
Go directly to the code, you should be able to understand it
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class Bundle  {
    [MenuItem("Tools/SaveAudioClip")]
    static void SavePos()
    {
       //--保存路径(可以自定义路径)
        string outpath = @"C:\Users\Username\Desktop\MyAudio\";
       //物体列表
        List<Object> objList = new List<Object>();
       //--获取当前工程下的文件所有文件路径
        string[] paths = Directory.GetFiles("Assets","*.*",SearchOption.AllDirectories);
        foreach (var path in paths)
        {
            //--剔除meta文件
            if (Path.GetExtension(path) != null && Path.GetExtension(path) != ".meta")
            {
                Object obj=AssetDatabase.LoadAssetAtPath<Object>(path);
                objList.Add(obj);
            }
        }
        //创建文件夹
        if (!File.Exists(outpath))
            Directory.CreateDirectory(outpath);

        List<string> audiopath = new List<string>();
        //--遍历所有的物体对象
        foreach (var item in objList)
        {
            //--如果是音频文件
            if (item is AudioClip)
            {
                string itemPath = AssetDatabase.GetAssetPath(item);
                string extension= Path.GetExtension(itemPath);
                audiopath.Add(AssetDatabase.GetAssetPath(item));
                //--第一种方法
                //AssetDatabase.ExportPackage(itemPath, outpath + item.name + extension);
                //第二种方法:利用流文件完成导出
                CopyFile(itemPath, outpath + item.name);
            }
        }
        //第三种方法:把整个打包成unitypackage文件
        AssetDatabase.ExportPackage(audiopath.ToArray(),outpath+"name"+".unitypackage",ExportPackageOptions.Default);
        Debug.Log("完成打包");
    }
    /// <summary>
    /// 复制文件到新的目录
    /// </summary>
    /// <param name="soucre">原文件</param>
    /// <param name="target">目标文件</param>
   static void CopyFile(string soucre, string target)
  {
      //1、读取的流
      using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
      {
          //2、写入的流
          using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
          {
              //--指定读取大小
              byte[] buffer = new byte[1024 * 1024];
              while (true)
              {
                  int r = fsRead.Read(buffer, 0, buffer.Length);
                  if (r == 0)
                      break;     
                  fsWrite.Write(buffer, 0, r);
              }
          }
      }
  }
}

Each of the three methods has advantages:

The first is the packaging method that comes with unity. If you package the prefab file, you can select ExportPackageOptions into the form of IncludeDependencies (to prevent the loss of scripts or something) ^_^

The second is to use the file stream to read

The third is to package all specific resources into unitypackage files

ps: One thing to remind is that if the packaged target file is the current unity project, it may not be an audio file. It is because there is no meta file generated...TnT...

This article just packs the audio files. If you are interested, you can try to pack other files (for example, what video, prefab, etc.);

The first time I write a blog, I am ignorant of unity, but I think there are some things that should be shared, so that both you and me can learn quickly. Right? 

Guess you like

Origin blog.csdn.net/K20132014/article/details/50885669