LoginSignup
1
0

More than 5 years have passed since last update.

UnlimitedHandをC#プロジェクトで使う

Last updated at Posted at 2016-11-23

Goal

一般的なC#プロジェクトでUnlimitedHandを制御する。
以下ではWPFプロジェクトで書いていますが、WindowsFormでもコンソールでも同じです。

Reference

いつものリファレンス
http://reference.unlimitedhand.com/ja/unity/

Shrinkwrap

今回も2016.11.20のバージョンです。
Unity用プラグインver.0041b、Arduino用プラグインver.0043αを使用しました。
初期設定などは[UnlimitedHandをUnityで使う]ページを参照してください

Procedure

WPFプロジェクトを作成、UH.csを追加し、以下の修正を行う

  • using UnityEngineを削除
  • MonoBehaviourの継承を削除
  • Debug.LogをDebug.WriteLineとかに置換する
  • guessPortName関数はWindowsでいいと思う。特殊なこと(Xamarinとか?)するならよしなに
  • DontDestroyOnLoadはコメントアウトでよい
  • Awakeをpublicに

あとは以下のように。

MainWindow.xaml
<Window x:Class="uh_wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:uh_wpf"
        Title="MainWindow" Height="350" Width="525" Closing="WindowClosing">
    <Grid>
        <Button Content="Stimulate0" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Stimulate1Click"/>
    </Grid>
</Window>

Closingイベントとボタンを追加しただけ。

MainWindow.xaml.cs
using System.Windows;

namespace uh_wpf
{
    public partial class MainWindow : Window
    {
        UH uhand;
        public MainWindow()
        {
            InitializeComponent();
            uhand = new UH();
            uhand.Awake();
        }

        private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            uhand.Disconnect();
        }

        private void Stimulate1Click(object sender, RoutedEventArgs e)
        {
            uhand.stimulate(0);
        }
    }
}

UHオブジェクトの制御とボタンクリック時に刺激を流すだけ。
MonoBehaviour使わない分、初期化・開放処理を自分でやらなきゃですが、それ以外はUnityで使うのと変わらないです。
まぁUH.csでやっていることも、ただシリアル通信やっているだけですから。

1
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
1
0