Version: 2022.3
言語: 日本語
ローカルアセットパッケージのインポート
ContentNamespaces

アーカイブ

アーカイブ は、あらゆるタイプのファイルを格納できる汎用パッケージ形式で、.zip ファイルに似ています。

アーカイブファイルシステム

Unity は ContentNamespace 内のマウントポイントにアーカイブをロードします。Unity がファイルをマウントすると、アプリケーションは Unity の仮想ファイルシステムを使用する Unity システムからアーカイブ内のファイルにアクセスできます。仮想ファイルシステムに直接アクセスするには AsyncReadManager を使用します。

アーカイブの作成とマウント

以下の例では、ContentBuildInterface.ArchiveAndCompress 関数でアーカイブを作成し、ArchiveFileInterface.MountAsync 関数でアーカイブをマウントする方法を説明します。この例では、LZ4 圧縮を使用し、テキストファイルを 1 つ含むアーカイブを作成します。

using Unity.Collections.LowLevel.Unsafe;
using Unity.Content;
using Unity.IO.Archive;
using Unity.IO.LowLevel.Unsafe;
using UnityEngine;
# if UNITY_EDITOR
using UnityEditor.Build.Content;
# endif

public class SampleBehaviour : MonoBehaviour
{
# if UNITY_EDITOR
  unsafe void CreateAndMountArchive()
  { 
    // アーカイブを作成
    ResourceFile[] rFiles = new ResourceFile[1];
    ResourceFile rf = new ResourceFile();
    rf.fileAlias = "file1.txt";
    rf.fileName = "Assets/file1.txt";
    rFiles[0] = rf;

    string archivePath = System.IO.Path.Combine(Application.streamingAssetsPath, "myArchive");
    ContentBuildInterface.ArchiveAndCompress(rFiles, archivePath, UnityEngine.BuildCompression.LZ4);

    // アーカイブをマウント
    var ns = ContentNamespace.GetOrCreateNamespace("MyNamespace123");
    ArchiveHandle ahandle = ArchiveFileInterface.MountAsync(ns, archivePath, "a:");
    ahandle.JobHandle.Complete();

    string textFilePath = ahandle.GetMountPath() + "file1.txt"; // ns:/MyNamespace123/a:/file1.txt
    ReadCommand cmd;
    cmd.Size = 1024;
    cmd.Buffer = UnsafeUtility.Malloc(cmd.Size, 4, Unity.Collections.Allocator.Temp);
    cmd.Offset = 0;
    
    NativeArray<ReadCommand> cmds = new NativeArray<ReadCommand>(1, Allocator.Persistent);
    cmds[0] = cmd;

    ReadHandle rHandle = AsyncReadManager.Read(textFilePath, (ReadCommand*)cmds.GetUnsafePtr(), 1);
    rHandle.JobHandle.Complete();

    rHandle.Dispose();
    UnsafeUtility.Free(cmd.Buffer, Unity.Collections.Allocator.Temp);
    cmds.Dipose():
  }
# endif
}
ローカルアセットパッケージのインポート
ContentNamespaces