CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Embedding data into a game
This section describes the procedure to load a file with FileMajik PRO.
Here we will create a script that "loads a text file from a packed file and shows its contents".
We will follow these steps:

(1) Creating a new Unity project and importing a plugin package

First, some preparation is needed to run the file loading script.
As described in the CRI ADX Quick Start section, create a new Unity project and import the plugin package into the project.
Let's name the project "TutorialFs" and the scene "NewScene".

(2) Copying the files to load

(2-1) Preparing the files
Next, let's prepare the files we will load from the script.
We will use sample.cpk, which is a sample CPK file for FileMajik PRO.
The following picture shows the location of the file.

Location of the sample file
  * cri

  \ * unity

    \ * samples

      \ * crifilesystem

        \ * basic

          \ * Assets

            \ * StreamingAssets

              \ * sample.cpk

 

 

 

Sample for FileMajik PRO

 

 

 

Packed file

 

The sample.cpk packed file contains the following files:

sample.cpk
- criware.png Image file (CRIWARE logo)
- sample_image1.png Image file 1
- sample_image2.png Image file 2
- sample_text.txt Text file

(2-2) Copying the files to StreamingAssets
You must copy all the files that will be loaded from the script to the StreamingAssets subfolder under the Assets folder of the Unity project.
Copy sample.cpk to the StreamingAssets subfolder.

Structure of the Assets folder for the TutorialFs Unity project
  * TutorialFs

  \ * Assets

    o * Editor

    o * Gizmos

    o * Plugins

    \ * StreamingAssets

      \ * sample.cpk

 

Assets folder

 

 

CRIWARE Unity Plugin

StreamingAssets folder

Packed file

 

(3) Adding a script to a game object

CRI ADX and CRI Sofdec both provide a dedicated component, but FileMajik PRO does not. Therefore, you must add a script that will take care of the file loading to a game object.
Use the following steps to create a game object and add a script.
  1. From the menu, select Game Object -> Create Empty to create the most basic game object.
  2. In the Hierarchy view, select the game object you created.
  3. Right-click in the "Project view" to create a C# script.
    Rename the script file "LoadText."
  4. Drag and drop LoadText.cs onto the game object in the Hierarchy view to add it to the game object.

    fmpu_qstart_game_attach_mini.png
    Adding a script to the game object

(4) Editing the script

Start MonoDevelop and modify LoadText.cs as shown below.

using System.Collections;
using System.IO;
using System.Text;
public class LoadText : MonoBehaviour {
private string loadedText;
private CriFsBinder binder = null;
private uint bindId = 0;
// Called when the object becomes enabled and active
void OnEnable()
{
// Create a binder
this.binder = new CriFsBinder();
}
// Use this for initialization
IEnumerator Start () {
// Bind CPK file in StreamingAssets
CriFsBindRequest bind_request = CriFsUtility.BindCpk(
this.binder, CriWare.streamingAssetsPath + "/sample.cpk");
// Wait until end of binding
yield return bind_request.WaitForDone(this);
if (bind_request.error != null) {
yield break;
}
else {
// Store bind ID
this.bindId = bind_request.bindId;
}
// Load text data in CPK file
CriFsLoadFileRequest load_request
= CriFsUtility.LoadFile(this.binder, "sample_text.txt");
// Wait until end of loading
yield return load_request.WaitForDone(this);
if (load_request.error != null) {
yield break;
}
// Convert loaded data to text
Encoding enc = Encoding.GetEncoding("utf-8");
this.loadedText = enc.GetString(load_request.bytes);
}
// Update is called once per frame
void Update () {
}
// Display loaded text
void OnGUI() {
// Check whether the text has been loaded
if (this.loadedText == null) {
return;
}
// Draw the loaded text
GUI.Label(new Rect(0, 0, Screen.width, Screen.height), this.loadedText);
}
// Called when the object becomes disabled or inactive
void OnDisable() {
// Unbind CPK
if (this.bindId > 0) {
CriFsBinder.Unbind(this.bindId);
this.bindId = 0;
}
// Destroy binder
this.binder.Dispose();
this.binder = null;
}
}

Function of the script

The script loads the text file sample_text.txt contained in the packed file sample.cpk and displays the text.

Flow of the script

When the script is started, it prepares the CPK file and loads the text file.
Then it displays the loaded text on a label. Here are the different steps:

Description of the script

[1] Creating a binder
Create a "binder" to load a file from the CPK file.
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.
Use the OnEnable function to create a binder.

[2] Binding the CPK file
Use the CriFsUtility::BindCpk function to register the CPK file with the binder.
This function is a non-blocking function, therefore you must wait for the process to complete.
The yield statement waits for completion.

[Note]
You do not need to pack a single file into a CPK file to access it.
You do not need the binding in the script either.

[3] Loading the text file
Use the CriFsUtility::LoadFile function to load the file.
This function is also a non-blocking function, so you must also wait for the process to complete.
Again, the yield statement waits for completion.

[4] Converting the file to text data
After the file is loaded, it is converted to text data.

[5] Displaying the text
Here we display the text we loaded on a label, inside the OnGUI function.

[6] Destroying the binder
Use the OnDisable function to unbind the CPK file from the binder and destroy the binder.

Script execution screen

Click the Play button in Unity to run the game. The following screen is displayed.

fmpu_qstart_game_run_mini.png
Game execution screen