Пример #1
0
        public static void SetAnchor(RectTransform rectTransform, string name)
        {
            Vector4 anchor = PsdUtils.GetAnchorWithGroupName(name);

            rectTransform.anchorMin = new Vector2(anchor.x, anchor.y);
            rectTransform.anchorMax = new Vector2(anchor.z, anchor.w);
        }
Пример #2
0
        private static string GetTextureFilePath(Layer layer, out string writePath)
        {
            string file = string.Empty;

            writePath = _texturePath;
            string layerName = PsdUtils.ClearNameTail(layer.Name);

            file = Path.Combine(writePath, layerName + ".png");
            return(file);
        }
Пример #3
0
        public static GroupClass CheckGroupClass(Layer layer)
        {
            if (layer.Children.Count == 0)
            {
                return(GroupClass.Empty);
            }

            if (layer.Name.StartsWith(BTN))
            {
                return(GroupClass.Button);
            }
            if (layer.Name.StartsWith(IMAGE))
            {
                return(GroupClass.Image);
            }
            if (layer.Name.StartsWith(TEXTURE))
            {
                return(GroupClass.Texture);
            }
            if (layer.Name.StartsWith(SCROLL))
            {
                return(GroupClass.ScrollRect);
            }
            if (layer.Name.StartsWith(PROGRESS))
            {
                return(GroupClass.Progress);
            }
            if (layer.Name.StartsWith(SLIDER))
            {
                return(GroupClass.Slider);
            }

            int imgLayerCount = 0;

            foreach (var child in layer.Children)
            {
                // 如果有图像layer
                if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child))
                {
                    imgLayerCount++;
                }
            }

            if (imgLayerCount == 1)
            {
                return(GroupClass.Image);
            }

            if (layer.Children.Count > 0)
            {
                return(GroupClass.RectTransform);
            }

            return(GroupClass.Empty);
        }
Пример #4
0
 private static void ExportTreeNode(Layer layer, UINode parentNode)
 {
     UpdateLayerName(layer, MakeNameSafe(layer.Name));
     if (PsdUtils.IsGroupLayer(layer))
     {
         ExportGroup(layer, parentNode);
     }
     else
     {
         ExportLayer(layer, parentNode);
     }
 }
Пример #5
0
        public static UINode CreateRectTransform(Layer layer)
        {
            GameObject    go = new GameObject(layer.Name);
            RectTransform goRectTransform = go.AddComponent <RectTransform>();

            UINode node = new UINode();

            node.rect = layer.Rect;
            node.Go   = go;

            SetAnchor(goRectTransform, layer.Name);
            PsdUtils.SetNodeName(node, go.name);
            return(node);
        }
Пример #6
0
        public static UINode CreateImage(Layer layer, bool isTexture = false)
        {
            Layer imgLayer = layer;

            foreach (var child in layer.Children)
            {
                if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child))
                {
                    imgLayer = child;
                    break;
                }
            }

            layer.Children.Remove(imgLayer);
            float width  = imgLayer.Rect.width;
            float height = imgLayer.Rect.height;

            GameObject    go = new GameObject(layer.Name);
            RectTransform goRectTransform = go.AddComponent <RectTransform>();

            goRectTransform.sizeDelta = new Vector2(width, height);
            if (!isTexture)
            {
                Image img = go.AddComponent <Image>();
                PsdImporter.CreateSprite(imgLayer, img);
                if (imgLayer.Is9Slice)
                {
                    img.type = Image.Type.Sliced;
                }
            }
            else
            {
                RawImage img = go.AddComponent <RawImage>();
                img.texture = PsdImporter.CreateTexture2D(imgLayer);
            }

            SetAnchor(goRectTransform, layer.Name);
            UINode node = new UINode();

            node.rect = imgLayer.Rect;
            node.Go   = go;
            PsdUtils.SetNodeName(node, go.name);
            return(node);
        }
Пример #7
0
 private static void UpdateScaleImgSprite()
 {
     foreach (KeyValuePair <string, List <Image> > kvp in _scaleImgDic)
     {
         string path = kvp.Key.Replace(PsdUtils.GetFullProjectPath(), string.Empty);
         AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
         Sprite sprite = (Sprite)AssetDatabase.LoadAssetAtPath(path, typeof(Sprite));
         if (null == sprite)
         {
             Debug.LogWarning(string.Format("缺少引用资源 {0} ", path));
         }
         else
         {
             foreach (var img in kvp.Value)
             {
                 img.sprite = sprite;
             }
         }
     }
 }
