When you have multiple devices, it is quite easy to share data between them. In this lesson, see how that is done.
- Now that we've got our server running, it's time to actually do some sharing. Now assume you are working on some objects and you have a friend who is currently at the other side of the world. But you want to share what you are doing with her. If you do something to our sphere, you want her to see the effect and vice versa. So let's make that possible. In order to get sharing of data working, we need the sharing component, which is part of the HoloToolkit. You can find it in the All Prefabs.
Scroll down and find it. Pick it up and drag it on the hierarchy on the left side. We need to set some properties. If you go to the right side to the inspector, you can see that the client role is currently set to Primary. Please leave it like this. The server address is the address of our sharing server that we just installed. If you don't remember what the IP address was, you can look it up by opening the server again. Let's do that just to make sure that we got the right number.
On my network, it's currently 10.16.18.112. So we need to enter that right here. 10.16.18.112. All the rest of the properties we can leave as they are right now. Now the sharing component is one that they scale of sending messages to the server and receiving them. It doesn't really know what to do with the data. So we will take care of that right now. We need to define a message. A message that will be sent across the network when one of the users taps on that ball.
Select the sharing component. And we're going to add a new component to it. This is the one that the HoloToolkit gives us. And we're going to adapt to it. It's called Custom Messages. Click on the cog wheel and click Edit Script. Now you'll see there's a lot of things going on right here. And most of them we don't need. We're just going to change it a little bit. Scroll down until you find the TestMessageID. This is just a sample enum, but we're going to expand on this. We're going to add a drop command to it.
Next we need to find a way to send this message. So click out of the TestMessageID, go to the top, and find the SendHeadTransform. This code is a nice sample code that gives us all the information we need to send some data. Copy this whole method. Copy and then paste it underneath it. Change the name from SendHeadTransform to SendCommand. We don't need these parameters, so we can remove these.
And just replace them with the one that we just created. CustomMessages, TestMessageID, msgId. Now in the method, change the line that says CreateMessage. Currently that takes the HeadTransform. But we want to pass in the message id that we just changed in the parameter list. Remove the AppendTransform, we don't need this. And the rest will be the same as it is right now. This is the one that takes actually care of sending the message.
Close this file. So we need to do something with this CustomMessages. In our DropHelper class, go to the Start method. We need to tell the CustomMessages that whenever this message comes in we need to respond to this. And we will do so like this. CustomMessage.Instance.MessageHandlers, this takes a parameter which is the message ID that we're going to use. And we're going to be responding to the Drop.
And this will have an OnDrop method. We haven't written that one out yet but we will do so now. Add the OnDrop. Public void OnDrop. The body of this is going to be extremely simple. DoDrop false. This will take care of receiving the message. So the moment the message come in, we will call the DoDrop method. However, we also want to be able to tap on the ball so we can have it drop manually from this side on.
To do that, go to the top of the class and add a new interface, IINputClickHandler. This is part of the Toolkit so we need to reference this. Use it, and we're going to implement it. This is the method that's being called as soon as the user taps on it. Here we're going to go DoDrop true.
Right now our code doesn't compile because we haven't implemented DoDrop yet. So we will do that right now. Public void DoDrop. And now I'll explain what this true and false actually are doing. We need this to tell the system if we want to send a message or not. Which makes sense. If we tap on it, we want to send out a message on network to all the other devices that we need to do a DoDrop on that side as well. However, if we got it through a message, we don't want to send a message out again. Okay, first we're going to do the thing that we've been doing all along.
Let's turn on gravity. GetComponent Rigidbody and turn the gravity on. Now I'm going to call this method. And you probably haven't seen this one before. This is the reset method I just mentioned. Invoke calls a method with the name Reset that we gave here in the string. And we'll tell it to wait about five seconds. The net effect is that after five seconds, this reset method will be called, which we've just shown here. And it will just turn off gravity and place the ball back again.
Now the last thing we need to do, if we require the message to be sent, we will do that in our custom messages, Instance, and we'll use the SendCommand that we just created. We'll give it the command Drop. And that's all there is to it. If you are running your server and if you have multiple devices, this is about time to fire up the app on your device and see what's going on. So let's try that out. So let's see if we got everything set up correctly.
Let's scroll up. There's the DoDrop. This is the MessageHandler. Ah, I see that Visual Studio is telling me that I made a mistake. OnDrop actually will get some data, the type of the NetworkInMessage. We need to have a using statement here. So let's include that as well using HoloToolkit sharing, that looks fine. Now we're not going to be needing this message. But if you want to do something else like passing in data to all the machines, you can add that to the message as well.
Again, we're just going to do the drops. So we don't care about this here. So this looks all nice. I think we're good to go. So let's fire up the HoloLenses, deploy the apps to multiple devices and see what happens. So, I've built the app. I've deployed it to our devices. The server's up and running. So it's time to see what's going on and let's have some fun. - Okay. - Cool. - So as you can see, the ball is over there floating nicely in the air.
There's a cursor on it. James standing next to it. And James will probably tap on it. And there he goes. The ball drops, I didn't do anything. That's amazing, and it's back up, cool. This is of course exactly how things work in the real world. Somebody does something to something and then it will respond and other people are seeing the same effect. So this really adds to the level of realism. And it's extremely cool. Wouldn't you agree? - Yeah.
- So, isn't it amazing?
Join instructors Dennis Vroegop and James Ashley for this introduction for HoloLens app development. They show you how to set up your development environment, including Unity and the HoloToolkit; how to deploy to HoloLens and the HoloLens emulator; and how to build apps that accept user input via gazes, tapping, and speech. Then discover how to use spatial mapping to detect your surroundings and analyze your environment with spatial understanding. Learn how to implement spatial sound that adds to the user's existing environment, and synchronize data to create shared experiences. Along the way, learn how it all comes together in two simple 3D apps: a visualization for enterprise business and a platform game.
- Installing Unity, Visual Studio, and the HoloToolkit
- Building an app in Unity
- Deploying to HoloLens or the HoloLens emulator
- Gaze and tapping
- Spatial mapping and spatial understanding
- Spatial sound
- Shared holograms
- Creating 3D enterprise apps
- Creating 3D games