/// <summary> /// sets the anchor bounds based on parent bounds /// </summary> /// <param name="r">parent bounds</param> public void SetPosition(Bounds r) { //calculate new bounds based on the min and max proportions Bounds = new Bounds(new Vector2(r.center.x + ((max.x + min.x - 1) / 2) * r.size.x, r.center.y + ((max.y + min.y - 1) / 2) * r.size.y), new Vector2(r.size.x * Mathf.Abs(max.x - min.x), r.size.y * Mathf.Abs(max.y - min.y))); //move to the new center position transform.position = Bounds.center; //scale any UI sprites to match the bounds SpriteRenderer render = GetComponent <SpriteRenderer>(); if (render != null) { render.size = Bounds.size; } //set the layout for this Anchor DUILayout layout = GetComponent <DUILayout>(); if (layout) { layout.SetAnchors(); } //recursively call set position on any child objects List <DUIAnchor> duias = new List <DUIAnchor>(); for (int i = 0; i < transform.childCount; i++) { DUIAnchor a = transform.GetChild(i).GetComponent <DUIAnchor>(); if (a != null) { duias.Add(a); } } foreach (DUIAnchor duia in duias) { duia.SetPosition(Bounds); } }
public void SetAnchors() { anchor = GetComponent <DUIAnchor>(); List <DUIAnchor> duias = new List <DUIAnchor>(); for (int i = 0; i < transform.childCount; i++) { duias.Add(transform.GetChild(i).GetComponent <DUIAnchor>()); } //calculate the offset between anchors Vector2 offset = new Vector2(layout == DUILayoutType.Horizontal ? 1.0f / (duias.Count) : 0, layout == DUILayoutType.Vertical ? 1.0f / (duias.Count) : 0); //set the min and max of every anchor based on anchor for (int i = 0; i < duias.Count; i++) { switch (layout) { case DUILayoutType.Vertical: duias[i].SetMinMax(Vector2.up - (i * offset), Vector2.one - ((i + 1) * offset)); break; case DUILayoutType.Horizontal: duias[i].SetMinMax(i * offset, ((i + 1) * offset) + Vector2.up); break; } DUILayout duil = duias[i].GetComponent <DUILayout>(); if (duil != null) { duil.SetAnchors(); } } }