Uncle Ciba's solo tour-u3d implement pop-up menu (top)-dynamic list

Source: Internet
Author: User

In u3d5.x, using Ugui as the default interface system, but there are too few controls, many requirements are not met, such as pop-up menu (PopupMenu)

I'm too lazy to go online to find a ready-made implementation, plus the existing code already has a lot of functionality about the list control, and no need to re-move the code.

Therefore, the realization of one, at present, only to achieve the core, build control related to the backbone of the class, later slowly enrich and do more fancy.

Before opening the statement, my u3d understanding is very limited, there are a lot of features that might be self-brought or have out-of-the-box library functionality I don't know, so chose to explore or implement by myself,

Feel too low welcome to give good advice.

Definition and code structure

PopupMenu is a list control that pops up in the appropriate location after clicking the mouse or button.

This list has a sub-list, and when clicked or the mouse enters a list item, the child list pops up.

The child list itself is also a popupmenu, or it can have a child list.

It can be seen from this definition:

1, PopupMenu first need the support of the list control, but the Ugui ScrollView support is not enough, so need dynamic list support.

2, second list items need to support Click or enter the event, click event, Ugui button is supported; the mouse enters the Pointenter event, not supported. All require an extended button to implement the response into the event, which is optional.

3, each list item needs to have special identification, or to load a part of the data, at least a different click or enter the response function, which requires a class to implement, that is, list items.

4, need a class to implement pop-up, hide, create lists, adjust location and other pop-up menu functions, this class is the component of the popup menu .

Dynamic list

Dynamic lists are classes that let Ugui's scrollview support dynamically adding and removing list items. My code is named Viewlist, and the key is to implement the ability to dynamically create list items and adjust contentsize.

Here you create a list item with your own text information:

      PublicListViewItem Createtextbutton (stringitemname) {Gameobject prototype= Resources.load<gameobject> ("Gui/control/itemtextbutton"); Gameobject Button=gameobject.instantiate (prototype);        Button.transform.SetParent (Viewcontent.transform); Button.transform.localScale=NewVector3 (1.0f,1.0f,1.0f); Button.name= Prototype.name +"_"+ItemName; Text text= Button.transform.Find ("Text"). Getcomponent<text>(); Text.color=NewColor (0xa9/255f,0xDD/255f,0xfd/255f); Text.text=ItemName; ListViewItem I= button. Getcomponent<listviewitem>(); if(i = =NULL) I= button. Addcomponent<listviewitem>(); I.listview= This;        M_items.add (i);        _resizeverticalcontent (button); returni; }

Where ListViewItem is a list item, all items are recorded to M_items for easy operation in PopupMenu.

ViewContent is the parent node of the list item, and support for Scrollrect is also supported without using Scrollrect:

if (getcomponent<scrollrect>())         = Getcomponent<scrollrect>(). Content.gameobject;     Else          = gameobject;

The following code implements the size control of the content of the vertical layout.

  void _resizeverticalcontent (gameobject button)    {        float height = button. Getcomponent<layoutelement>(). minheight;         float spacing = viewcontent.getcomponent<verticallayoutgroup>(). spacing;         float tb = viewcontent.getcomponent<verticallayoutgroup>(). Padding.top;        Viewcontent.getcomponent<RectTransform> (). Sizedelta =             new Vector2 ( Viewcontent.getcomponent<recttransform>(). sizedelta.x,                * (spacing + height)-spacing + TB );    }

It is important to control the size of the content, and for ScrollView, content lays down all list items, and the length of viewport setting mask,content (in the case of vertical scrolling) is much greater than viewport, Displays only the viewport size range

Content, which enables scrolling effects. For non-scrollview, the container that is about to do the list item does not have a scrolling effect, and its size needs to match the number of items in the list.

If it is a grid layout, you can refer to the following code:

        floatHeight = button. Getcomponent<layoutelement>(). MinHeight; floatspacing = viewcontent.getcomponent<gridlayoutgroup>(). Spacing.y; intNC = (int) (Viewcontent.getcomponent<recttransform> (). sizedelta.x/button. Getcomponent<LayoutElement>(). MinWidth); Viewcontent.getcomponent<RectTransform> (). Sizedelta =NewVector2 (viewcontent.getcomponent<recttransform>(). sizedelta.x, (ViewContent.transform.childCount% NC = =0?ViewContent.transform.childCount/NC:viewContent.transform.childCount/NC +1)                 * (float) (spacing + height));

The following article describes the extension buttons, Viewlistitem, etc.

Uncle Ciba's solo tour-u3d implement pop-up menu (top)-dynamic list

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.