0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MIDIのイベントに合わせてキューブが落ちるようにした

Posted at

最終的にはMRピアノを再現したくその過程でやったことをまとめました。
MIDIファイルも用意してますが、Google Driveに保存してるため何かの拍子で場合によっては消えている可能性があります。その場合は申し訳ないですが自前で用意してください。

1.MIDI Animation Track for Unity Timelineを導入

UnityのPacageの中にあるmanifest.jsonに以下を書き込む
image.png

{
   "scopedRegistries": [
    {
      "name": "Keijiro",
      "url": "https://registry.npmjs.com",
      "scopes": [
        "jp.keijiro"
      ]
    }
  ],
  "dependencies": {
    "jp.keijiro.klak.timeline.midi": "1.0.5",
    ...
  }
} 

こんな感じに書きこみます。
image.png

成功すると以下の画面が出ます

image.png

これで導入は完了です。
PacageManagerにも無事追加されてますね
image.png

2.キューブが落ちるスクリプトを書く

down.csのファイル名で以下のコードをコピペする

down.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class down : MonoBehaviour
{
    //キューブの速度はここで操作
    float speed = 0.01f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(0,-speed,0);
    }
}

キューブにアタッチする。

実行結果

3.キューブを生成する関数を作る

空のオブジェクト(CubeSet)を生成する

以下のコードをアタッチする

Cube.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cube : MonoBehaviour
{
    [SerializeField] GameObject cubePrefab;
    // Start is called before the first frame update
    void Start()
    {
        //プレハブを生成
        CubeSet();
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            CubeSet();
        }
    }

    public void CubeSet()
    {
        //プレハブを生成
        //Instantiate(生成したいもの, 場所,角度);
        Instantiate(cubePrefab, new Vector3(0,10,0),Quaternion.identity);//Quaternion.identity要は無回転
    }
}

インスペクターにプレハブをセット(忘れがちなんでちゃんとやりましょう)
image.png

4.タイムラインを作成

右クリックからTimelineを生成し、ダブルクリックする
image.png
こんな画面になる

image.png

空のオブジェクト(Timeline)を生成し、先ほど生成したTimelineをアタッチ、プラスでAudioSoruceのコンポーネントも追加
image.png

mp3,Timeine(空のオブジェクト),MIDIファイルそれぞれをドロップアンドドロップ
image.png
MIDIとmp3の長さの比率はなんかおかしかったので僕はこんな感じにしました
image.png
↓今回使用したファイル

<MIDIファイルを自前で用意した方へ>

MIDIファイルだけでなくmp3ファイルも必要なので以下のサイトで用意してください
(mp4ではなくmp3)

MIDIファイルとmp3を追加すると以下のようになる
※僕はDominoで自作した関係上トラックが18個あるので18個あります。実際に打ったのは3つ目のトラックなので赤で囲ったものを使います。
image.png

5.MIDI Signal Receiver(MIDIのイベントを取得)を作成

空のオブジェクト(MIDI Signal Receiver)を生成

MIDI Signal Receiverのコンポーネントと以下のコードをアタッチ

NoteInstance.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NoteInstance : MonoBehaviour
{
    [SerializeField] Cube cube = default;
    public void NoteEvent()
    {
        Debug.Log("音が鳴りました");
        cube.CubeSet();
    }
}

このようになる(※Note On Eventの+を押した)
image.png

次にMIDI Signal Receiverの設定をいじる。

image.png

MIDI Signal Receiverをアタッチ
image.png

CubeSetをMIDI Signal ReceiverのインスペクターのNoteInstance.csアタッチ
image.png

6.実行結果

少しずれてますが大体やりたいことはできたと思います

参考記事、動画

↓2~6の動画にお世話になりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?