CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Loading a file from a CPK file

Data flow

With FileMajik PRO, you can pack many data files used in the application into a single one and access them as you want at runtime.
(You can refer to a file packed in a CPK file by using its name or its ID.)

crifs2_tutor02_dataflow_small.png
A packed file is called a "CPK file" (CRI packed file).
The "CPK File Builder" tool is used to create a CPK file.
It can be done very easily, just by dragging and dropping a directory that contains the files you want into the tool.

Overview of loading a file

The following picture shows the process of loading a file from a CPK file.

fmpu_keys_fileload_cpk_system_mini.png

First, you need to register a CPK file in a "binder" (binding).
A binder is like a virtual drive or device.
Binding allows you to access files (content files) contained in a CPK file through a binder.
Specify a binder and the name of a content file, then use the loader object to load the content file.

Process flow

  • (1) Creating a binder
    Create a binder to load a file from a CPK file.

    this.binder = new CriFsBinder();

  • (2) Binding a CPK file
    Bind a CPK file to the binder you created.
    You can bind a local CPK file but also a CPK file located on a remote server.

    • Binding a local CPK file
      CriFsBindRequest request = CriFsUtility.BindCpk(this.binder, "StreamingAssets/sample.cpk");

    • Binding a CPK file on a remote server
      CriFsBindRequest request = CriFsUtility.BindCpk(this.binder, "http://crimot.com/sdk/sampledata/crifilesystem/sample.cpk");
A binding request is issued and the file is bound asynchronously in the background.
The yield statement waits for the completion of the binding operation.
If the binding is completed successfully, you will obtain a bind ID from the request.

yield return request.WaitForDone(this);
if (request.error == null) {
this.bindId = request.bindId;
}

  • (3) Loading a file
    A coroutine is used to load a file.
    First, a request to load the file from the CPK file is issued. The yield statement waits for the completion of the file loading.
    The file loading procedure is the same both for local and remote CPK files.

    CriFsLoadFileRequest request = CriFsUtility.LoadFile(this.binder, contentfile);

  • (4) Unbinding and destroying the binder
    The binding can be cancelled like this:

    CriFsBinder.Unbind(this.bindId);

    Also, make sure to destroy the binder.

    this.binder.Dispose();

[Note]
For the actual code, see the " [CriFs] Installing data " sample.