forked from microsoft/MixedRealityCompanionKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemotePlayerManager.cs
More file actions
116 lines (100 loc) · 4.59 KB
/
Copy pathRemotePlayerManager.cs
File metadata and controls
116 lines (100 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SimpleSharing
{
// Note: In this simple sharing implementation, network messages are only being sent from HoloLens clients to the Unity server.
// This means that only Unity will have a complete knowledge of the application state.
//
// If you want all clients to know about all other clients' states, you will need to
// relay all network traffic coming into this class to all other clients in the remotePlayers dictionary.
public class RemotePlayerManager : SimpleSharing.Singleton<RemotePlayerManager>
{
Dictionary<int, GameObject> remotePlayers = new Dictionary<int, GameObject>();
// Simple example showing how to respond to remote user's interaction.
// See LocalPlayerManager.
GameObject placedObject = null;
public AudioClip placeSound;
AudioSource placeSource;
private void Awake()
{
placeSource = GetComponent<AudioSource>();
if (placeSource == null)
{
placeSource = gameObject.AddComponent<AudioSource>();
}
}
private void AddRemotePlayer(int connectionID)
{
// In this simple example, this will add a cube to the remote player's head.
// For more robust remote player behavior, consider adding a RemotePlayer class to this GO and destroying the cube.
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
go.transform.localScale = Vector3.one * 0.2f;
go.name = "RemotePlayer_" + connectionID.ToString();
// Child remote players to the shared anchored object if available.
if (AnchorManager.Instance != null && AnchorManager.Instance.objectToAnchor != null)
{
go.transform.SetParent(AnchorManager.Instance.objectToAnchor.transform);
}
else
{
// Otherwise child remote players to this gameObject, which shares a transform to the AnchorManager.
go.transform.SetParent(gameObject.transform);
}
// All remote interaction will be keyed off of the connectionID.
remotePlayers.Add(connectionID, go);
}
public void UpdateRemotePlayer(int connectionID, Vector3 position, Quaternion rotation)
{
if (!remotePlayers.ContainsKey(connectionID))
{
AddRemotePlayer(connectionID);
}
remotePlayers[connectionID].transform.localPosition = position;
remotePlayers[connectionID].transform.localRotation = rotation;
}
public void RemoveRemotePlayer(int connectionID)
{
if (remotePlayers.ContainsKey(connectionID))
{
GameObject go = remotePlayers[connectionID];
remotePlayers.Remove(connectionID);
DestroyImmediate(go);
}
}
// Respond to remote player input here.
// This will be called if a remote player's airtap successfully collided with any geometry.
public void PerformRemoteAirTap(int connectionID, Vector3 airTapLocation, Vector3 airTapDirection, Vector3 airTapHitLocation)
{
Debug.Log(String.Format("Client {0} performed air tap at location {1}, which hit location {2}", connectionID, airTapLocation.ToString(), airTapHitLocation.ToString()));
if (AnchorManager.Instance != null
&& AnchorManager.Instance.objectToAnchor != null)
{
if (placedObject == null)
{
placedObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);
placedObject.transform.parent = AnchorManager.Instance.objectToAnchor.transform;
placedObject.transform.localScale = Vector3.one * 0.2f;
}
placedObject.transform.localPosition = airTapHitLocation;
if (placeSound != null && !placeSource.isPlaying)
{
placeSource.PlayOneShot(placeSound);
}
}
}
private void Update()
{
if (Input.GetKey(KeyCode.Space))
{
if (placeSound != null && !placeSource.isPlaying)
{
placeSource.PlayOneShot(placeSound);
}
}
}
}
}