CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Embedding movie data into a game
This section describes how to embed a movie file created with the Sofdec tool into a game and play back the file.

Create a new Unity project and place the object for movie playback with CRI Sofdec into a scene.
Here are the steps.
The procedure to create a new project and import a plugin package is the same than the one described in Embedding data into a game for CRI ADX.
If you have already used "CRI ADX", you can start with (3) Copying a movie file .


(1) Creating a Unity project

(1-1) Creating a Unity project
Start Unity and create an empty project.
In this tutorial, the "TutorialMana" folder is used as the project folder.
  • Creating a new project in the Project Wizard window
    From the File menu, select New Project.
    The Project Wizard window appears. Specify the folder where you want to save your project.
    Do not select any check boxes from the Import the following packages list.
    Finally, click the Create Project button to create the empty project.

    sfd2u_qstart_game_newproj_mini.png
    [Project Wizard]
(1-2) Saving a scene
After the new project is created, a scene appears. It only contains the "Main Camera".
Let's save the scene.
In this tutorial, the name of the scene will simply be "NewScene".
  • Using the Save Scene dialog box
    From the File menu, select Save Scene.
    The Save Scene dialog box appears. Specify the name of the scene.

    sfd2u_qstart_game_newscene_mini.png
    After an empty scene is created
[Note]
Here, the coordinates of Main Camera are (0, 1, -10).

(2) Importing a plugin package

Now, let's import the "CRIWARE Unity Plugin package" into the project.
The plugin package file is at the following location in the SDK:

  * cri

  \ * unity

    \ * plugin

      \ * criware_unity_plugin_smartphone.unitypackage

 

 

 

 

 

  • Importing the custom package
    From the Assets -> Import Package menu, select the Custom Package menu command.
    A file dialog box appears. Specify the plugin package file above and import it.

(3) Copying a movie file

(3-1) Preparing a file
In this tutorial, we will use "sample_256x256.usm", some movie data encoded with CRI Sofdec.

Location of the sample data
  * cri

  \ * unity

    \ * samples

      \ * crimana

        \ * basic

          \ * Assets

            \ * StreamingAssets

              \ * sample_256x256.usm

 

 

 

CRI Sofdec sample

 

 

 

Movie file

 

(3-2) Copying the file to StreamingAssets
Copy the movie file (USM file) to the "StreamingAssets" subfolder of the Assets folder of the Unity project.

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

  \ * Assets

    o * Editor

    o * Gizmos

    o * Plugins

    o * Resources

    \ * StreamingAssets

      \ * sample_256x256.usm

 

Assets folder

 

 

CRIWARE Unity Plugins

Shader resources for CRI Mana

StreamingAssets folder

USM file

 

Attention
If you want to handle USM files as CRI assets, see the Asset Support plug-ins page below.
Once the data copy is completed, the "Project view" in Unity is refreshed and looks like this:

sfd2u_qstart_game_datafiles_mini.png
Data copy is completed

(4) Creating the CRIWARE Library Initializer

Add the overall initialization module for CRIWARE and the error handling module before starting movie playback control.
Adding the initialization module is indispensable. If it is not added, playback is not performed properly.
Execute [Create CRIWARE Library Initializer] and [Create CRIWARE Error Handler] commands of [CRI] menu from the Unity menu bar, respectively.
After executing these commands, CriWareInitializer and CriWareErrorHandler will appear in the Hierarchy window.
At this time, there are no needs to change their options.

adx2u_qstart_game_initializer_hierarchy.png
After CRIWARE Library Initializer is created

(5) Creating a CriManaMovieController

The "CriManaMovieController component" is used to control movie playback in CRI Sofdec.
A CriWare.CriManaMovieController component must be provided for each movie to play.
In this sample, let's play a movie on a cube.

(5-1) Adding a CriManaMovieController component to a cube
Use the steps below to add a CriManaMovieController component to a 3D object cube.
  1. From the menu, select GameObject -> 3D Object -> Cube to create a cube.
  2. Select the cube in the "Hierarchy view".
  3. From the menu, select Component -> CRIWARE -> [CriManaMovieController .

(5-2) Specifying the movie file to play
In the "Inspector", you can set the movie playback parameters for the CriManaMovieController component.
Specify the name of the movie file to play and the movie playback method (automatic playback) as shown below.
  1. Select the cube in the "Hierarchy view".
  2. In the Movie Path field of the CriManaMovieController component, enter the name of the movie file, "sample_256x256.usm".
    (It is assumed that the movie file is in the StreamingAssets folder.)
  3. To play the movie automatically, select the Play On Start check box.

(5-3) Playing the movie
Now you are ready to play the movie.
Click on the Play button in Unity to play the movie.

(6) Controlling movie playback with a script

So far, only basic movie playback has been provided. Now, let's add a script to control the movie playback.
We will add a script that pauses the movie when clicking on the cube with the mouse.
(6-1) Adding a script that controls the movie playback on the cube
Use the steps below to add a script to the cube.
  1. Select the cube in the "Hierarchy view".
  2. Right-click in the "Project view" to create a C# script.
    Rename the file "MoviePause".
  3. Drag and drop MoviePause.cs in the "Hierarchy view" to add it to the cube.

    sfd2u_qstart_game_add_pause_mini.png
    Adding a script to the cube

Start MonoDevelop and modify MoviePause.cs as shown below.

public class MoviePause : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
private int Yaw_Speed = 10;
private int Pitch_Speed = 0;
private int Roll_Speed = 10;
void Update () {
// Rotate the Cube while the pause status is false
if (pause_stat == false) {
transform.Rotate(Vector3.down * Time.deltaTime * Yaw_Speed);
transform.Rotate(Vector3.right * Time.deltaTime * Pitch_Speed);
transform.Rotate(Vector3.forward * Time.deltaTime * Roll_Speed);
}
}
// Called when the mouse button has been pressed
private bool pause_stat = false;
void OnMouseDown() {
// Toggle the pause status
pause_stat = (pause_stat == false)?true:false;
// Get the CriManaMovieController component
CriManaMovieController mc = GetComponent<CriManaMovieController>();;
// Pause the movie playback
mc.player.Pause(pause_stat);
}
}

OnMouseDown function
The OnMouseDown function is called when the mouse button is clicked.
The script obtains the CriManaMovieController component assigned to the object and calls the Pause function from the player member.
If the argument passed to the Pause function is true, playback is paused. If it is false, playback is resumed.

Update function
This function is called for each frame.
The script rotates the cube while playing the movie.

(6-2) Executing the game
Click the Play button in Unity to play the movie.
The movie we placed on the cube is automatically played, and the cube rotates slowly.
Every time you click the cube, the movie is paused or resumed.

sfd2u_qstart_game_rotating_mini.png
The cube on which the movie is playing rotates