public void ContainsPointVerifivation() { Rect testRect1 = new Rect(0.0f, 0.0f, 30.0f, 30.0f); Assert.IsTrue(RectUtility.Contains(testRect1, new Vector2(1.0f, 1.0f))); // Contains Assert.IsFalse(RectUtility.Contains(testRect1, new Vector2(30.0f, 10.0f))); // Borders Assert.IsFalse(RectUtility.Contains(testRect1, new Vector2(31.0f, 15.0f))); // Does not Contain }
public void ContainsVerifivation() { Rect testRect1 = new Rect(0.0f, 0.0f, 30.0f, 30.0f); Rect testRect2 = new Rect(10.0f, 10.0f, 10.0f, 10.0f); Rect testRect3 = new Rect(10.0f, 10.0f, 100.0f, 100.0f); Rect testRect4 = new Rect(-10.0f, 10.0f, 1.0f, 1.0f); Assert.IsTrue(RectUtility.Contains(testRect1, testRect2)); // Contains Assert.IsFalse(RectUtility.Contains(testRect1, testRect4)); // Outside Assert.IsFalse(RectUtility.Contains(testRect1, testRect3)); // Outside and smaller Assert.IsFalse(RectUtility.Contains(testRect2, testRect3)); // Smaller Assert.IsTrue(RectUtility.Contains(testRect3, testRect2)); // Borders }
/// <summary> /// 自动优化,每一个视图,如果被上层的任意视图矩形区域完全覆盖,则自动SetActive(false),节省渲染开销 /// </summary> void AutoOptimize() { SortViewListBySiblingIndex(); var lastIndex = viewList.Count - 1; if (isAutoOptimize && lastIndex > -1) { viewList[lastIndex].SetActive(true); var lastRT = viewList[lastIndex].GetComponent <RectTransform>(); //如果被它完全压住的窗口,则隐藏 for (int i = 0; i < lastIndex; i++) { var compareRT = viewList[i].GetComponent <RectTransform>(); if (RectUtility.Contains(lastRT.rect, compareRT.rect)) { compareRT.gameObject.SetActive(false); } } } }
// This will need to be implemented with a Quad-tree (or some analog) if // this naive, O(n-squared) search implementation turns out to be too slow. public Rect NextPosition(IWidget widget, IEnumerable <Rect> reservedSpaces, Vector2 parentSize, IList <KeyValuePair <IWidget, IAutoLayout> > autolayoutWidgets) { Rect attemptedReservation = new Rect(0.0f, 0.0f, widget.ExternalSize.x, widget.ExternalSize.y); List <Rect> validLocations = new List <Rect>(); // Best case is if this widget fits in the initial guess location (top left) if (IsValidLocation(reservedSpaces, attemptedReservation)) { validLocations.Add(attemptedReservation); } // If widget doesn't fit in the initial guess, try to place it to the // right of any reserved space. if (validLocations.Count == 0) { Rect parentSpace = new Rect(0.0f, 0.0f, parentSize.x, parentSize.y); foreach (Rect reservedSpace in reservedSpaces) { attemptedReservation = new Rect(reservedSpace.x + reservedSpace.width, reservedSpace.y, widget.ExternalSize.x, widget.ExternalSize.y); if (RectUtility.Contains(parentSpace, attemptedReservation) && IsValidLocation(reservedSpaces, attemptedReservation)) { // n-squared validLocations.Add(attemptedReservation); } } } // If the widget didn't fit to the right of any reserved space, try under them // This search doesn't check against the parent space, so the reserved space can // go off the bottom of that space if (validLocations.Count == 0) { foreach (Rect reservedSpace in reservedSpaces) { attemptedReservation = new Rect(reservedSpace.x, reservedSpace.y + reservedSpace.height, widget.ExternalSize.x, widget.ExternalSize.y); if (IsValidLocation(reservedSpaces, attemptedReservation)) { // n-squared validLocations.Add(attemptedReservation); } } } // Find the best validLocation: Highest on Y and then furthest to the left. List <Rect> bestOnY = new List <Rect>(); float lowestYValue = Mathf.Infinity; foreach (Rect validLocation in validLocations) { if (validLocation.y < lowestYValue) { lowestYValue = validLocation.y; } } foreach (Rect validLocation in validLocations) { if (validLocation.y == lowestYValue) { bestOnY.Add(validLocation); } } // Out of the valid locations that are best on Y, find the one that's furthest to the left float lowestXValue = Mathf.Infinity; foreach (Rect validLocation in bestOnY) { if (validLocation.x < lowestXValue) { lowestXValue = validLocation.x; attemptedReservation = validLocation; } } mCachedPositions[widget] = new Vector2(attemptedReservation.x, attemptedReservation.y); if (widget.Style != null) { mCachedPositions[widget] += new Vector2(widget.Style.ExternalMargins.Left, widget.Style.ExternalMargins.Top); } return(attemptedReservation); }