Unity - Scripting API: ArchiveFileInterface (original) (raw)
class in Unity.IO.Archive
/
Implemented in:UnityEngine.CoreModule
Suggest a change
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Close
Submission failed
For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close
Your name Your email Suggestion*
Cancel
Description
Provides methods for managing Unity archive files.
// The following example describes how to create an archive with // ContentBuildInterface.ArchiveAndCompress and mount the archive with // ArchiveFileInterface.MountAsync. This example produces an archive that uses LZ4 // compression and contains one text file.
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() { // Create the Archive ResourceFile[] rFiles = new ResourceFile[1]; ResourceFile rf = new ResourceFile(); rf.fileName = "Assets/file1.txt"; // Path of the existing file, to copy into the Archive rf.fileAlias = "file1.txt"; // Path given to the file inside the Archive rFiles[0] = rf;
string archivePath = System.IO.Path.Combine([Application.streamingAssetsPath](Application-streamingAssetsPath.html), "myArchive");
[ContentBuildInterface.ArchiveAndCompress](Build.Content.ContentBuildInterface.ArchiveAndCompress.html)(rFiles, archivePath, UnityEngine.BuildCompression.LZ4);
// Mount the Archive
var ns = [ContentNamespace.GetOrCreateNamespace](Unity.Content.ContentNamespace.GetOrCreateNamespace.html)("MyNamespace123");
[ArchiveHandle](Unity.IO.Archive.ArchiveHandle.html) ahandle = [ArchiveFileInterface.MountAsync](Unity.IO.Archive.ArchiveFileInterface.MountAsync.html)(ns, archivePath, "a:");
ahandle.JobHandle.Complete();
string textFilePath = ahandle.GetMountPath() + "file1.txt"; // ns:/MyNamespace123/a:/file1.txt
[ReadCommand](Unity.IO.LowLevel.Unsafe.ReadCommand.html) cmd;
cmd.Size = 1024;
cmd.Buffer = [UnsafeUtility.Malloc](Unity.Collections.LowLevel.Unsafe.UnsafeUtility.Malloc.html)(cmd.Size, 4, [Unity.Collections.Allocator.Temp](Unity.Collections.Allocator.Temp.html));
cmd.Offset = 0;
NativeArray<[ReadCommand](Unity.IO.LowLevel.Unsafe.ReadCommand.html)> cmds = new NativeArray<[ReadCommand](Unity.IO.LowLevel.Unsafe.ReadCommand.html)>(1, [Allocator.Persistent](Unity.Collections.Allocator.Persistent.html));
cmds[0] = cmd;
[ReadHandle](Unity.IO.LowLevel.Unsafe.ReadHandle.html) rHandle = [AsyncReadManager.Read](Unity.IO.LowLevel.Unsafe.AsyncReadManager.Read.html)(textFilePath, ([ReadCommand](Unity.IO.LowLevel.Unsafe.ReadCommand.html)*)cmds.GetUnsafePtr(), 1);
rHandle.JobHandle.Complete();
// ...At this point cmd.Buffer contains contents from file1.txt (up to 1024 bytes)...
rHandle.Dispose();
[UnsafeUtility.Free](Unity.Collections.LowLevel.Unsafe.UnsafeUtility.Free.html)(cmd.Buffer, [Unity.Collections.Allocator.Temp](Unity.Collections.Allocator.Temp.html));
cmds.Dipose():
ahandle.Unmount().Complete();
} #endif }
Static Methods
Method | Description |
---|---|
GetMountedArchives | Retrieves all mounted archives. |
MountAsync | Loads all files in an archive to a mount point. |