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.

How to - Selecting from a List?

Discussion in 'UGUI & TextMesh Pro' started by launchpad, Aug 22, 2014.

  1. launchpad

    launchpad

    Joined:
    Aug 17, 2007
    Posts:
    95
    I am looking for an elegant way to select a line from a text list, eg. a name from a list of login names.

    It can likely be achieved using the Scroll View example, but that would be cumbersome. It would be nice to return the line/paragraph index or content from a line selected in a scrolling text field.

    Anyone got a strategy? I can't see anything obvious in the API.
     
  2. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    Also interested in this.
     
  3. GarethIW

    GarethIW

    Joined:
    Jun 24, 2014
    Posts:
    50
    I'm implementing a multiplayer game browser which is kinda this. I created a "line item" prefab which has a Selectable component on it (as well as some child Text boxes), and then instantiate it into a scrolling rect:

    Code (CSharp):
    1. var entry = (GameObject) Instantiate(BrowserEntryPrefab, new Vector3(0, y, 0), Quaternion.identity);
    2. entry.transform.SetParent(EntriesPanel.transform, false);
    3.  
    4. var ent = new EventTrigger.Entry();
    5. var cb = new EventTrigger.TriggerEvent();
    6. cb.AddListener(RoomClicked);
    7. ent.callback = cb;
    8. ent.eventID = EventTriggerType.PointerClick;
    9. entry.GetComponent<EventTrigger>().delegates.Add(ent);
    10.  
    11. entry.transform.FindChild("Name").GetComponent<Text>().text = "blah";
    12.  
    13. y -= 20f;
    14. if (y < -158) height += 20f;
    15. EntriesPanel.GetComponent<RectTransform>().sizeDelta = new Vector2(EntriesPanel.GetComponent<RectTransform>().sizeDelta.x, height);
    The last bit is spacing the items out vertically, and resizing the parent panel which acts as the Content for the scroll rect. I had to resize the panel manually as otherwise the items simply overflowed out the bottom and the scroll content size never changes.

    This is messy and inelegant, but until someone makes some decent controls (ListView, DropDownList, DataGrid) we're probably stuck doing stuff like this. Hope someone points out a better method!
     
  4. GrantTheAnt

    GrantTheAnt

    Joined:
    Jan 13, 2013
    Posts:
    19