[Unityui] Dynamic sliding list

Source: Internet
Author: User

The UI layout is such a drop:



Specific about the sliding list of settings can refer to here, where image scroll rect component content assignment is content, here, our content is an empty object, its size is the size of that box. Here are two important points:

The y of the 1.Content pivot must be set to the maximum value of Y, just like this:


What is this for? In fact, the principle of dynamic sliding list is to dynamically change the height of content, when the center point in the top position, you can ensure that the top position is unchanged, only the lower position in the change. If the center point is in the middle position, then the height change, both sides will change.

2. List item settings, note the list of anchor presets and pivot, because they will affect the location of the prefab instantiation, here for the convenience of calculation, I set the anchor presets for Top-center,pivot (0.5,1)


Finally, give the code:

Using unityengine;using system.collections;using System.collections.generic;public class Taskpanel:monobehaviour {PU    Blic static Taskpanel instance;    Public list<gameobject> items = new list<gameobject> (); Public gameobject content;//Content public Vector2 contentsize;//content original height public gameobject item;//list item public float ITE    Mheight;        Public Vector3 Itemlocalpos;        void Awake () {if (!instance) instance = this;    else Destroy (this);        } void Start () {content = Gameobject.find ("content"); Contentsize = content.        Getcomponent<recttransform> (). Sizedelta;        Item = resources.load ("item") as Gameobject; ItemHeight = Item.        Getcomponent<recttransform> (). Rect.height;     Itemlocalpos = item.transform.localPosition;        }//Add list item public void AddItem () {Gameobject a = instantiate (item) as Gameobject;        A.transform.parent = Content.transform; A.transform.locAlposition = new Vector3 (itemlocalpos.x, Itemlocalpos.y-items.        Count * itemheight, 0); Items.        Add (a); if (contentsize.y <= items. Count * itemheight)//Add content to the height {contents. Getcomponent<recttransform> (). Sizedelta = new Vector2 (contentsize.x, items.        Count * itemheight); }}//Remove list item public void RemoveItem (Gameobject t) {int index = items.        IndexOf (t); Items.        Remove (t);        Destroy (t); for (int i = index; i < items. Count;        i++)//Remove the list item after each item is moved forward {items[i].transform.localposition + = new Vector3 (0, itemheight, 0); } if (Contentsize.y <= items. Count * itemheight)//adjust content to the height of contents. Getcomponent<recttransform> (). Sizedelta = new Vector2 (contentsize.x, items.        Count * itemheight); else content.    Getcomponent<recttransform> (). Sizedelta = Contentsize; }}


[Unityui] Dynamic sliding list

Related Article

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.