クリック&プレイ

サンプル内容

概要

ボタンをクリックしてサウンドを再生するスクリプトのサンプルです。

cri4u_samples_criatom_script01_main.png

操作方法

シーン情報


ミドルウェア CRI ADX2 (CRI Atom)
サンプル Scriptサンプル:クリック&プレイ
格納場所 /cri/unity/samples/UnityProject/Assets/Scenes/criatom/script/ScriptSample01_ClickToPlay/Scenes
シーンファイル ScriptSample01_scene_ClickToPlay.unity
ADX2データのオリジナル データ:ピンボール


プログラムの解説

オブジェクトに音(CriAtomSource)を貼り付けておいて、クリックに合わせて再生制御するスクリプトのサンプルです。
左側のキューブにはクリックに合わせて連続で音を再生するスクリプトが貼ってあります。
右側のスフィアにはクリックに合わせて音楽を再生開始/再生停止するスクリプトが貼ってあります。

クリックで再生するスクリプトの説明

GUIボタンがクリックされた際に、オブジェクトに貼ってある音(CriAtomSource)に再生要求を出すスクリプトです。
ワンショットの効果音など多数再生される音を想定しています。

スクリプトファイル: 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();
    }
}

クリックで再生開始/停止するスクリプトの説明

GUIボタンがクリックされた際に、オブジェクトに貼ってある音(CriAtomSource)の再生状態を調べて、再生中なら停止を実行し、停止中なら再生開始を実行するスクリプトです。
音楽やセリフなど多重再生を行わない長尺のサウンド再生を想定しています。

スクリプトファイル: 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();
    }
}


CRI Middleware logo Copyright (c) 2012-2018 CRI Middleware Co., Ltd. CRI ADX2 LE マニュアル (for Unity) SDKVer.2.10版