Search Unity

Multi purpose interface UI Scripting

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

  1. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    Hi all
    I am trying to figured out how can I write a general script that can be use for a multipurpose interface UI.
    Basically I want that the final user is able to set all the variables throughout the editor without going into scripting.
    At the moment I have created some variables that I assign via scripting in order to retrieve other variables of a specific script. However this does not solve the problem because I still need to go into scripting in order to initialize them. For example:
    Code (CSharp):
    1. HourVar = MyScript.Instance.Hours;
    2. MySlider.value = HourVar;
    another time I would want for example:
    Code (CSharp):
    1. HourVar = MyOtherScript.Instance.Hours;
    2. MySlider.value = HourVar;
    Is there a way to assign MyScript.Instance.Hours to HourVar without scripting?
    Don't know if this makes sense but basically I just want a graphic method in order to assign variables to different scripts.
    Many thanks in advance.
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Are you looking for a visual scripting method, or a way to dynamically change logic through assets? You could do this using Reflection, or by having the classes in question register themselves, but whether that's a good idea depends on what you're using it for.

    Also, I think this question should probably be in the Scripting forum.
     
  3. imagoculturae

    imagoculturae

    Joined:
    Apr 8, 2014
    Posts:
    81
    No I am not looking for a visual scripting method. Not sure what are Reflection or register classes but I will look into it.
    I am only say that there must be a way to assign different scripts to variables without manually changing my general canvas script
    In other words I want to build UI sliders, button etc.. that can be assigned to different scripts in the editor.
    At the moment I have:
    Code (CSharp):
    1.  private float HourVar;
    Code (CSharp):
    1.     HourVar = MyScript.Instance.Hours;
    2.     MySlider.value = HourVar;
    3.  
    But leater on I would want to say for example
    Code (CSharp):
    1.  private float HourVar;
    Code (CSharp):
    1.     HourVar = MyOtherScript.Instance.Hours;
    2.     MySlider.value = HourVar;
    3.  
    Yes im my canvasScript I could just search and replace MyScript.Instance.Hours, with MyOtherScript.Instance.Hours; but I want to do this in the editor

    I will re-post on scripting , thanks for now
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Reflection with attributes is probably your answer.
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    As for the Property -> UI there's not much that can be done without scripting.
    In UI -> Property perspective you could use the UnityEvent that comes with the UI components.
    As well as you can define your own.

    On slider on the inspector that would be a gray box OnValueChanged with +-.

    Declare a method that is called via inspector -> drag & drop the object that's implementing it to the UnityEvent.
    Make sure your UnityEvent has same parameters as you're outputing via your UI component. E.g. slider would have float property by default.

    If you define your method like so:

    Code (CSharp):
    1. public void DoSomethingWithFloat(float dynamicFloat) {
    2. // Do whatever
    3. }
    You'll be able to assign the method with "dynamic" float variable passed to the event.