public async Task GenerateNode(Node node, GameObject parent = null) { FigmaNodesProgressInfo.CurrentNode++; FigmaNodesProgressInfo.CurrentInfo = "Node generation in progress"; FigmaNodesProgressInfo.ShowProgress(0f); //RendersFolderの有無の確認 GenerateRenderSaveFolder(_importer.GetRendersFolderPath()); var boundingBox = node.absoluteBoundingBox; bool isParentCanvas = false; if (parent == null) { parent = FindCanvas(); offset = boundingBox.GetPosition(); isParentCanvas = true; } GameObject nodeGo = new GameObject(); RectTransform parentT = parent.GetComponent <RectTransform>(); if (isParentCanvas) { root = parentT; } nodeGo.name = node.name; var rectTransform = nodeGo.AddComponent <RectTransform>(); SetPosition(parentT, rectTransform, boundingBox); if (node.name == "bbbbbbbbbb") { Debug.Log("oopsie"); } if (!isParentCanvas) { SetConstraints(parentT, rectTransform, node.constraints); } SetMask(node, nodeGo); if (node.type != "TEXT" && (node.children == null || node.children.Length == 0)) { await RenderNodeAndApply(node, nodeGo); } else { AddText(node, nodeGo); AddFills(node, nodeGo); if (node.children == null) { return; } foreach (var child in node.children) { await GenerateNode(child, nodeGo); } } }
private async Task RenderNodeAndApply(Node node, GameObject nodeGo) { FigmaNodesProgressInfo.CurrentInfo = "Loading image"; FigmaNodesProgressInfo.ShowProgress(0f); var result = await _importer.GetImage(node.id); var t = nodeGo.transform as RectTransform; string spriteName = $"{node.name}_{node.id.Replace(':', '_')}.png"; Image image = null; Sprite sprite = null; FigmaNodesProgressInfo.CurrentInfo = "Saving rendered node"; FigmaNodesProgressInfo.ShowProgress(0f); try { SaveTexture(result, $"/{_importer.GetRendersFolderPath()}/{spriteName}"); sprite = ChangeTextureToSprite($"Assets/{_importer.GetRendersFolderPath()}/{spriteName}"); if (Math.Abs(t.rect.width - sprite.texture.width) < 1f && Math.Abs(t.rect.height - sprite.texture.height) < 1f) { image = nodeGo.AddComponent <Image>(); image.sprite = sprite; return; } } catch (Exception e) { Debug.LogError(e.Message); } var child = InstantiateChild(nodeGo, "Render"); if (sprite != null) { image = child.AddComponent <Image>(); image.sprite = sprite; t = child.transform as RectTransform; t.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, sprite.texture.width); t.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, sprite.texture.height); } }
private async Task <Texture2D> LoadTextureByUrl(string url) { using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url)) { request.SendWebRequest(); while (request.downloadProgress < 1f) { FigmaNodesProgressInfo.ShowProgress(request.downloadProgress); await Task.Delay(100); } if (request.isNetworkError || request.isHttpError) { return(null); } var data = request.downloadHandler.data; Texture2D t = new Texture2D(0, 0); t.LoadImage(data); FigmaNodesProgressInfo.HideProgress(); return(t); } }
public async Task <Texture2D> GetImage(string nodeId) { WWWForm form = new WWWForm(); string request = string.Format(ImagesUrl, _fileName, nodeId); using (UnityWebRequest www = UnityWebRequest.Get(request)) { www.SetRequestHeader("Authorization", $"Bearer {_settings.Token}"); www.SendWebRequest(); while (!www.isDone) { FigmaNodesProgressInfo.CurrentInfo = "Getting node image info"; FigmaNodesProgressInfo.ShowProgress(www.downloadProgress); await Task.Delay(100); } FigmaNodesProgressInfo.HideProgress(); if (www.isNetworkError) { Debug.Log(www.error); } else { var result = www.downloadHandler.text; var substrs = result.Split('"'); FigmaNodesProgressInfo.CurrentInfo = "Loading node texture"; foreach (var s in substrs) { if (s.Contains("http")) { return(await LoadTextureByUrl(s)); } } } } return(null); }
private async void GetFile(string fileUrl) { WWWForm form = new WWWForm(); string request = fileUrl; FigmaNodesProgressInfo.CurrentNode = FigmaNodesProgressInfo.NodesCount = 0; FigmaNodesProgressInfo.CurrentTitle = "Loading nodes info"; using (UnityWebRequest www = UnityWebRequest.Get(request)) { www.SetRequestHeader("Authorization", $"Bearer {_settings.Token}"); www.SendWebRequest(); while (!www.isDone) { FigmaNodesProgressInfo.CurrentInfo = "Loading nodes info"; FigmaNodesProgressInfo.ShowProgress(www.downloadProgress); await Task.Delay(100); } FigmaNodesProgressInfo.HideProgress(); if (www.isNetworkError) { Debug.Log(www.error); } else { var result = www.downloadHandler.text; FigmaParser parser = new FigmaParser(); var nodes = parser.ParseResult(result); FigmaNodesProgressInfo.NodesCount = GetNodesCount(nodes); FigmaNodeGenerator generator = new FigmaNodeGenerator(this); foreach (var node in nodes) { await generator.GenerateNode(node); } } FigmaNodesProgressInfo.HideProgress(); } }