Unity DataStreams Guide
This guide was written as a quick reference step by step guide for setting up a simple Unity datastream. For more in depth information on each step please check the documentation at https://www.genvidtech.com/for-developers/sdk-latest/
Download Clock Assets
Download the folder containing the assets for this guide
Import “clock_face” (or import the entire pre-made clock)
Clock Setup
If you imported the entire pre-made clock then skip this section after placing the clock in the level and making sure it works correctly
Create an Empty Object named “Clock”
Make sure it’s in view of the main camera
Create an Empty Object named “Face” that’s a child of “Clock”
Add a component of type “Sprite Renderer”
Drag “clock_face” into the “Sprite” property. Note you may have to change the texture type on “clock_face” to Sprite.
Create 3 more Empty Objects that are children of “Clock”.
“HourHand”, “MinuteHand”, and “SecondHand”
Create 3 Cube objects that are children of each hand named “HourHandCube”, “MinuteHandCube”, and “SecondHandCube”
In each cubes component “MeshRenderer” replace Element 0 with “BlackDefaultMaterial”
You may need to create a black material yourself to apply
Change “HourHand” rotation to 0, 0, 270
Change “SecondHand” rotation to 0, 0, 315
Change “HourHandCube” position to 0, .75, 0 and Scale to .15, 1.5, 1
Change “MinuteHandCube” position to 0, 1, 0 and Scale to .1, 2, 1
Change “SecondHandCube” position to 0, .5, 0 and Scale to .075, 1, 1
Clock in Scene
On “Clock” create a new script component named “Clock”
Declare 3 public GameObjects “HourHand”, “MinuteHand”, and “SecondHand”
Drag the objects with the same names into these properties
Create an Update() function
Get the current time in hours, minutes and seconds then rotate the corresponding hand to the correct position based on these numbers
void Update() { DateTime now = DateTime.Now; float hour = float.Parse(now.ToString("%h")); float minute = (float) now.Minute; float second = (float) now.Second; float millisecond = (float) now.Millisecond; float smoothHour = hour + minute / 60; float smoothMinute = minute + second / 60; float smoothSecond = second + millisecond / 1000; float hourAngle = getAngle(0, 12, smoothHour - 1); float minuteAngle = getAngle(0, 60, smoothMinute); float secondAngle = getAngle(0, 60, smoothSecond); HourHand.transform.rotation = Quaternion.Euler(0, 0, hourAngle - 30); MinuteHand.transform.rotation = Quaternion.Euler(0, 0, minuteAngle); SecondHand.transform.rotation = Quaternion.Euler(0, 0, secondAngle); }
Create a “getAngle” function that takes in 3 properties (min, max, and value) and returns a float value
This function should convert the value given to a lerp between 0 and 360 based on the min and max values passed in
float getAngle(float min, float max, float value) { float lerp = Mathf.InverseLerp(min, max, value); return Mathf.Lerp(360, 0, lerp); }
DataStream Implementation
Open your Streams Listener Object in the scene
Add a script component. Create a C# script named ClockStreams
Add the following struct:
[System.Serializable] public struct Message { [SerializeField] public float hour; [SerializeField] public float minute; [SerializeField] public float second; }
This structure holds the data we will send in the stream
Add the following function:
public void SubmitGameData(GenvidStreamParameters streamParams) { if (GenvidPlugin.Instance.IsInitialized && GenvidPlugin.Instance.enabled) { string streamId = streamParams.Id; DateTime now = DateTime.Now; // We create a GameData object with the information we want to submit. Message gameData = new Message() { hour = now.Hour, minute = now.Minute, second = now.Second, }; // We submit the data as a data stream. GenvidPlugin.Instance.SessionManager.Session.Data.SubmitGameDataJSON(streamId, gameData); } }
This is called by the GenvidStreams object
We declare a new instance of the structure created above and set the values then submit it as a data stream
Back on the Streams Listener object - Expand “Streams->Listeners” add an element to this list
Drag your stream parameters into the “Stream” property
Expand “Events”
Add an element to the OnSubmitStream list
Drag a reference to itself into the empty Object property that shows up when adding a new element to the list
“No Function” should now be selectable. Click it and from the drop down look for ClockStreams->SubmitGameData
A GenvidStreamParameters object should appear. Drag your parameters object into it
You can verify the clock is working properly by hitting play
