Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

linking a slider created at runtime to a float

Discussion in 'UGUI & TextMesh Pro' started by topofsteel, Nov 4, 2014.

  1. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
    Actually, how would I associate any UI element created at runtime with a function? In the case of the slider, I have

    Code (CSharp):
    1. newSlider.onValueChange.Invoke(float arg());
    It wasn't entirely obvious how to hook it up with the dynamic float through the interface at first either. Can anyone show me what it's looking for or how to do this? Thanks.
     
  2. vptb

    vptb

    Joined:
    Jan 15, 2014
    Posts:
    28
    newSlider.onValueChanged.AddListener(YourFunction());

    public void YourFunction(float arg){
    }

    As for your second question, if you already have your public function with a float parameter, on the interface you just need to add the object with the script, then select your script and on the top of the list you should have your dynamic float functions.
     
  3. nventimiglia

    nventimiglia

    Joined:
    Sep 20, 2011
    Posts:
    153
    If you need databinding, Foundation is currently free and provides databinding for most uGUI controls complete with custom inspectors.

    http://mmofoundation.com
     
  4. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
    I'm close, but inexperience plagues me. I have a couple of questions...

    I believe I have that set up correctly, but in the wrong place? I am setting this up when I'm creating the slider, correct? I cannot get any indication that my UpdateFloat function is ever being called, even with a Debut.Log in place. I have tried it with and without the second argument, input.

    The return value Float that invoke is forcing is confusing me. If I were setting this up through the inspector, I don't believe the function can have a return value. I simply want to send that float off to be used, for lack of a better description.

    I have a dozen or more sliders, each used to set a float in the same manner. What am I missing? Thank you very much for your time!!

    (moved the onValueChanged to the bottom for clarity)

    Code (CSharp):
    1.     private void CreateFloat(ProceduralPropertyDescription input, int number)
    2.     {
    3.         float inputFloat = substance.GetProceduralFloat(input.name);
    4.  
    5.         GameObject newFloat = Instantiate(floatUI) as GameObject;
    6.         newFloat.name = input.name;
    7.         newFloat.transform.parent = gameObject.transform;
    8.  
    9.         Text newLabel = newFloat.GetComponentInChildren<Text>();
    10.         newLabel.text = input.name;
    11.  
    12.         Slider newSlider = newFloat.GetComponent<Slider>();
    13.         newSlider.minValue = input.minimum;
    14.         newSlider.maxValue = input.maximum;
    15.         newSlider.value = inputFloat;      
    16.  
    17.         RectTransform rectTransform = newFloat.GetComponent<RectTransform>();
    18.  
    19.         float minx = -140;
    20.         float miny = ((itemStart * number) + itemHeight) * -1;
    21.         rectTransform.offsetMin = new Vector2(minx, miny);
    22.  
    23.         float maxx = 140;
    24.         float maxy = (itemStart * number) * -1;
    25.         rectTransform.offsetMax = new Vector2(maxx, maxy);
    26.  
    27.         newSlider.onValueChanged.Invoke(UpdateFloat(input, newSlider.value));              
    28.     }
    29.  
    30.     public float UpdateFloat(ProceduralPropertyDescription input, float arg)
    31.     {
    32.         substance.SetProceduralFloat(input.name, arg);
    33.         return arg;
    34.     }