Exemple #1
0
        public static UiTreeRoot Parse(string psdPath)
        {
            using (var document = PsdDocument.Create(psdPath))
            {
                var uiTree = new UiTreeRoot();

                uiTree.Name    = Path.GetFileName(psdPath);
                uiTree.Width   = document.Width;
                uiTree.Height  = document.Height;
                uiTree.Configs = _ParseConfig(document);

                PsdLayerConfig config = uiTree.Configs.GetLayerConfig(DocumentRootMagicLayerId);

                uiTree.Pivot = _GetPivot(config);

                uiTree.XAnchor = _GetXAnchorType(config);
                uiTree.YAnchor = _GetYAnchorType(config);

                var imageResource        = document.ImageResources;
                var resolutionProperty   = imageResource["Resolution"] as Reader_ResolutionInfo;
                int horizontalResolution = Convert.ToInt32(resolutionProperty.Value["HorizontalRes"]);

                uiTree.HorizontalPixelPerInch = horizontalResolution;

                foreach (PsdLayer layer in document.Childs)
                {
                    uiTree.Children.Add(_ParsePsdLayerRecursive(uiTree, layer));
                }

                return(uiTree);
            }
        }
Exemple #2
0
        private static GameObject _BuildUguiGameObjectTree(UiTreeRoot uiTree)
        {
            var        uguiVisitor    = new BuildUguiGameObjectVisitor(default(Rect), null, uiTree.HorizontalPixelPerInch);
            GameObject rootGameObject = uguiVisitor.Visit(uiTree);

            return(rootGameObject);
        }
        public GameObject Visit(UiTreeRoot root)
        {
            var uiRootGameObject    = new GameObject(root.Name);
            var uiRootRectTransform = uiRootGameObject.AddComponent <RectTransform>();
            var fullDocumentRect    = new Rect(0, 0, root.Width, root.Height);

            _SetRectTransform
            (
                uiRootRectTransform,
                fullDocumentRect, fullDocumentRect,
                root.GetAnchorMinValue(), root.GetAnchorMaxValue(),
                root.Pivot
            );
            uiRootRectTransform.ForceUpdateRectTransforms();

            var layerIdTag = uiRootGameObject.AddComponent <PsdLayerIdTag>();

            layerIdTag.LayerId = -1;

            var childrenVisitor = new BuildUguiGameObjectVisitor(fullDocumentRect, uiRootRectTransform, _basePixelPerInch);

            root.Children.ForEach(child => child.Accept(childrenVisitor));

            return(uiRootGameObject);
        }
Exemple #4
0
        public static void _SaveTextureAsAsset(string psdPath, UiTreeRoot uiTree)
        {
            string importedTexturesFolder = _GetImportedTexturesSavePath(psdPath);

            _EnsureFolder(importedTexturesFolder);
            string[]      allExistingFilePaths    = Directory.GetFiles(importedTexturesFolder);
            List <string> allExistingFilenameList = allExistingFilePaths.Select(Path.GetFileName).ToList();

            var saveTextureVisitor = new SaveTextureVisitor(importedTexturesFolder);

            saveTextureVisitor.Visit(uiTree);

            var newUiRequiredFilenameList = new List <string>();

            saveTextureVisitor.CreatedTextureFilename.ForEach(createdFilename =>
            {
                newUiRequiredFilenameList.Add(createdFilename);
                newUiRequiredFilenameList.Add(createdFilename + ".meta");
            });
            saveTextureVisitor.ReusedTextureFilename.ForEach(reusedFilename =>
            {
                newUiRequiredFilenameList.Add(reusedFilename);
                newUiRequiredFilenameList.Add(reusedFilename + ".meta");
            });

            foreach (string existingFilename in allExistingFilenameList)
            {
                if (!newUiRequiredFilenameList.Contains(existingFilename, StringComparer.OrdinalIgnoreCase))
                {
                    File.Delete(Path.Combine(importedTexturesFolder, existingFilename));
                }
            }
        }
        public static Vector2 GetAnchorMinValue(this UiTreeRoot uiTreeRoot)
        {
            float x = _GetAnchorMin(uiTreeRoot.XAnchor, 0);
            float y = _GetAnchorMin(uiTreeRoot.YAnchor, 0);

            return(new Vector2(x, y));
        }
