Пример #1
0
    private GameObject BuildText(Figma.Document document)
    {
        var go = BuildBase(document);

        SetupRect(document, go);

        var tmp = go.AddComponent <TextMeshProUGUI>();

        tmp.text      = document.Characters;
        tmp.fontSize  = document.Style.FontSize;
        tmp.alignment = TextAlignment(document);

        if (document.Fills.Length > 0)
        {
            if (document.Fills.Length > 1)
            {
                Debug.LogWarning($"More than one fill detected on {go.name}, using first.");
            }

            var firstFill = document.Fills[0];
            var color     = firstFill.Color;

            tmp.color = new Color(color.R, color.G, color.B, color.A);

            tmp.enabled = firstFill.Visible;
        }
        return(go);
    }
Пример #2
0
    private GameObject BuildRegularPolygon(Figma.Document document)
    {
        var go = BuildBase(document);

        BuildFills(document, go);
        return(go);
    }
Пример #3
0
    private GameObject BuildRectange(Figma.Document document)
    {
        var go = BuildBase(document);

        SetupRect(document, go);
        BuildFills(document, go);
        return(go);
    }
Пример #4
0
    private GameObject BuildCanvas(Figma.Document document)
    {
        var go = BuildBase(document);

        go.AddComponent <Canvas>();
        BuildBackground(document, go);
        return(go);
    }
Пример #5
0
    private GameObject BuildVector(Figma.Document document)
    {
        var go = BuildBase(document);

        SetupRect(document, go);
        var image = BuildFills(document, go);

        imagesById.Add(document.Id, image);
        return(go);
    }
Пример #6
0
    private void BuildBackground(Figma.Document document, GameObject go)
    {
        var image = go.AddComponent <UnityEngine.UI.Image>();

        image.color = new Color(
            document.BackgroundColor.R,
            document.BackgroundColor.G,
            document.BackgroundColor.B,
            document.BackgroundColor.A);

        image.enabled = false;
    }
Пример #7
0
    private Image BuildFills(Figma.Document document, GameObject go)
    {
        if (document.Fills == null)
        {
            return(null);
        }

        if (document.Fills.Length > 0)
        {
            if (document.Fills.Length > 1)
            {
                Debug.LogWarning($"More than one fill detected on {go.name}, using first.");
            }

            var image = go.AddComponent <Image>();

            var firstFill = document.Fills[0];
            var color     = firstFill.Color;

            if (color != null)
            {
                image.color = new Color(color.R, color.G, color.B, color.A);
            }

            if (firstFill.Type != Figma.FillType.Solid)
            {
                imagesById.Add(document.Id, image);
            }

            image.enabled = firstFill.Visible;

            return(image);
        }

        return(null);
    }
Пример #8
0
    void Build(Figma.Document document, Transform parent)
    {
        GameObject go = null;

        switch (document.Type)
        {
        case Figma.NodeType.Boolean:
            break;

        case Figma.NodeType.Canvas:
            go = BuildCanvas(document);
            break;

        case Figma.NodeType.Component:
            break;

        case Figma.NodeType.Document:
            break;

        case Figma.NodeType.Ellipse:
            break;

        case Figma.NodeType.Frame:
            go = BuildFrame(document);
            break;

        case Figma.NodeType.Group:
            go = BuildGroup(document);
            break;

        case Figma.NodeType.Instance:
            break;

        case Figma.NodeType.Line:
            break;

        case Figma.NodeType.Rectangle:
            go = BuildRectange(document);
            break;

        case Figma.NodeType.RegularPolygon:
            go = BuildRegularPolygon(document);
            break;

        case Figma.NodeType.Slice:
            break;

        case Figma.NodeType.Star:
            break;

        case Figma.NodeType.Text:
            go = BuildText(document);
            break;

        case Figma.NodeType.Vector:
            go = BuildVector(document);
            break;

        default:
            break;
        }

        if (!go && document.Type != Figma.NodeType.Vector)
        {
            go = new GameObject();
        }

        go.transform.SetParent(parent, true);

        if (document.Children != null)
        {
            foreach (var item in document.Children)
            {
                Build(item, go?.transform);
            }
        }

        // Prevent top-level "Page N" from being turned off
        if (document.AbsoluteBoundingBox == null)
        {
            go.SetActive(true);
        }

        go.SetActive(document.Visible);
    }
