wiki.sine.space | sinespace

Demo Radio (Scripting)

From wiki.sine.space
Revision as of 00:18, 13 August 2019 by Jayden Catnip (Talk | contribs)

Jump to: navigation, search

Sinespace Radio Demo (Beginner)

In this tutorial we are going to create a radio script that allows us to play and change the streamed radio being played within a region in Sinespace. You will require a current install of Unity and our SpacePack API for this demo to work correctly Get Started.

Create a Radio Model

Firstly right click in the hierarchy and create an empty game object and set the transform to 0 0 0 and rename it to Radio Demo, this will be our container for the virtual goods component and room furniture component (more on this later), it also helps us keep our project tidy. Right click on the Radio Demo game object and create a cube and renamed it as Stream Player, this will be our radio model (you can add any model you like here).

Radio Demo 1.jpeg

While the Stream Player is selected add the following components to the game object by clicking the add component button at the bottom of the inspector :

  • Scripting runtime component (used to add scripting to a game object)
  • Shoutcast Streaming component (used to play streaming audio to the listeners)
  • Clickable Activator (Makes the game object clickable, used for the radio admin)

Create Your Radio Interface

Radio Demo 4.jpeg

Right click Radio Demo object and create a canvas and in the canvas inspector set the canvas render mode to screen space overlay, this will create a canvas in which we can add UI elements over the main screen.

Radio Demo 2.jpeg

In the canvas inspector set the UI scale mode to scale with screen size, this will scale the UI and elements dependent on the users screen resolution.

Radio Demo 3.jpeg

Right click the canvas and create a new canvas called Radio Admin and set the anchor preset to bottom centre and the rect transform width 400 and height 30, move the position to slightly above bottom centre, this will act as our container for our radio interface.

Radio Demo 5.jpeg

Right click the Radio Admin object and create an input field width 200 height 30 and position it to the left of the canvas (container), rename it to ifServerAddress. You can change the font sizes color to suit your requirements.

Radio Demo 7.jpeg

Right click the Radio Admin object and create a UI button, rename to Submit and change the width to 100 height 30, position it to the right of the input field. Using the drop down arrow to the left of the button click on the text item and change the text to Submit.

Radio Demo 8.jpeg

Select the Submit button and in the inspector locate the On Click() box, press the plus icon then drag the Stream Player. from the hierarchy to the bottom left box, click the drop down in the top right and select scripting runtime > CallScriptFunction, in the bottom right box enter UpdateRadio, this will be the script function that is called when we click on the submit button.

Radio Demo 9.jpeg

Right click the Radio Admin object and create another UI button width 100 height 30 and rename it to Close and position it to the right of the submit button, using the drop down arrow next to the button use the text field as before and change the text to Close.

Radio Demo 10.jpeg

Click on the Close button in the hierarchy and as before locate the On Click() box, press the plus icon and drag the Radio Admin object into the bottom left box, in the top right box select game object > set active (bool) and make sure the checkmark is unchecked. This will set the Radio Admin canvas inactive when the button is pressed.

Radio Demo 11.jpeg

Click on the Stream Player object in the hierarchy and locate the clickable activator we added earlier, press the plus icon in the on click event () box and drag the Radio Admin canvas to the bottom left box, in the top right drop down select game object > set active (bool) and make sure the check box is checked. This will set our radio admin as active when the radio model is clicked.

Radio Demo 12.jpeg

To finish off our admin panel select the Radio Admin canvas in the hierarchy and set it to inactive. This will hide the admin panel till the radio cube is clicked.

Radio Demo 13a.jpeg

Radio Scripting

To start with we need to add some script references that will allow us to access the components on some of the objects, While the Stream player object is hi-lighted in the hierarchy scroll down to the script runtime component in the inspector and locate the object references panel, in the size enter 2 as we will be referencing two items for use inside the script. In the first name field type Stream Player and drag the Stream Player object from the hierarchy onto the reference field (important note here scripts are case sensitive so remember the names)

Radio Demo 14.jpeg

in the second name field type ifStreamAddress and drag the ifStreamAddress object from the hierarchy onto the second reference field.

Radio Demo 15.jpeg

Now for the fun part the script, this can be typed directly into the script runtime (source code box) or you can use a script editor such as VS Code to enter the script. For the sake of visibility I will be using VS Code a tutorial on how to install this is located here Auto Complete Setup

Radio Demo 16.jpeg

Our full radio script is shown above, below I will go through the script in order that it is read and explain what it all does.

Radio Demo 17.jpeg

These two lines of make the objects and their components available within the script for example our input field text for the stream address and the shoutcast streaming component on the Stream Player. They are called from the script references that we previously set in the scripting runtime (object references) and are called from within the script with Space.Host.GetReference(“objectname”). To call them from inside the script we now only have to use StreamPlayer or ifStreamAddress.

Radio Demo 18.jpeg

Space.Host.StartCoroutine(UserJoin) - This line calls a special kind of function called a coroutine [[1]], our reason for using this is to allow as to add a small 5 second pause on the script coroutine.yield(5). We pause the script temporarily to give the user time to fully load into the region before we try to connect to the network.

Space.Network.SubscribeToNetwork(“RadioDemo”, GetStream) – This line subscribes us to space networking, once subscribed this line will listen for messages on s specific channel “RadioDemo” when a message is received the function GetStream is called. When the user first loads into the region the radio will only activate if a network message is received, therefore we add GetStream() to the bottom of the function to initiate the radio when a user joins the region.

Why do we use networking? When a user loads into the region the radio script loads “client side” meaning any changes made to the radio address will only be changed for the user who changed it, we use Sinespace networking to update (sync) the other users in the region.