Exemple #6
0
        public static void ImportSelection()
        {
            string psdPath = _GetSelectedPsdPath();

            if (!string.IsNullOrEmpty(psdPath))
            {
                UiTreeRoot uiTree = PsdParser.Parse(psdPath);
                ImportPsdAsPrefab(psdPath, uiTree);
            }
        }
Exemple #7
0
        public static void _SaveTextureAsAsset(string psdPath, UiTreeRoot uiTree)
        {
            string importedTexturesFolder = _GetImportedTexturesSavePath(psdPath);

            _ClearFolder(importedTexturesFolder);

            var saveTextureVisitor = new SaveTextureVisitor(importedTexturesFolder);

            saveTextureVisitor.Visit(uiTree);
        }
Exemple #8
0
        // Cannot import texture then get the Sprite reference on the same frame
        private static IEnumerator _ImportPsdAsPrefabProcess(string psdPath, UiTreeRoot uiTree)
        {
            _SaveTextureAsAsset(psdPath, uiTree);

            yield return(null);

            GameObject uiGameObject = _BuildUguiGameObjectTree(uiTree);

            var prefabPath = _GetImportedPrefabSavePath(psdPath);

            _SavePrefab(prefabPath, uiGameObject);
            GameObject.DestroyImmediate(uiGameObject);
        }
Exemple #9
0
        private static IEnumerator _ImportSelectionWithCanvasProcess(string psdPath)
        {
            UiTreeRoot uiTree = PsdParser.Parse(psdPath);

            var canvasGameObject    = _CreateCanvasGameObject(uiTree.Width, uiTree.Height);
            var canvasRectTransform = canvasGameObject.GetComponent <RectTransform>();

            canvasRectTransform.ForceUpdateRectTransforms();

            yield return(_ImportPsdAsPrefabProcess(psdPath, uiTree));

            string prefabPath = _GetImportedPrefabSavePath(psdPath);
            var    uiPrefab   = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);
            var    uiInstance = GameObject.Instantiate(uiPrefab);

            uiInstance.GetComponent <Transform>().SetParent(canvasRectTransform, worldPositionStays: false);
        }
        public GameObject Visit(UiTreeRoot root)
        {
            var uiRootGameObject = new GameObject(root.Name);

            var uiRootRectTransform = uiRootGameObject.AddComponent <RectTransform>();

            uiRootRectTransform.anchorMin = Vector2.zero;
            uiRootRectTransform.anchorMax = Vector2.one;
            uiRootRectTransform.offsetMin = Vector2.zero;
            uiRootRectTransform.offsetMax = Vector2.zero;
            uiRootRectTransform.ForceUpdateRectTransforms();

            var layerIdTag = uiRootGameObject.AddComponent <PsdLayerIdTag>();

            layerIdTag.LayerId = -1;

            var baseRect        = new Rect(0, 0, root.Width, root.Height);
            var childrenVisitor = new BuildUguiGameObjectVisitor(baseRect, uiRootRectTransform, _basePixelPerInch);

            root.Children.ForEach(child => child.Accept(childrenVisitor));

            return(uiRootGameObject);
        }
