【问题标题】:Can you export sliced sprites (PNG images) in Unity?你可以在 Unity 中导出切片精灵(PNG 图像)吗?
【发布时间】:2019-09-14 15:08:49
【问题描述】:

所以我有一个统一的 Sprite 2d 和 UI。 (PNG图像)包含许多小图像,如按钮。所以我将它们切片,我可以单独使用它们。但问题是我想导出我在切片时获得的每个 png 图像以供其他用途。那我可以这样做吗?并将它们作为单独的png?

而我想要的是这些图片:

在我的(比如说)桌面中将它们导出为单独的 png。

谢谢!

【问题讨论】:

    标签: unity3d png sprite slice


    【解决方案1】:

    使用 Photoshop,裁剪它,将进行大量剪切和粘贴,另存为 PNG。 将其导入文件夹内的统一,我喜欢你的设计。还可以在 Youtube 上观看 Brackeys 教程,https://www.youtube.com/user/Brackeys,他将我从学生时代拯救出来。

    (编辑:我推荐 Photoshop,因为它是我唯一知道的。)

    【讨论】:

      【解决方案2】:

      我也有这个需求,我最终用一个简单的编辑器脚本解决了它:

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      using UnityEditor;
      
      public class ExportSubSprites : Editor {
      
          [MenuItem("Assets/Export Sub-Sprites")]
          public static void DoExportSubSprites() {
              var folder = EditorUtility.OpenFolderPanel("Export subsprites into what folder?", "", "");
              foreach (var obj in Selection.objects) {
                  var sprite = obj as Sprite;
                  if (sprite == null) continue;
                  var extracted = ExtractAndName(sprite);
                  SaveSubSprite(extracted, folder);
              }
      
          }
      
          [MenuItem("Assets/Export Sub-Sprites", true)]
          private static bool CanExportSubSprites()
          {
              return Selection.activeObject is Sprite;
          }
      
          // Since a sprite may exist anywhere on a tex2d, this will crop out the sprite's claimed region and return a new, cropped, tex2d.
          private static Texture2D ExtractAndName(Sprite sprite) {
              var output = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
              var r = sprite.textureRect;
              var pixels = sprite.texture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height);
              output.SetPixels(pixels);
              output.Apply();
              output.name = sprite.texture.name + " " + sprite.name;
              return output;
          }
      
          private static void SaveSubSprite(Texture2D tex, string saveToDirectory) {
              if (!System.IO.Directory.Exists(saveToDirectory)) System.IO.Directory.CreateDirectory(saveToDirectory);
              System.IO.File.WriteAllBytes(System.IO.Path.Combine(saveToDirectory, tex.name + ".png"), tex.EncodeToPNG());
          }
      }
      

      首先,将其放入名为EditorSubSprites.cs 的脚本文件中,并确保它位于编辑器文件夹中。如果您没有编辑器文件夹,您可以简单地在 /Assets/Editor/ 处创建

      要使用,展开纹理资源的精灵,并选择您希望导出的任意数量的精灵。右键单击并选择“导出子精灵”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-07
        • 2014-10-27
        • 2022-12-05
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        相关资源
        最近更新 更多