Пример #9
0
    private TextAlignmentOptions TextAlignment(Figma.Document document)
    {
        TextAlignmentOptions tao = TextAlignmentOptions.TopLeft;

        switch (document.Style.TextAlignHorizontal)
        {
        case Figma.TextAlignHorizontal.Center:
            switch (document.Style.TextAlignVertical)
            {
            case Figma.TextAlignVertical.Bottom:
                tao = TextAlignmentOptions.Bottom;
                break;

            case Figma.TextAlignVertical.Center:
                tao = TextAlignmentOptions.Center;
                break;

            case Figma.TextAlignVertical.Top:
                tao = TextAlignmentOptions.Top;
                break;

            default:
                break;
            }
            break;

        case Figma.TextAlignHorizontal.Justified:
            break;

        case Figma.TextAlignHorizontal.Left:
            switch (document.Style.TextAlignVertical)
            {
            case Figma.TextAlignVertical.Bottom:
                tao = TextAlignmentOptions.BottomLeft;
                break;

            case Figma.TextAlignVertical.Center:
                tao = TextAlignmentOptions.Left;
                break;

            case Figma.TextAlignVertical.Top:
                tao = TextAlignmentOptions.TopLeft;
                break;

            default:
                break;
            }
            tao = TextAlignmentOptions.Left;
            break;

        case Figma.TextAlignHorizontal.Right:
            switch (document.Style.TextAlignVertical)
            {
            case Figma.TextAlignVertical.Bottom:
                tao = TextAlignmentOptions.BottomRight;
                break;

            case Figma.TextAlignVertical.Center:
                tao = TextAlignmentOptions.Right;
                break;

            case Figma.TextAlignVertical.Top:
                tao = TextAlignmentOptions.TopRight;
                break;

            default:
                break;
            }
            break;

        default:
            break;
        }

        return(tao);
    }
Пример #10
0
    private void SetupRect(Figma.Document document, GameObject go)
    {
        var absoluteBoundingBox = document.AbsoluteBoundingBox;

        if (absoluteBoundingBox == null)
        {
            return;
        }

        var anchorMin = Vector2.zero;
        var anchorMax = Vector2.one;

        if (document.Constraints != null)
        {
            switch (document.Constraints.Horizontal)
            {
            case Figma.Horizontal.Center:
                anchorMin.x = .5f;
                anchorMax.x = .5f;
                break;

            case Figma.Horizontal.Left:
                anchorMin.x = 0;
                anchorMax.x = 0;
                break;

            case Figma.Horizontal.LeftRight:
                break;

            case Figma.Horizontal.Right:
                anchorMin.x = 1;
                anchorMax.x = 1;
                break;

            case Figma.Horizontal.Scale:
                anchorMin.x = 0;
                anchorMax.x = 1;
                break;

            default:
                break;
            }

            switch (document.Constraints.Vertical)
            {
            case Figma.Vertical.Bottom:
                anchorMin.y = 0;
                anchorMax.y = 0;
                break;

            case Figma.Vertical.Center:
                anchorMin.y = .5f;
                anchorMax.y = .5f;
                break;

            case Figma.Vertical.Scale:
                anchorMin.y = 0;
                anchorMax.y = 1;
                break;

            case Figma.Vertical.Top:
                anchorMin.y = 1;
                anchorMax.y = 1;
                break;

            case Figma.Vertical.TopBottom:
                break;

            default:
                break;
            }
        }

        var rectTransform = go.AddComponent <RectTransform>();

        rectTransform.anchorMin = new Vector2(0, 1);
        rectTransform.anchorMax = new Vector2(0, 1);
        rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, absoluteBoundingBox.X, absoluteBoundingBox.Width);
        rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, absoluteBoundingBox.Y, absoluteBoundingBox.Height);
    }
Пример #11
0
    private GameObject BuildBase(Figma.Document document)
    {
        var go = new GameObject(document.Name);

        return(go);
    }