Last active
April 7, 2018 08:35
-
-
Save TakahiroMiyaura/c8b45f67e2db03cdfec232323aa6de77 to your computer and use it in GitHub Desktop.
Mixed Reality Toolkit - Unity で始めるHoloLensとImmersiveHMD連携 ~ 両デバイスを1つのアプリで作る基本 ref: https://qiita.com/miyaura/items/6e70c048f0270cf296a3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Updateメソッド内で呼び出される自分自身のプレーヤーの制御に使用 | |
public abstract void LocalPlayerUpdate(); | |
//自分自身のプレーヤーの初期化処理で使用 | |
public abstract void InitializeLocalPlayer(); | |
//他のプレーヤーの初期化処理で使用 | |
public abstract void InitializeRemotePlayer(); | |
//子クラスがどのデバイスの時の処理をサポートするかを取得するプロパティ | |
public abstract MixedRealityCameraManager.DisplayType SupportDisplayType { get; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[RequireComponent(typeof(SamplePlayerController))] | |
public class HoloLensController : DeviceControllerBase, IInputClickHandler | |
{ | |
private MixedRealityCameraManager.DisplayType _supportDisplayType; | |
private SamplePlayerController PlayerNetworkBehaviour | |
{ | |
get { return (SamplePlayerController) GetPlayerNetworkBehaviour(); } | |
} | |
public override void LocalPlayerUpdate() | |
{ | |
transform.position = CameraCache.Main.transform.position; | |
transform.rotation = CameraCache.Main.transform.rotation; | |
} | |
public override void InitializeLocalPlayer() | |
{ | |
CreatePlayerObject(Color.blue); | |
} | |
public override void InitializeRemotePlayer() | |
{ | |
CreatePlayerObject(Color.red); | |
} | |
public override MixedRealityCameraManager.DisplayType SupportDisplayType | |
{ | |
//HoloLensをサポートしている列挙値(MixedRealityCameraManager.DisplayType.Transparent)を返す | |
get { return MixedRealityCameraManager.DisplayType.Transparent; } | |
} | |
public void OnInputClicked(InputClickedEventData eventData) | |
{ | |
if (PlayerNetworkBehaviour.isLocalPlayer) PlayerNetworkBehaviour.CmdFire(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[RequireComponent(typeof(SamplePlayerController))] | |
public class ImmersiveHMDController : DeviceControllerBase, IInputClickHandler | |
{ | |
private MixedRealityCameraManager.DisplayType _supportDisplayType; | |
private SamplePlayerController PlayerNetworkBehaviour | |
{ | |
get { return (SamplePlayerController) GetPlayerNetworkBehaviour(); } | |
} | |
public override void LocalPlayerUpdate() | |
{ | |
transform.position = CameraCache.Main.transform.position; | |
transform.rotation = CameraCache.Main.transform.rotation; | |
} | |
public override void InitializeLocalPlayer() | |
{ | |
CreatePlayerObject(Color.blue); | |
} | |
public override void InitializeRemotePlayer() | |
{ | |
CreatePlayerObject(Color.red); | |
} | |
public override MixedRealityCameraManager.DisplayType SupportDisplayType | |
{ | |
get { return MixedRealityCameraManager.DisplayType.Opaque; } | |
} | |
public void OnInputClicked(InputClickedEventData eventData) | |
{ | |
if (PlayerNetworkBehaviour.isLocalPlayer) PlayerNetworkBehaviour.CmdFire(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[SyncVar(hook = "CurrentDisplayTypeChanged")] | |
private MixedRealityCameraManager.DisplayType _currentDisplayType; | |
private void CurrentDisplayTypeChanged(MixedRealityCameraManager.DisplayType update) | |
{ | |
Debug.LogFormat("CurrentDisplayType changing from {0} to {1}", _currentDisplayType, update); | |
_currentDisplayType = update; | |
_refreshDeviceController = true; | |
} | |
private void Update() | |
{ | |
//対象デバイスが変更されている場合は使用するDeviceContollerの再設定を行う。 | |
if (_refreshDeviceController) | |
{ | |
SetDeviceController(); | |
_refreshDeviceController = false; | |
} | |
// SamplePlayerControllerが他のプレーヤで同期されている場合は座標を調整する。 | |
if (!isLocalPlayer && string.IsNullOrEmpty(_playerName) == false) | |
{ | |
transform.localPosition = Vector3.Lerp(transform.localPosition, _localPosition, 0.3f); | |
transform.localRotation = _localRotation; | |
return; | |
} | |
if (!isLocalPlayer) return; | |
// もしアンカーの状態が変更されていればすべてに通知する。 | |
if (_anchorEstablished != _anchorManager.AnchorEstablished) | |
CmdSendAnchorEstablished(_anchorManager.AnchorEstablished); | |
// アンカーの設置が未完了の場合座標の更新は行わない。 | |
if (_anchorEstablished == false) | |
{ | |
return; | |
} | |
//DeviceContollerを経由してローカルプレーヤの更新を行います。 | |
_deviceController.LocalPlayerUpdate(); | |
// 変更した座標情報を他のプレーヤーに通知します。 | |
CmdTransform(transform.localPosition, transform.localRotation); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment