Search Unity

OnGUI and a hundred sliders

Discussion in 'Immediate Mode GUI (IMGUI)' started by cecarlsen, Nov 19, 2007.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    862
    Dear forum

    Code (csharp):
    1. function OnGUI () {
    2.     slider_0 = GUI.HorizontalSlider ( Rect ( 20, 20, 100, 10 ), 5, 0, 10);
    3.     slider_1 = GUI.HorizontalSlider ( Rect ( 20, 40, 100, 10 ), 5, 0, 10);
    4.     slider_2 = GUI.HorizontalSlider ( Rect ( 20, 60, 100, 10 ), 5, 0, 10);
    5.     // ... and so on ... //
    6.        
    7.     if( GUI.changed ){
    8.         // yes something changed ... but which slider exactly? //
    9.     }
    10. }
    Do I have to create lastValue-variables for each slider and compare every new slider value to the according lastValue to find out exactly which slider has moved ... or is there a less crude way of locating the changed slider?

    Note that I need the sliders on the same script because I want to arrange them with GUILayout.

    For the sake of about 100 lines of code
    ~ce
     
  2. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    Code (csharp):
    1. var            howManSliders : int = 20;
    2. var            mySliders : float [];
    3. //var            myPrevValues : float [];
    4.  
    5. function Start () {
    6.     // stuff our array full of zeroes
    7.     var tmp = new Array ();
    8.     for (var i : int = 0; i < howManSliders; i++) {
    9.         tmp.Add (0.0);
    10.     }
    11.     mySliders = tmp;
    12.     //myPrevValues = tmp;
    13.     // check array loaded properly
    14.     Debug.Log(mySliders.length);
    15. }
    16.  
    17. function OnGUI () {
    18.     var j : int = 0;
    19.     for (var ary : int in mySliders) {
    20.         //myPrevValues[j] = mySliders[j];
    21.         var k : int = 15 * j;
    22.         mySliders[j] = GUI.HorizontalSlider ( Rect ( 20, 20+k, 100, 10 ), mySliders[j], 0, 10);
    23.         // check for change
    24.         if (GUI.changed) {
    25.             Debug.Log ("'"+j+"' changed");
    26.             GUI.changed = false;   // reset the value, so we can detect the next one
    27.         }
    28.         //if (mySliders[j] != myPrevValues[j]) {
    29.         //  Debug.Log("slider '" + j + "' has changed!");
    30.         //}
    31.         j++;
    32.     }
    33. }
    [edited] I stand corrected again! I've fixed the code above to use GUI.changed properly! When I tried it last night I overlooked resetting it to false after checking it! So simple.

    Cheers.

    ps. i love this new gui system! it's kick arse powerful and fast! :wink:
     
  3. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    862
    That's cool, I'll go for the "compare old and new values" solution then.

    Thanks thylaxene
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    You can check GUI.changed after each slider
    Code (csharp):
    1.  
    2. myVar = GUILayout.Slider (myVar, 0, 1);
    3. if (GUI.changed) {
    4.     Debug.Log ("myVar changed");
    5.     GUI.changed = false;   // reset the value, so we can detect the next one
    6. }
    7.    
    8. myOtherVar = GUILayout.Slider (myOtherVar, 0, 1);
    9. if (GUI.changed) {
    10.     Debug.Log ("myOtherVar changed");
    11.     GUI.changed = false;   // reset the value, so we can detect the next one
    12. }
    13.  
    Or you can just use a for loop
     
  5. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    862
    Ha! Now we are getting somewhere. Great Nicholas!

    =D