Exemple #11
0
        private static UiNode _ParsePsdLayerRecursive(UiTreeRoot tree, PsdLayer layer)
        {
            int    id        = (int)layer.Resources["lyid.ID"];
            string name      = layer.Name;
            bool   isVisible = layer.IsVisible;

            PsdLayerConfig config = tree.Configs.GetLayerConfig(id);

            bool isSkipped = config.GetLayerConfigAsBool(IsSkippedPropertyTag);

            Vector2 pivot = _GetPivot(config);

            XAnchorType xAnchor = _GetXAnchorType(config);
            YAnchorType yAnchor = _GetYAnchorType(config);

            var rect = new Rect
            {
                xMin = layer.Left,
                xMax = layer.Right,
                yMin = tree.Height - layer.Bottom,
                yMax = tree.Height - layer.Top
            };

            bool isGroup = _IsGroupLayer(layer);
            bool isText  = _IsTextLayer(layer);

            var baseUiNode = new UiNode
            {
                Id        = id,
                Name      = name,
                IsVisible = isVisible,
                IsSkipped = isSkipped,

                Pivot   = pivot,
                XAnchor = xAnchor,
                YAnchor = yAnchor,
                Rect    = rect
            };

            if (isGroup)
            {
                var children = new List <UiNode>();

                foreach (PsdLayer childLayer in layer.Childs)
                {
                    children.Add(_ParsePsdLayerRecursive(tree, childLayer));
                }

                return(new GroupNode(baseUiNode)
                {
                    Children = children
                });
            }
            else if (isText)
            {
                var engineData           = (StructureEngineData)layer.Resources["TySh.Text.EngineData"];
                var engineDict           = (Properties)engineData["EngineDict"];
                var styleRun             = (Properties)engineDict["StyleRun"];
                var runArray             = (ArrayList)styleRun["RunArray"];
                var firstRunArrayElement = (Properties)runArray[0];
                var firstStyleSheet      = (Properties)firstRunArrayElement["StyleSheet"];
                var firstStyleSheetData  = (Properties)firstStyleSheet["StyleSheetData"];

                var fontIndex = (int)firstStyleSheetData["Font"];

                var fontSize = _GetFontSizeFromStyleSheetData(firstStyleSheetData);
                // TODO: Fix this hack
                fontSize = fontSize / 75 * 18;
                var textColor = _GetTextColorFromStyleSheetData(firstStyleSheetData);

                var documentResources = (Properties)engineData["DocumentResources"];
                var fontSet           = (ArrayList)documentResources["FontSet"];
                var font     = (Properties)fontSet[fontIndex];
                var fontName = (string)font["Name"];

                var text = (string)layer.Resources["TySh.Text.Txt"];

                return(new TextNode(baseUiNode)
                {
                    FontSize = fontSize,
                    FontName = fontName,

                    Text = text,
                    TextColor = textColor
                });
            }
            else
            {
                WidgetType widgetType = config.GetLayerConfigAsWidgetType(WidgetTypePropertyTag);

                Texture2D texture2D = GetTexture2DFromPsdLayer(layer);

                return(new ImageNode(baseUiNode)
                {
                    WidgetType = widgetType,
                    SpriteSource = texture2D != null ?
                                   new InMemoryTextureSpriteSource {
                        Texture2D = texture2D
                    } :
                    (ISpriteSource) new NullSpriteSource()
                });
            }
        }
Exemple #12
0
 public static void ImportPsdAsPrefab(string psdPath, UiTreeRoot uiTree)
 {
     Executor.Add(AdInfinitum.Coroutine.Create(
                      _ImportPsdAsPrefabProcess(psdPath, uiTree)));
 }
Exemple #13
0
 public void Visit(UiTreeRoot root)
 {
     root.Children.ForEach(child => child.Accept(this));
 }
