2
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.

【Unity】toioのSimulatorをUnityEditorでのみ読み込む

Posted at

本記事について

Sample_ConnectTypeのサンプルでは、ConnectTypeを切り替えることによって、
UnityEditorではSimulator, iOSアプリでは実機のCubeと切り替えることが可能。

ただ、iOSアプリでSimulatorを動かさないのであればそもそもCubeやStageをSceneに置きたくない。
=> SimulatorをSceneごと分離できるかどうかやってみよう

環境

mac os 12.5 silicon
toio SDK for Unity v1.5.1
Unity 2021.3.7f1

結論

スクリーンショット 2022-12-07 20.09.59.png

特に問題なくUnityEditorではSimulation, iosアプリではSimulationSceneをロードせずに実機Cubeを動かすことができた。

別シーンのCubeに接続できるか調査

ドキュメント 3.1. 検索(Scanner)

toioの検索はScannerクラスが行なっているとのこと。

シミュレータ実装:
シミュレータのCubeプレハブから作成された GameObject を検索

とあるのでその具体的な部分をコードから読み解く。

検索箇所

Object.FindObjectsOfTypeを使ってるので、別シーンからでも読めるはず。
(toio SDKではGameObject.FindObjectsOfTypeと書いているけれど, GameObjectで定義されたクラスメソッドではないため本記事ではObject.FindObjectsOfTypeと呼ぶ)

シーンの用意

2つのシーンを作る

ToioScene: CubeManagerを置く。
SimulationCubeScene: StageとCubeを置く。

SimulationCubeScene

Sample_ConnectType.sceneをコピー
「scene」オブジェクトについてる「Sample_ConnectType」コンポーネントを外すor非アクティブにする。
StageにCameraとEventSystemがあるが、これらはSimulationCubeSceneに配置したくないのでStageから消したい。
が、オリジナルのプレハブを変えると他のSample動かなくなるため、Stageプレハブを別フォルダにコピーしてから編集。

スクリーンショット 2022-12-07 20.46.54.png

これでSimulation側の準備OK

ToioScene

  1. SceneロードしたらSimulationCubeSceneを追加
  2. CubeManager起動

実際に私が動かしたコードではなく、本記事用に書いたコードだが多分以下のような感じで動く

.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using toio;
using UnityEngine.SceneManagement;
using Cysharp.Threading.Tasks;

public class ToioSceneSample : MonoBehaviour
{
    public ConnectType connectType;

    CubeManager cm;
    async void Start()
    {

#if UNITY_EDITOR
            await SceneManager.LoadSceneAsync("SimulationCubeScene", LoadSceneMode.Additive);
#endif

        // ConnectType.Auto - ビルド対象に応じて内部実装が自動的に変わる
        // ConnectType.Simulator - ビルド対象に関わらずシミュレータのキューブで動作する
        // ConnectType.Real - ビルド対象に関わらずリアル(現実)のキューブで動作する
        cm = new CubeManager(connectType);
        await cm.MultiConnect(2);
    }

    void Update()
    {
        foreach(var cube in cm.syncCubes)
        {
            cube.Move(50, -50, 100);
        }
    }
}

注意点

SimulationSceneを読み込まないのはできたけど、toio SDKがResourcesを使ってるのでこれらのリソースが全てアプリに入ってしまう。
Resources for simulator included in build even if simulator not used. シミュレータ使わないのに関連ファイルがビルドに含まれる #262

ビルド時にフォルダ名変えて差分多く出るのは好みじゃないので、
現状手っ取り早く対応するなら「Simulatorを使わない」が一番な気はする。

2
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
2
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?