Пример #8
0
        public static string GetFilePath(Layer layer, out string writePath)
        {
            string file = string.Empty;

            writePath = _currentPath;
            string layerName = layer.Name;

            if (layerName.Contains(PUBLIC_IMG_HEAD))
            {
                int length = writePath.Length - 1;
                if (writePath.LastIndexOf(@"/") != -1)
                {
                    length = writePath.LastIndexOf(@"/");
                }

                writePath  = writePath.Substring(0, length);
                writePath += PUBLIC_IMG_PATH;
            }
            //layerName = PsdUtils.TrimSliceReg(layerName);
            layerName = PsdUtils.ClearNameTail(layerName);
            file      = Path.Combine(writePath, layerName + ".png");
            return(file);
        }
Пример #9
0
 private static string GetRelativePath(string fullPath)
 {
     return(fullPath.Replace(PsdUtils.GetFullProjectPath(), string.Empty));
 }
Пример #10
0
        private static void Import(string asset)
        {
            _scaleImgDic = new Dictionary <string, List <Image> >();

            _imageDic = new Dictionary <string, Sprite>();

            _btnNameList = new List <string>();

            _nullImageIndex = 0;

            _positionDic = new Dictionary <GameObject, Vector3>();

            _currentDepth = MaximumDepth;

            string fullPath = Path.Combine(PsdUtils.GetFullProjectPath(), asset.Replace('\\', '/'));

            PsdFile psd = new PsdFile(fullPath);

            _canvasSize = ScreenResolution;

            _depthStep = psd.Layers.Count != 0 ? MaximumDepth / psd.Layers.Count : 0.1f;

            int lastSlash = asset.LastIndexOf('/');

            string assetPathWithoutFilename = asset.Remove(lastSlash + 1, asset.Length - (lastSlash + 1));

            _psdName = asset.Replace(assetPathWithoutFilename, string.Empty).Replace(".psd", string.Empty);

            _currentPath = PsdUtils.GetFullProjectPath() + "Assets/" + CURR_IMG_PATH_ROOT;
            _texturePath = Path.Combine(_currentPath, "Textures");
            _currentPath = Path.Combine(_currentPath, _psdName);

            CreateDic(_texturePath);
            CreateDic(_currentPath);

            if (_layoutInScene || _createPrefab)
            {
                if (UseUnityUI)
                {
                    CreateUIEventSystem();
                    CreateUICanvas();
                }

                //create ui Root
                _rootPsdGameObject = CreateObj(_psdName);
                _rootPsdGameObject.transform.SetParent(_canvasObj.transform, false);
                _rootNode    = new UINode();
                _rootNode.Go = _rootPsdGameObject;
                Debug.Log("Name:" + _rootPsdGameObject.name);

                RectTransform rectRoot = _rootPsdGameObject.GetComponent <RectTransform>();

                rectRoot.anchorMin = new Vector2(0, 0);
                rectRoot.anchorMax = new Vector2(1, 1);
                rectRoot.offsetMin = Vector2.zero;
                rectRoot.offsetMax = Vector2.zero;

                _rootNode.rect = new Rect(0, 0, ScreenResolution.x, ScreenResolution.y);

                Vector3 rootPos = Vector3.zero;
                _rootPsdGameObject.transform.position = Vector3.zero;
                _currentGroupGameObject = _rootPsdGameObject;
            }

            List <Layer> tree = BuildLayerTree(psd.Layers);

            ExportTree(tree, _rootNode);
            PsdUtils.CreateUIHierarchy(_rootNode);
            PsdUtils.UpdateAllUINodeRectTransform(_rootNode);

            if (_createPrefab)
            {
                UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab(asset.Replace(".psd", ".prefab"));
                PrefabUtility.ReplacePrefab(_rootPsdGameObject, prefab);
            }

            UpdateScaleImgSprite();
            AssetDatabase.Refresh();
        }