Exemple #14
0
        private static UiNode _ParsePsdLayerRecursive(UiTreeRoot tree, PsdLayer layer)
        {
            int    id        = (int)layer.Resources["lyid.ID"];
            string name      = layer.Name;
            bool   isVisible = layer.IsVisible;

            var config = tree.Configs.GetLayerConfig(id);

            bool isSkipped = _GetLayerConfigAsBool(config, IsSkippedPropertyTag);

            Vector2 pivot = new Vector2(_GetLayerConfigAsFloat(config, XPivotPropertyTag, 0.5f),
                                        _GetLayerConfigAsFloat(config, YPivotPropertyTag, 0.5f));

            XAnchorType xAnchor = _GetXAnchorType(config.GetValueOrDefault(XAnchorPropertyTag));
            YAnchorType yAnchor = _GetYAnchorType(config.GetValueOrDefault(YAnchorPropertyTag));

            var rect = new Rect
            {
                xMin = layer.Left,
                xMax = layer.Right,
                yMin = tree.Height - layer.Bottom,
                yMax = tree.Height - layer.Top
            };

            bool isGroup = _IsGroupLayer(layer);
            bool isText  = _IsTextLayer(layer);

            var baseUiNode = new UiNode
            {
                Id        = id,
                Name      = name,
                IsVisible = isVisible,
                IsSkipped = isSkipped,

                Pivot   = pivot,
                XAnchor = xAnchor,
                YAnchor = yAnchor,
                Rect    = rect
            };

            if (isGroup)
            {
                bool hasScrollRect          = _GetLayerConfigAsBool(config, HasScrollRectPropertyTag);
                bool isScrollRectHorizontal = _GetLayerConfigAsBool(config, IsScrollRectHorizontalPropertyTag);
                bool isScrollRectVertical   = _GetLayerConfigAsBool(config, IsScrollRectVerticalPropertyTag);

                bool    hasGrid      = _GetLayerConfigAsBool(config, HasGridPropertyTag);
                Vector2 gridCellSize = Vector2.zero;
                Vector2 gridSpacing  = Vector2.zero;

                if (hasGrid)
                {
                    float gridCellSizeX = _GetLayerConfigAsFloat(config, GridCellSizeXPropertyTag);
                    float gridCellSizeY = _GetLayerConfigAsFloat(config, GridCellSizeYPropertyTag);
                    float gridSpacingX  = _GetLayerConfigAsFloat(config, GridSpacingXPropertyTag);
                    float gridSpacingY  = _GetLayerConfigAsFloat(config, GridSpacingYPropertyTag);

                    gridCellSize = new Vector2(gridCellSizeX, gridCellSizeY);
                    gridSpacing  = new Vector2(gridSpacingX, gridSpacingY);
                }

                var children = new List <UiNode>();

                foreach (PsdLayer childlayer in layer.Childs)
                {
                    children.Add(_ParsePsdLayerRecursive(tree, childlayer));
                }

                return(new GroupNode(baseUiNode)
                {
                    HasScrollRect = hasScrollRect,
                    IsScrollRectHorizontal = isScrollRectHorizontal,
                    IsScrollRectVertical = isScrollRectVertical,

                    HasGrid = hasGrid,
                    CellSize = gridCellSize,
                    Spacing = gridSpacing,

                    Children = children
                });
            }
            else if (isText)
            {
                var engineData           = (StructureEngineData)layer.Resources["TySh.Text.EngineData"];
                var engineDict           = (Properties)engineData["EngineDict"];
                var styleRun             = (Properties)engineDict["StyleRun"];
                var runArray             = (ArrayList)styleRun["RunArray"];
                var firstRunArrayElement = (Properties)runArray[0];
                var firstStyleSheet      = (Properties)firstRunArrayElement["StyleSheet"];
                var firstStyelSheetData  = (Properties)firstStyleSheet["StyleSheetData"];

                var fontIndex = (int)firstStyelSheetData["Font"];

                var fontSize = _GetFontSizeFromStyelSheetData(firstStyelSheetData);
                // TODO: Fix this hack
                fontSize = fontSize / 75 * 18;
                var textColor = _GetTextColorFromStyelSheetData(firstStyelSheetData);

                var documentResources = (Properties)engineData["DocumentResources"];
                var fontSet           = (ArrayList)documentResources["FontSet"];
                var font     = (Properties)fontSet[fontIndex];
                var fontName = (string)font["Name"];

                var text = (string)layer.Resources["TySh.Text.Txt"];

                return(new TextNode(baseUiNode)
                {
                    FontSize = fontSize,
                    FontName = fontName,

                    Text = text,
                    TextColor = textColor
                });
            }
            else
            {
                string     widgetTypeString = config.GetValueOrDefault(WidgetTypePropertyTag);
                WidgetType widgetType       = _GetWidgetType(widgetTypeString);

                Texture2D texture2D = GetTexture2DFromPsdLayer(layer);

                return(new ImageNode(baseUiNode)
                {
                    WidgetType = widgetType,
                    SpriteSource = new InMemoryTextureSpriteSource {
                        Texture2D = texture2D
                    }
                });
            }
        }