예제 #1
0
        private bool LoadImage()
        {
            settings = new PsdExportSettings(image);
            bool valid = (settings.Psd != null);

            if (valid)
            {
                // Parse the layer info
                fileInfo = new PsdFileInfo(settings.Psd);
            }
            return(valid);
        }
예제 #2
0
        public static Sprite CreateSprite(PsdExportSettings settings, Layer layer)
        {
            Texture2D tex = CreateTexture(layer);

            if (tex == null)
            {
                return(null);
            }
            Sprite sprite = SaveAsset(settings, tex, "_" + layer.Name);

            Object.DestroyImmediate(tex);
            return(sprite);
        }
예제 #3
0
        public static void Export(PsdExportSettings settings, PsdFileInfo fileInfo)
        {
            for (int i = 0; i < settings.Psd.Layers.Count; i++)
            {
                var groupInfo = fileInfo.GetGroupByLayerIndex(i);
                if (groupInfo != null && !groupInfo.visible)
                {
                    continue;
                }

                if (!fileInfo.LayerVisibility[i])
                {
                    continue;
                }

                var layer = settings.Psd.Layers[i];
                CreateSprite(settings, layer);
            }
            settings.SaveMetaData();
        }
예제 #4
0
        private static Sprite SaveAsset(PsdExportSettings settings, Texture2D tex, string suffix)
        {
            string assetPath = AssetDatabase.GetAssetPath(settings.Image);
            string path      = Path.Combine(Path.GetDirectoryName(assetPath),
                                            Path.GetFileNameWithoutExtension(assetPath) + suffix + ".png");

            if (settings.ScaleBy > 0)
            {
                int width    = Mathf.RoundToInt(tex.width / 2);
                int height   = Mathf.RoundToInt(tex.height / 2);
                int mipLevel = 1;
                if (settings.ScaleBy == 2)
                {
                    width    = Mathf.RoundToInt(tex.width / 4);
                    height   = Mathf.RoundToInt(tex.height / 4);
                    mipLevel = 2;
                }
                Texture2D resized = new Texture2D(width, height);
                resized.SetPixels32(tex.GetPixels32(mipLevel));
                resized.Apply();
                tex = resized;
            }

            byte[] buf = tex.EncodeToPNG();
            File.WriteAllBytes(path, buf);
            AssetDatabase.Refresh();

            // Load the texture so we can change the type
            var             textureObj      = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
            TextureImporter textureImporter = (TextureImporter)AssetImporter.GetAtPath(path);

            textureImporter.textureType         = TextureImporterType.Sprite;
            textureImporter.spriteImportMode    = SpriteImportMode.Single;
            textureImporter.spritePivot         = settings.PivotVector;
            textureImporter.spritePixelsToUnits = settings.PixelsToUnitSize;
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
            EditorUtility.SetDirty(textureObj);

            return((Sprite)AssetDatabase.LoadAssetAtPath(path, typeof(Sprite)));
        }