article

AndresGenvid avatar image
AndresGenvid posted

02. Unity Clock DataStreams Guide   

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

  1. Download the folder containing the assets for this guide

  2. Import “clock_face” (or import the entire pre-made clock)

Clock Setup

  1. 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

  2. Create an Empty Object named “Clock”

    • Make sure it’s in view of the main camera

  3. 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.

  4. Create 3 more Empty Objects that are children of “Clock”.

    • “HourHand”, “MinuteHand”, and “SecondHand”

  5. Create 3 Cube objects that are children of each hand named “HourHandCube”, “MinuteHandCube”, and “SecondHandCube”

  6. In each cubes component “MeshRenderer” replace Element 0 with “BlackDefaultMaterial”

    • You may need to create a black material yourself to apply

  7. Change “HourHand” rotation to 0, 0, 270

  8. Change “SecondHand” rotation to 0, 0, 315

  9. Change “HourHandCube” position to 0, .75, 0 and Scale to .15, 1.5, 1

  10. Change “MinuteHandCube” position to 0, 1, 0 and Scale to .1, 2, 1

  11. Change “SecondHandCube” position to 0, .5, 0 and Scale to .075, 1, 1


  12. 1667951050844.pngClock in Scene


  13. On “Clock” create a new script component named “Clock”

  14. Declare 3 public GameObjects “HourHand”, “MinuteHand”, and “SecondHand”

    • Drag the objects with the same names into these properties

  15. 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

  16.     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);
        }


  17. 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

  18.  float getAngle(float min, float max, float value)
     {
          
         float lerp = Mathf.InverseLerp(min, max, value);
         return Mathf.Lerp(360, 0, lerp);
     }


DataStream Implementation

  1. Open your Streams Listener Object in the scene

  2. Add a script component. Create a C# script named ClockStreams

  3. Add the following struct:

  4. [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

  5. Add the following function:

  6.    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

  7. Back on the Streams Listener object - Expand “Streams->Listeners” add an element to this list

  8. Drag your stream parameters into the “Stream” property

  9. Expand “Events”

  10. Add an element to the OnSubmitStream list

  11. Drag a reference to itself into the empty Object property that shows up when adding a new element to the list

  12. “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

  13. You can verify the clock is working properly by hitting play



1667951133422.pngExample Stream Listener



Unity
10 |600

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Article

Contributors

AndresGenvid contributed to this article