Unity Run mode implements ComboBox with Guilayout

Source: Internet
Author: User

Tag:scroll   system.in    should be     based on    get    already    list box    div   color   

public class ComboBox {public int index;//index public string weight= "";//weight private int labelwidth = 60;//title of wide public float Weight {get {return float. Parse (weight);        }}///<summary>///The width of the check box///</summary> Public const int width = 150;        <summary>////The height of the check box///</summary> Public const int height = 20;        <summary>//lable//</summary> public string label = "";        public int viewareaheight = 200; Public ComboBox (Dictionary<object, object> datasource,int viewheight = $) {if (DataSource = = Nu                ll) {DataSource = new dictionary<object, object> (); for (int i = 0; i < i++) {Datasource.add (I, String).                Format ("<color= #ff0000ff > Initialize Data {0}</color>", i));        }    } viewareaheight = Viewheight; This.        Init (datasource,1);        } private Dictionary<object, object> DataSource;        Private Object CurrentValue;        Private Object Currentdisplaytext;               private int currentindex;        private bool showlist;        Private Vector2 scrollposition;        private float showlisttime;        private int guidepth;        private bool alreadyinitguidepth; <summary>///select Change event Parameters/////</summary> public class Selectitemchangedargs:eventarg            s {Private object itemvalue;            Private Object Itemdisplaytext;                Public object Itemvalue {get {return itemvalue;                    }} public Object Itemdisplaytext {get {                return itemdisplaytext; }} public SelecTitemchangedargs (Object Ivalue, Object idisplaytext) {itemvalue = Ivalue;            Itemdisplaytext = Idisplaytext; }}///<summary>///Select Item Change Event///</summary> public event eventhandler<        Selectitemchangedargs> onselectitemchanged;        Public object Selectvalue {get {return currentvalue;}        } public Object Selectdisplaytext {get {return currentdisplaytext;}        } public float Showlisttime {get {return showlisttime;} }///<summary>//Data source///</summary> public dictionary<object, object> Da                Tasource {set {DataSource = value;                Currentdisplaytext = null;            CurrentValue = null;            } get {return dataSource; }        }        ///<summary>////Initialize the parameters, the control should call this method only once, to reset the data source directly using the DataSource property can///</summary>//<para M name= "Data" >//<see cref= "Dictionary<system.object, system.object>"/>//</param&gt        ; <param name= "Depth" >///A <see cref= "System.Int32"/>//</param> public void            Init (Dictionary<object, object> data, int depth) {dataSource = data;            Currentindex =-1; Puts the control on top of the current GUI element and only sets the GUI depth for the first call to the initialization method, preventing repeated calls to the if (!alreadyinitguidepth) in the loop {//GUIDEP                th = gui.depth-1;            Alreadyinitguidepth = true;            } currentdisplaytext = null;        CurrentValue = null; }///<summary>///force Set drop-down list is shown///</summary>//<param name= "show" &        Gt A <see cref= "System.Boolean"/>//</param> public VOID Setshowlist (bool show) {if (showlist) {showlist = Show;            }}///<summary> Draw drop-down list box///</summary> public void Draw () {            if (DataSource = = null) {return;  } if (Currentdisplaytext = = NULL | | currentvalue = = NULL) {if (Datasource.count >                    0) {Currentdisplaytext = DataSource.Values.First ();                    CurrentValue = DataSource.Keys.First ();                Currentindex = 0;                    } else {currentdisplaytext = "none";                    CurrentValue =-1;                Currentindex =-1;            }} guilayout.beginvertical (Guilayout.width (Width));            Guilayout.beginhorizontal ();            int spacew = Labelwidth; if (label = = null | | Label.            Equals ("")) {spacew = 0;            } guilayout.label (Label, Guilayout.width (SPACEW));            String buttonname = "";            if (Currentdisplaytext = = null) {ButtonName = "none"; }else if (Currentdisplaytext is Monster_model) {buttonname = ((Monster_model) currentdisplaytext)            . Name;            }else if (Currentdisplaytext is string) {ButtonName = Currentdisplaytext.tostring (); } ButtonName + = (showlist?            "<color= #f27272FF >></color>": <color= #f27272FF >V</color> ");                if (Guilayout.button (buttonname,guilayout.width (Width +))) {showlist =!showlist;                if (showlist) {showlisttime = Time.time;                } else {showlisttime = 0f;        }    } guilayout.endhorizontal (); This.            Drawlist ();        Guilayout.endvertical ();            }///<summary>///Draw list items///</summary> public void Drawlist () {                if (showlist = = true) {//In order to leave the most square below the horizontal line, here the height minus 1 guilayout.beginhorizontal ();                int spacew = Labelwidth; if (label = = NULL | | label.                Equals ("")) {spacew = 0;                } Guilayout.label ("", Guilayout.width (SPACEW)); Scrollposition = Vector2.lerp (scrollposition, Guilayout.beginscrollview (Scrollposition, True, True,GUILayout.Width (                WIDTH+20), Guilayout.height (This.viewareaheight)), 0.5f);                for (int i = 0; i < Datasource.count; i++) {DrawItem (i);                } guilayout.endscrollview ();            Guilayout.endhorizontal ();        }        }<summary>///Draw content items and respond to events///</summary>//<param name= "index" >//A <see cref= "System.Int32"/>//</param> private void DrawItem (int index) {OB            Ject value = dataSource.Values.ElementAt (index);            String buttonname = value As String;            If (value is Monster_model) {buttonname = ((Monster_model) value). Name; } if (Guilayout.button (Buttonname,guilayout.width (Width))) {if (value is Monster_mode                    L) {Currentdisplaytext = ((Monster_model) value). Name;                CurrentValue = dataSource.Keys.ElementAt (index);                    } else {currentdisplaytext = value;                CurrentValue = dataSource.Keys.ElementAt (index);                 } if (Currentindex! = index) {   Currentindex = index;                        if (onselectitemchanged! = null) {if (value is Monster_model)                        {onselectitemchanged (this, new Selectitemchangedargs (CurrentValue, value));  } else {onselectitemchanged (this, new                        Selectitemchangedargs (CurrentValue, Currentdisplaytext));            }}} Showlist = false;            }} public void Ongui () {gui.depth = guidepth; This.        Draw ();         }///<summary>//Compare the Order of ComboBox controls by time, in order to click the new control, the other combobox control drop-down list is automatically hidden///</summary> public class Showlistcomparer:icomparer<combobox> {#region Icomparer[combobox] Implemen tation int icomparer<combobox>. Compare (ComboBox x, CoMbobox y) {return X.showlisttime.compareto (y.showlisttime) *-1; } #endregion}}

First of all: This function is in a great God GUI code based on the modification, so that it can be automatically typesetting, but has forgotten who is.

Unity Run mode implements ComboBox with Guilayout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.