Beispiel #1
0
        ///// <summary> NOTE: Use the optimized version if parent is known </summary>
        ///// <param name="fixedEdge"></param>
        ///// <param name="newInset"></param>
        ///// <param name="newSize"></param>
        //static void SetInsetAndSizeFromParentEdgeWithCurrentAnchors(this RectTransform child, RectTransform.Edge fixedEdge, float newInset, float newSize)
        //{
        //    child.SetInsetAndSizeFromParentEdgeWithCurrentAnchors(child.parent as RectTransform, fixedEdge, newInset, newSize);
        //}

        /// <summary> Same as RectTransform's built-in method, but this preserves the anchors</summary>
        public static void SetInsetAndSizeFromParentEdgeWithCurrentAnchors(this RectTransform child, RectTransform.Edge fixedEdge, float newInset, float newSize)
        {
            // Commented: as of 27.07.2018, a better way was found, which luckily also removes the need of mapped actions + bonus2: no need for parent anymore.
            // The subtle problem with the old approach is that it didn't work for very large distances, like when changing
            // an item's inset/size while it was way out of the parent's rectangle

            //_SetInsetAndSizeFromParentEdgeWithCurrentAnchors_MappedActions[(int)fixedEdge](child, parentHint, newInset, newSize);

            int     axisIndex = (int)fixedEdge / 2;
            Vector2 origAnchorMin = child.anchorMin, origAnchorMax = child.anchorMax;
            Vector2 origSizeDelta = child.sizeDelta;
            float   sizeChange    = newSize - child.rect.size[axisIndex];

            child.SetInsetAndSizeFromParentEdge(fixedEdge, newInset, newSize);
            child.AfterSetInsetAndSizeFromParentEdgeWithCurrentAnchors_RestoreAnchorsIfNeeded(axisIndex, origSizeDelta, ref origAnchorMin, ref origAnchorMax, sizeChange);
        }