CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
[CriFs] Loading a file

Description of the sample

Overview

cri4u_samples_crifs_scene01_game_mini.png

This sample simply loads a file.

Operations

  • Loading Style: ORDINARY and Loading Style: COROUTINE buttons
    They specify whether to use an ordinary method (Ordinary) or a coroutine method (Coroutine) to load a file.

  • Load Text File button
    Loads and displays a local text file.

  • Load Image File (Local) button
    Loads and displays a local image file.

  • Stop Loading button
    If "Ordinary" is selected, you can use this button to stop loading the file.

  • Reset button
    Clears the loaded items.

Scene information


Middleware FileMajik PRO (CRI File System)
Sample Basic samples
Location /CRIWARE/SDK/unity/samples/UnityProject/Assets/Scenes/crifilesystem/basic/
Scene file Scene_01_LoadFile.unity


Description of the program

Ordinary method

This method checks whether the loading is complete by polling periodically (i.e. when each frame is updated).
It is used in developing traditional consumer games.

Coroutine method

This method uses a coroutine to wait for the loading to complete with the yield statement.
It can simplify the loading process.
Loading with the coroutine method
private IEnumerator LoadTextFile(string path)
{
/* Issue a request for file loading. */
var request = CriFsUtility.LoadFile(path);
}

The code above requests the loading of a file.
The loading process itself is executed asynchronously in the background.

/* Sleep until the processing is completed . */
yield return request.WaitForDone(this);

The code above waits until the loading is complete.

if (request.error == null)
{
/* The request.bytes stores the loaded data. */
using (var st = new StreamReader(new MemoryStream(request.bytes), Encoding.UTF8)) {
this.loadedText = st.ReadToEnd();
}
}

If the error is null, loading was successful.
If a text file has been loaded, we change the content of the strings.

/* Check whether the file loading is successful. */
if (request.error == null)
{
this.textures[n] = new Texture2D(0, 0);
/* The request.bytes stores the loaded data. */
this.textures[n].LoadImage(request.bytes);
}

If an image file has been loaded, load the content into a texture.