Example #1
0
    public static void CreatePattern()
    {
        var go     = Selection.activeGameObject;
        var top    = go.transform.Find("Top");
        var right  = go.transform.Find("Right");
        var left   = go.transform.Find("Left");
        var bottom = go.transform.Find("Bottom");

        if (top == null || right == null || left == null || bottom == null)
        {
            Debug.LogError("not a rect" + top + right + left + bottom);
            return;
        }

        var parentScale = go.transform.localScale;
        // alternative way would be to get the bounds
        var avgThickness = (bottom.localScale.y + top.localScale.y) * parentScale.y +
                           (right.localScale.x + left.localScale.x) * parentScale.x;

        avgThickness /= 4f;

        InputBoxWindow.RequestFloat(
            "Border thickness", avgThickness.ToString(),
            delegate(float thickness) {
            float yScale      = thickness / parentScale.y;
            float xScale      = thickness / parentScale.x;
            float width       = parentScale.x;
            float height      = parentScale.y;
            var parentPos     = go.transform.position;
            top.localScale    = top.localScale.SwapY(yScale);
            top.position      = top.position.SwapY(parentPos.y + (height - thickness) / 2);
            bottom.localScale = bottom.localScale.SwapY(yScale);
            bottom.position   = bottom.position.SwapY(parentPos.y - (height - thickness) / 2);
            left.localScale   = left.localScale.SwapX(xScale);
            left.position     = left.position.SwapX(parentPos.x - (width - thickness) / 2);
            right.localScale  = right.localScale.SwapX(xScale);
            right.position    = right.position.SwapX(parentPos.x + (width - thickness) / 2);
        });
    }