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