Search Unity

Multi purpose interface UI Scripting

Discussion in 'Scripting' started by imagoculturae, Jul 24, 2018.

  1. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    A few months back I created something I call 'datasacks' and I've been using it in all my games lately.

    It is basically a ScriptableObject-driven approach to "genericizing" the Unity editor-to-code interface. The point is to never again drag GameObjects/Unity UI objects into your scripts. You put Datasacks on the UI objects and then they send/receive data to Datasacks, and your scripts interoperate with the data as they see fit.

    In response to your question in the other forum, today I decided to open source datasacks... I hope it helps!

    Code can be found here:

    https://github.com/kurtdekker/datasacks

    or here:

    https://bitbucket.org/kurtdekker/datasacks

    There's a little sample game to give you the idea of how some of the stuff works as well as a discussion in the project readme.txt.
     
  3. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Whoah! That's amazing. Thank you so much. I will have a look and let you know how it goes as I am not a programmer and I have to take it slowly.
    This will be so important for me because I am really interested in designing general purpose UI without the need of specify what their final purpose will be. For example I am interesting in designing MIDI controllers within the UNITY UI. Or Slider that controls digital lights for live events and so on...
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Excellent! The little game demo in Datasacks shows you how to take input from a slider. You make a slider, put a DSSliderSetFloat.cs script on it, drag the Datasack you want to be updated by that slider (one per slider), and your UI portion is now done.

    Now over in code you would have a script that subscribes to each of those Datasacks to be notified of changes, and then when changes come in, convert it to a MIDI command and send it out.

    That DSSliderSetFloat.cs is bidirectional, so changes to the data also drive the slider itself.

    This means that it also subscribes to the Datasack you connect to it so you can see that sample code if you wanna copy/paste it to get your script up and working and listening to Datasacks. You would obviously subscribe to a bunch of datasacks at once, so that you can respond to whatever slider changes happen.

    Here's the sample subscription code:

    Code (csharp):
    1.     void    OnDatasackChanged( Datasack ds)
    2.     {
    3.         // Here is where you service the notification
    4.         // that the contents of this Datasack changed
    5.         // You can inspect the name of the Datasack if
    6.         // you expect more than one Datasack to call
    7.         // this function.
    8.     }
    9.  
    10.     void    OnEnable()
    11.     {
    12.         dataSack.OnChanged += OnDatasackChanged;
    13.     }
    14.     void    OnDisable()
    15.     {
    16.         dataSack.OnChanged -= OnDatasackChanged;
    17.     }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    I put together a diagram for how these things work. It's in the repo now too.

    20180724_datasacks_overview.png
     
  6. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Amazing! Thank you so much, I was trying to do this since long time...and I could not believe that Unity was not able to do it out of the box.
    I will experiment with all of this and let you know how it goes.
    Thanks again
    Nic
     
    Kurt-Dekker likes this.