Пример #11
0
        public static UINode CreateUIText(Layer layer)
        {
            Color      color      = layer.FillColor;
            GameObject gameObject = new GameObject(layer.Name);

            Font font = PsdImporter.GetFontInfo();

            Text textUI = gameObject.GetComponent <Text>();

            if (textUI == null)
            {
                textUI = gameObject.AddComponent <Text>();
            }

            RectTransform rectTransform = gameObject.GetComponent <RectTransform>();

            if (rectTransform == null)
            {
                rectTransform = gameObject.AddComponent <RectTransform>();
            }

            textUI.text = layer.Text;
            textUI.font = font;
            textUI.horizontalOverflow = HorizontalWrapMode.Overflow;
            textUI.verticalOverflow   = VerticalWrapMode.Overflow;
            textUI.raycastTarget      = false;

            //描边信息
            if (layer.TextOutlineColor.a != 0f)
            {
                Outline outline = textUI.GetComponent <Outline>();
                if (outline == null)
                {
                    outline = textUI.gameObject.AddComponent <Outline>();
                }

                outline.effectColor    = layer.TextOutlineColor;
                outline.effectDistance = new Vector2(layer.OutLineDis, layer.OutLineDis);
            }

            float fontSize = layer.FontSize;
            float ceiling  = Mathf.Ceil(fontSize);

            textUI.fontSize  = (int)fontSize;
            textUI.color     = color;
            textUI.alignment = TextAnchor.MiddleCenter;

            switch (layer.Justification)
            {
            case TextJustification.Left:
                textUI.alignment = TextAnchor.MiddleLeft;
                break;

            case TextJustification.Right:
                textUI.alignment = TextAnchor.MiddleRight;
                break;

            case TextJustification.Center:
                textUI.alignment = TextAnchor.MiddleCenter;
                break;
            }

            SetAnchor(rectTransform, layer.Name);

            UINode node = new UINode();

            node.rect = layer.Rect;
            node.Go   = gameObject;
            PsdUtils.SetNodeName(node, gameObject.name);
            return(node);
        }
Пример #12
0
        public static UINode CreateProgress(Layer layer)
        {
            Direction direction = Direction.LeftToRight;

            if (layer.Name.ContainsIgnoreCase(RIGHT2LEFT))
            {
                direction = Direction.RightToLeft;
            }
            else if (layer.Name.ContainsIgnoreCase(TOP2BUTTOM))
            {
                direction = Direction.TopToButtom;
            }
            else if (layer.Name.ContainsIgnoreCase(BUTTOM2TOP))
            {
                direction = Direction.ButtomToTop;
            }

            Layer fgLayer = null;
            Layer bgLayer = null;

            foreach (var child in layer.Children)
            {
                if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child))
                {
                    if (child.Name.ContainsIgnoreCase("@fg"))
                    {
                        fgLayer = child;
                    }
                    if (child.Name.ContainsIgnoreCase("@bg"))
                    {
                        bgLayer = child;
                    }
                }
            }
            if (fgLayer == null)
            {
                Debug.LogError("progress can't find @fg");
                return(null);
            }

            //GameObject go
            GameObject progressGo = new GameObject(layer.Name);

            float         width         = fgLayer.Rect.width;
            float         height        = fgLayer.Rect.height;
            RectTransform rectTransform = progressGo.AddComponent <RectTransform>();

            rectTransform.sizeDelta = new Vector2(width, height);

            GameObject    fgGo            = new GameObject("Foreground");
            RectTransform fgRectTransform = fgGo.AddComponent <RectTransform>();
            Image         fgImage         = fgGo.AddComponent <Image>();

            fgRectTransform.sizeDelta = new Vector2(width, height);
            PsdImporter.CreateSprite(fgLayer, fgImage);

            GameObject    maskGo            = new GameObject("Mask");
            RectTransform maskRectTransform = maskGo.AddComponent <RectTransform>();
            Image         maskImg           = maskGo.AddComponent <Image>();

            maskGo.AddComponent <UnityEngine.UI.Mask>();
            maskRectTransform.sizeDelta = new Vector2(width, height);
            //maskImg.sprite = Resources.Load<Sprite>("unity_builtin_extra/UIMask");
            maskImg.color = new Color(1, 1, 1, 0.01f);

            GameObject    bgGo;
            RectTransform bgRectTransform = null;

            if (bgLayer != null)
            {
                float bgWidth  = bgLayer.Rect.width;
                float bgHeight = bgLayer.Rect.height;

                bgGo            = new GameObject("Background");
                bgRectTransform = bgGo.AddComponent <RectTransform>();
                Image bgImage = bgGo.AddComponent <Image>();
                bgRectTransform.sizeDelta = new Vector2(bgWidth, bgHeight);
                PsdImporter.CreateSprite(bgLayer, bgImage);
                bgRectTransform.SetParent(rectTransform);
                bgRectTransform.localPosition = Vector3.zero;
                layer.Children.Remove(bgLayer);
            }



            Vector2 pivotOrAnchor = Vector2.zero;

            switch (direction)
            {
            case Direction.LeftToRight: pivotOrAnchor = new Vector2(0, 0.5f); break;

            case Direction.RightToLeft: pivotOrAnchor = new Vector2(1, 0.5f); break;

            case Direction.TopToButtom: pivotOrAnchor = new Vector2(0.5f, 1); break;

            case Direction.ButtomToTop: pivotOrAnchor = new Vector2(0.5f, 0); break;
            }


            maskRectTransform.SetParent(rectTransform);
            maskRectTransform.localPosition = new Vector3((pivotOrAnchor.x - 0.5f) * fgLayer.Rect.width, (pivotOrAnchor.y - 0.5f) * fgLayer.Rect.height, 0);

            fgRectTransform.SetParent(maskRectTransform);
            fgRectTransform.localPosition = Vector3.zero;

            maskRectTransform.pivot   = pivotOrAnchor;
            fgRectTransform.pivot     = pivotOrAnchor;
            fgRectTransform.anchorMax = pivotOrAnchor;
            fgRectTransform.anchorMin = pivotOrAnchor;

            layer.Children.Remove(fgLayer);

            SetAnchor(rectTransform, layer.Name);

            UINode node = new UINode();

            node.rect = fgLayer.Rect;
            node.Go   = progressGo;
            PsdUtils.SetNodeName(node, progressGo.name);
            return(node);
        }
