Filling in the blanks

In this section, we will finally make sure that our network code can understand our object by spawning it with network-friendly code. Instead of turning our skee object on or off based on certain conditions, we will spawn it into the game using the PrefabSpawnManager.

The PrefabSpawnManager class takes a Prefab and converts it into data types that can easily be translated across a network. This makes for a far more efficient use of objects:

To begin, click on that Add Component button shown in the preceding screenshot. Scroll down to New Script, type SyncSpawnSkee in the box, make sure that C Sharp is selected, and click on the Create and Add button.

Double-click on the file you have just created, and when it opens, type this short section of code into the editor:

In the SyncSpawnSkeeBallMachine() method, we first create a SyncSpawnedObject called  spawnedObject. We then get the position to spawn the object. In this case, we are using the actual position stored in the Prefab itself.

Next is our actual spawn line:

SpawnManager.Spawn(spawnedObject, position, skeePrefab.gameObject.transform.rotation, null, "skee", true);
spawnedObject = Our Prefab;
position = the position to spawn the object;
skeePrefab.gameObject.transform.rotation = the prefabs rotation;
null = parent to assign to the object;
"skee" = baseName;
True = isOwnedLocally;

After that, we link it up so that the ApplicationManager has a direct reference to the spawnedObject. We then get a reference to the spawnedObject inside this class, so we can make sure that the object does not have a parent and change the object name to skee. The system takes the basename and applies it to a process so that this comes out:

"skee - 23481325423 - ff39457315fh- fneu2941".  I wanted something simple.

Alright, now that we have all of our coding done, it's time to wire up the object that we have just set up and see if this thing works:

Yeah, that is a lot of missing stuff. Not a problem. This should not take more than a few minutes to fix. First and foremost, skip the Skeeball machine; that will be filled in dynamically.

Next, use the following steps to fill in the missing details of the Application Manager:

  1. For the Sharing Prefab, drag the Sharing object from the Hierarchy view to this field.
  2. For the Spatial Prefab, drag the Spatial Mapping object from the Hierarchy view to the field.
  3. If for some reason you are getting the missing Prefab error for the Debug Text Mesh, refer to the preceding chapter for that.

Lastly, use the following steps for SyncSpawnSkee:

  1. Drag the skee Prefab from the Project view to the skee Prefab field.
  2. Drag the Sharing object from the Hierarchy view to the Spawn Manager field.

That is it.

Yeah, it does look like it's missing something, but as I said, some of it is handled dynamically:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.253.170