Unity(Ver4.5)で使用するdllファイル(C#)を、Visual Studio Express 2013でクラスライブラリとして作成する方法を、「Unityで使用するC#からdllファイルの作成」で説明しました。今回は、Unity(Editor Ver:2022.3.11f1)で使用するdllファイル(C#)を、Visual Studio 2022でクラスライブラリとして作成する方法を説明します。
今回作成するアプリは、Unityを使ってBluetoothLEデバイスからのアドバタイズパケットを受信して接続します。
Unityでの「Windows.Foundation.UniversalApiContract」の参照不可のエラーが発生して、コンパイルができませんでした。原因は不明。
Visual Studio 2022によるプロジェクトの作成
Visual Studio 2022を起動し、「新しいプロジェクトの作成」をクリックし、次のようにC#クラスライブラリを選択します。
表示された「新しいプロジェクトを作成します」ダイアログでプロジェクト名を「TestScan」とし、次の「追加情報」ダイアログでフレームワーク「.NET Standard2.1」を選択し、新規プロジェクトを作成します。
UWP用APIが使えるアドオン「Microsoft.Windows.SDK.Contracts」」のインストール
ツールメニューから「NuGetパッケージマネージャ」→「ソリューションのNuGetパッケージの管理」を選択して、NuGetパッケージの管理画面を表示します。
NuGetパッケージの管理画面から、「Microsoft.Windows.SDK.Contracts」を検索してインストールします。インストールが完了すると、ソリューションエクスプローラの依存関係に「Microsoft.Windows.SDK.Contracts」が表示されます。
Windows10のBLE周りのAPIはUWPアプリ専用のため、.NET Frameworkからは、WinRTのAPI「Microsoft.Windows.SDK.Contracts」を経由してアクセスする必要があります。
UnityEngine.DLLの参照設定
Unity(Editor Ver:2022.3.11f1)のUnityEngine.DLLは、「c:\Program Files\2022.3.11f1\Editor\Data\Managed」に置かれています。これを新規に作成したプロジェクト内「 ・・Project\TestScan\TestScan」に置いて、Visual Studio 2022から参照します。
参照するためには、Visual Studio 2022のソリューションエクスプローラーの依存関係を右クリックし、参照の対かを選択して、先ほどディスクトップに置いたUnityEngine.DLLを選択します。
正常に参照できると、次のように参照設定の表示に、「UnityEngine」が追加されます。
アドバタイズメントスキャンアプリの作成
C# .NET Frameworkのコンソールアプリで作成したアドバタイズメントスキャンプログラムは、BluetoothLEデバイスからのアドバタイズパケットを受信して接続します。アドバタイズメントスキャンプログラムを次に示します。
using System;
using System.Threading;
using Windows.Devices.Bluetooth.Advertisement;
using UnityEngine;
public class TestScan : MonoBehaviour
{
BluetoothLEAdvertisementWatcher? watcher;
public void Start()
{
UnityEngine.Debug.LogWarning("Start");
watcher = new BluetoothLEAdvertisementWatcher();
watcher.Received += Watcher_Received;
watcher.ScanningMode = BluetoothLEScanningMode.Passive;
watcher.Start();
Thread.Sleep(60000);
watcher.Stop();
UnityEngine.Debug.LogWarning("Stop");
}
public void Watcher_Received(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
UnityEngine.Debug.LogWarning("---Received---");
var bleServiceUUIDs = args.Advertisement.ServiceUuids;
UnityEngine.Debug.LogWarning("Found");
UnityEngine.Debug.LogWarning("MAC:" + args.BluetoothAddress.ToString());
UnityEngine.Debug.LogWarning("NAME:" + args.Advertisement.LocalName.ToString());
UnityEngine.Debug.LogWarning("ServiceUuid");
foreach (var uuidone in bleServiceUUIDs)
{
UnityEngine.Debug.LogWarning(uuidone.ToString());
}
UnityEngine.Debug.LogWarning("---END---");
UnityEngine.Debug.LogWarning("");
}
}
コンパイルすると「Unity Projects\SerialHandler\SerialHandler\bin\Debug」に「SerialHandler.dll」が作成されます。
namespaceは外します。namespaceを付与したときはUnityで次のエラーが表示されます。
dllファイルをUnityへ登録
作成された「SerialHandler.dll」を、次のようにunityのProjectのAssetsにトラッグします。
Unity で.NET Standard2.1 を使用する
「Edit」→「Project Setting」を選択します。
表示されたダイアログで、「Player」→「Configuration」→「Api Compatibility Le..」を選択して、「.NET Standard2.1 」を選択します。
Unityのサンプルコードの作成
作成したUnityのサンプルコードを次に示します。
using UnityEngine;
public class TestScanSampleCode : MonoBehaviour
{
private TestScan m_TestScan;
void Start()
{
UnityEngine.Debug.LogWarning("start");
m_TestScan = gameObject.AddComponent<TestScan>();
m_TestScan.Start();
}
}
Unityのサンプルコードのコンパイル
Unityで作成したサンプルコードをコンパイルすると、次のエラーが発生します。
- Assembly ‘Library/ScriptAssemblies/Assembly-CSharp.dll’ will not be loaded due to errors:
Reference has errors ‘TestScan’. - Assembly ‘Assets/TestScan.dll’ will not be loaded due to errors:
Unable to resolve reference ‘Windows.Foundation.UniversalApiContract’. Is the assembly missing or incompatible with the current platform?
Reference validation can be disabled in the Plugin Inspector.





