LoginSignup
4
2

More than 5 years have passed since last update.

Unityをmicro:bitとのシリアル通信でコントロール(mac)

Last updated at Posted at 2018-05-14

micro:bitからUnityで作ったコンテンツを操作する手順です。
通信はシリアルになりますがmicro:bitを2台使って無線での操作にします。
bluetoothはよくわからなかったのでこの方法にしました。
↓完成形


micro:bit(操作側)

micro:bitのエディターにアクセスして、
下記スクリーンショットの様にグループの指定(なんでもいいです)と通信の際のトリガーを指定します。
アイコン表示は無くてもいいです。
送信する文字列も、文字じゃ無くてもなんでもいいです。

FireShot Capture 490 - Microsoft MakeCode for micro_bit - https___makecode.microbit.org__lang=ja.png

micro:bit(受信側)

受信側も最初にグループの指定をします。
グループ番号は送信側と合わせます。
送信側からのデータを受信したら、今度はシリアル通信で文字列を書き出す様にします。
FireShot Capture 491 - Microsoft MakeCode for micro_bit - https___makecode.microbit.org__lang=ja.png

Unity

今回はParticle SystemをPlayしてるので下記の様になります。
SerialPortの引数はmicro:bitを接続しているポート名とボーレート(115200)を指定します。
ボーレートは他の記事を参考にこの値を入れています。
※以下のコードではシリアル通信の文字列の判定はしていません。

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class Serial : MonoBehaviour
{
    SerialPort stream = new SerialPort("/dev/cu.usbmodem1422", 115200);
    public ParticleSystem p;
    // Use this for initialization
    void Start()
    {
        stream.ReadTimeout = 10;
        stream.Open();
        stream.DiscardInBuffer();

    }

    // Update is called once per frame
    void Update()
    {
        try
        {
            p.Play();
        }
        catch (System.Exception)
        {
        }
    }
}
4
2
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
4
2