Search Unity

How to get and set value of Horizontal ScrollBar using value in TextField?

Discussion in 'Immediate Mode GUI (IMGUI)' started by SeneyHsns, Dec 11, 2014.

?

How to get and set value of Horizontal ScrollBar using value in TextField?

  1. sd

    0 vote(s)
    0.0%
  2. dsf

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. SeneyHsns

    SeneyHsns

    Joined:
    Dec 11, 2014
    Posts:
    3
    I want to have one TextField which is able to set and get value to and from scrollbar, but I can only get value. Any suggestion, please help. Here is my code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     private float scrolledVal;
    6.  
    7.     void OnGUI() {
    8.         scrolledVal = GUI.HorizontalScrollbar (new Rect(20, 20, 100, 30), scrolledVal, 1.0F, 33f, 58f);  
    9.         scrolledVal = float.Parse(GUI.TextField (new Rect(130, 20, 50, 20), scrolledVal.ToString()));
    10.     }
    11. }
     

    Attached Files:

    Last edited by a moderator: Dec 11, 2014
  2. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    Your example actually does update in both directions. The problem is that the scrollbar's range 33-58 is actually preventing you from typing a number in the text box. You can check this by setting your scrollbar range to 0-100. Try typing something and it will work.

    So why does the range screw it up? If you try typing a number like 45, the first digit '4' is less than '33' so the scrollbar immediately forces the value to be 33. In fact you can't type any number in the textbox because the first digit is always less than 33.

    The solution is to keep a separate variable for each control and only update one of your controls at a time. Try this.

    Code (CSharp):
    1.  
    2. private float scrolledVal;
    3. private string textVal = "";
    4.  
    5. void OnGUI()
    6. {
    7.     // Scrollbar
    8.     scrolledVal = GUI.HorizontalScrollbar(new Rect(20, 20, 100, 30), scrolledVal, 1, 33, 58);
    9.  
    10.     // Text field
    11.     GUI.SetNextControlName("MyText");
    12.     textVal = GUI.TextField(new Rect(130, 20, 50, 20), textVal);
    13.  
    14.     // Update either the text field or the scrollbar depending on which has focus
    15.     if (GUI.GetNameOfFocusedControl().Equals("MyText"))
    16.     {
    17.         float parsedVal;
    18.         if (float.TryParse(textVal, out parsedVal)) scrolledVal = parsedVal;
    19.     }
    20.     else
    21.     {
    22.         textVal = scrolledVal.ToString("0.00");
    23.     }
    24. }
    25.  
    As far as I know there's no way easier way to fix this in OnGUI. The built-in Unity GUI is great for prototyping GUI's, but it get's messy when you try to do anything beyond the basics.
     
    Last edited: Dec 15, 2014
  3. AdamScura

    AdamScura

    Joined:
    Mar 25, 2012
    Posts:
    55
    BTW:

    I'm making an object oriented GUI framework to assist me in making custom editor windows. It wraps up all the GUI and GUIEditor functions into clean object oriented code, and adds advanced features like events, and data binding so it will automatically keep your variables in sync with your controls.

    So instead of the clunky OnGUI workaround, you could just do this...

    Code (CSharp):
    1.  
    2. private Root root;
    3. private float scrolledValue;
    4.  
    5. void Awake()
    6. {
    7.     root = new Root();
    8.     root.Add<HorizontalScrollbar>().SetRange(1, 33, 58).BindValue(this, "scrolledValue");
    9.     root.Add<TextField>().BindText(this, "scrolledValue");
    10. }
    11.  
    12. void OnGUI()
    13. {
    14.     root.OnGUI(this);
    15. }
    16.  
    I'm debating if I should release it on the asset store. Let me know if you're interested.
     
  4. SeneyHsns

    SeneyHsns

    Joined:
    Dec 11, 2014
    Posts:
    3
    Thanks you very much Adam Scura. It helps me.