Пример #13
0
        public static UINode CreateScrollRect(Layer layer)
        {
            bool horizontal = false;
            bool vertical   = true;

            if (layer.Name.Contains(RIGHT2LEFT) || layer.Name.Contains(LEFT2RIGHT))
            {
                horizontal = true;
            }
            if (layer.Name.Contains(TOP2BUTTOM) || layer.Name.Contains(BUTTOM2TOP))
            {
                vertical = true;
            }

            Layer sizeLayer = null;

            foreach (var child in layer.Children)
            {
                if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child))
                {
                    if (child.Name.ContainsIgnoreCase("@size"))
                    {
                        sizeLayer = child;
                    }
                }
            }
            if (sizeLayer == null)
            {
                Debug.LogError("Scroll Rect can't find @size");
                return(null);
            }

            GameObject    scrollRectGo  = new GameObject(layer.Name);
            RectTransform rectTransform = scrollRectGo.AddComponent <RectTransform>();
            ScrollRect    scrollRect    = scrollRectGo.AddComponent <ScrollRect>();

            float width  = sizeLayer.Rect.width;
            float height = sizeLayer.Rect.height;

            rectTransform.sizeDelta = new Vector2(width, height);

            GameObject    viewPortGo        = new GameObject("Viewport");
            RectTransform viewPortTransform = viewPortGo.AddComponent <RectTransform>();

            viewPortGo.AddComponent <UnityEngine.UI.Mask>();
            Image maskImage = viewPortGo.AddComponent <Image>();

            maskImage.color             = new Color(1, 1, 1, 0.01f);
            viewPortTransform.sizeDelta = new Vector2(width, height);

            GameObject    contentGo        = new GameObject("Content");
            RectTransform contentTransform = contentGo.AddComponent <RectTransform>();

            contentTransform.sizeDelta = new Vector2(width, height);

            scrollRect.viewport = viewPortTransform;
            scrollRect.content  = contentTransform;

            scrollRect.vertical   = vertical;
            scrollRect.horizontal = horizontal;

            viewPortTransform.anchorMax = new Vector2(1, 1);
            viewPortTransform.anchorMin = new Vector2(0, 0);
            viewPortTransform.pivot     = new Vector2(0, 1);

            contentTransform.anchorMax = new Vector2(0, 1);
            contentTransform.anchorMin = new Vector2(0, 1);
            contentTransform.pivot     = new Vector2(0, 1);


            UINode scrollNode = new UINode();

            scrollNode.rect = sizeLayer.Rect;
            scrollNode.Go   = scrollRectGo;
            PsdUtils.SetNodeName(scrollNode, scrollRectGo.name);

            UINode viewportNode = new UINode();

            viewportNode.rect  = sizeLayer.Rect;
            viewportNode.Go    = viewPortGo;
            viewportNode.pivot = viewPortTransform.pivot;


            UINode contentNode = new UINode();

            contentNode.rect  = sizeLayer.Rect;
            contentNode.Go    = contentGo;
            contentNode.pivot = contentTransform.pivot;

            scrollNode.children.Add(viewportNode);
            viewportNode.children.Add(contentNode);

            layer.Children.Remove(sizeLayer);

            SetAnchor(rectTransform, layer.Name);

            PsdImporter.ExportTree(layer.Children, contentNode);

            layer.Children.Clear();

            return(scrollNode);
        }