CRIWARE Unity Plugin Manual  Last Updated: 2024-07-12
Click & play

Description of the sample

Overview

This sample script plays a sound when a button is clicked.

cri4u_samples_criatom_script01_main.png

Operations

  • Click on the button on the left cube
    Plays the sound assigned to the cube. Click again to repeat the playback.

  • Click on the button on the right sphere
    Plays the sound assigned to the sphere. Click again to stop the playback.


Scene information


Middleware CRI ADX (CRI Atom)
Sample Script sample: click & play
Location /CRIWARE/SDK/unity/samples/UnityProject/Assets/Scenes/criatom/script/ScriptSample01_ClickToPlay
Scene file ScriptSample01_scene_ClickToPlay.unity
Original ADX data Data: Pinball


Description of the program

This script plays the sound that has been assigned (as a CriAtomSource) to the object that is clicked.
The script plays the sound each time when the cube is clicked.
However it plays/stops the sound alternatively when the sphere is clicked.

Description of the script that plays a sound with every click

Each time the GUI button is clicked, this script requests the playback of the sound (CriAtomSource) associated with the object.
This script assumes that the sound can easily be played many times, such as a one-shot sound effect.

Script file Scripts/PlaySoundOnClick.cs
public class PlaySoundOnClick : MonoBehaviour {
private bool trigger = false;
void Start () {
}
void Update () {
if (trigger) {
CriAtomSource atomSrc = gameObject.GetComponent<CriAtomSource>();
if (atomSrc != null) {
atomSrc.Play();
}
trigger = false;
}
}
void OnGUI()
{
if (Scene_00_SampleList.ShowList == true) {
return;
}
if (Camera.main == null) {
return;
}
Scene_00_GUI.BeginGui("01/SampleMain1");
/* Set UI skin. */
GUI.skin = Scene_00_SampleList.uiSkin;
var pos = Camera.main.WorldToScreenPoint(transform.position);
pos.y = Screen.height - pos.y;
if (Scene_00_GUI.Button(new Rect(pos.x, pos.y, 150, 150), "Play")) {
trigger = true;
}
Scene_00_GUI.EndGui();
}
}

Description of the script that plays/stops sound with each click

Each time the GUI button is clicked, this script checks the playback status of the sound (CriAtomSource) associated with the object, and toggles its start or stop, depending on the current playback status.
This script assumes that the sound associated with the object is longer, such as music or narration, and that is not suitable for multiple playbacks.

Script file Scripts/PlayAndStopSoundOnClick.cs
public class PlayAndStopSoundOnClick : MonoBehaviour {
private bool trigger = false;
void Start () {
}
void Update () {
if (trigger) {
CriAtomSource atomSrc = gameObject.GetComponent<CriAtomSource>();
if (atomSrc != null) {
CriAtomSource.Status status = atomSrc.status;
if ((status == CriAtomSource.Status.Stop) || (status == CriAtomSource.Status.PlayEnd)) {
atomSrc.Play();
} else {
atomSrc.Stop();
}
}
trigger = false;
}
}
void OnGUI()
{
if (Scene_00_SampleList.ShowList == true) {
return;
}
if (Camera.main == null) {
return;
}
Scene_00_GUI.BeginGui("01/SampleMain2");
/* Set UI skin. */
GUI.skin = Scene_00_SampleList.uiSkin;
var pos = Camera.main.WorldToScreenPoint(transform.position);
pos.y = Screen.height - pos.y;
if (Scene_00_GUI.Button(new Rect(pos.x, pos.y, 150, 150), "Play/Stop")) {
trigger = true;
}
Scene_00_GUI.EndGui();
}
}