NLDClient-yudde/ProjectNLD/Assets/Editor/Scene/SplitTextureUtil.cs

90 lines
3.4 KiB
C#
Raw Normal View History

2023-12-25 15:38:15 +08:00
using System;
using System.IO;
2026-01-13 13:20:29 +08:00
using PhxhSDK;
2023-12-25 15:38:15 +08:00
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class SplitTextureUtil
{
2025-02-28 14:10:39 +08:00
[MenuItem("Assets/*Custom/贴图/切图")]
2023-12-25 15:38:15 +08:00
public static void SplitTexture()
{
var texture = Selection.activeObject as Texture2D;
if (texture)
{
var path = AssetDatabase.GetAssetPath(texture);
2024-01-05 14:48:47 +08:00
var dir = Path.GetDirectoryName(path).Replace("\\", "/");
SplitTexture(texture, 1024, 1024, dir, "");
2023-12-25 15:38:15 +08:00
}
}
2024-01-05 14:48:47 +08:00
/// <summary>
/// 分割贴图
/// </summary>
/// <param name="texture">要分割的贴图</param>
/// <param name="width">分割后的块的宽度(像素)</param>
/// <param name="height">分割后的块的高度(像素)</param>
/// <param name="directory">分割后贴图的位置</param>
/// <param name="prefix">文件名前缀(可选)</param>
public static Texture2D[,] SplitTexture(Texture2D texture, int width, int height, string directory, string prefix = "")
2023-12-25 15:38:15 +08:00
{
2024-03-13 16:07:27 +08:00
var row = Mathf.CeilToInt(texture.height * 1f / height);
var column = Mathf.CeilToInt(texture.width * 1f / width);
2023-12-25 15:38:15 +08:00
var rowReminder = texture.height % height;
2024-03-13 16:07:27 +08:00
if (rowReminder == 0)
rowReminder = height;
2023-12-25 15:38:15 +08:00
var columnReminder = texture.width % width;
2024-03-13 16:07:27 +08:00
if (columnReminder == 0)
columnReminder = width;
2024-01-05 14:48:47 +08:00
Texture2D[,] result = new Texture2D[row, column];
2023-12-25 15:38:15 +08:00
var allPixels = texture.GetPixels();
for (int r = 0; r < row; r++)
{
for (int c = 0; c < column; c++)
{
var realWidth = c == column - 1 ? columnReminder : width;
var realHeight = r == row - 1 ? rowReminder : height;
var pixelCount = realWidth * realHeight;
2026-01-13 13:20:29 +08:00
var textureOutput = TextureUtils.Create($"split_{texture.name}_{r}_{c}", realWidth, realHeight);
2023-12-25 15:38:15 +08:00
Color[] pixels = new Color[pixelCount];
var startIndex = r * height * texture.width + c * width;
for (int i = 0; i < pixelCount; i++)
{
var innerRow = i / realWidth;
var innerColumn = i % realWidth;
var pickIndex = startIndex + innerRow * texture.width + innerColumn;
pixels[i] = allPixels[pickIndex];
}
textureOutput.SetPixels(pixels);
Byte[] bytes = textureOutput.EncodeToPNG();
var path = $"{directory}/{prefix}split_{texture.name}_{r}_{c}.jpg";
2023-12-25 15:38:15 +08:00
FileStream fs = File.Open(path, FileMode.Create);
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(bytes);
writer.Flush();
writer.Close();
fs.Close();
Object.DestroyImmediate(textureOutput);
AssetDatabase.Refresh();
// 获取图片设置
var importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null)
{
importer.alphaIsTransparency = true;
importer.SaveAndReimport();
}
AssetDatabase.Refresh();
2024-01-05 14:48:47 +08:00
result[r, c] = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
2023-12-25 15:38:15 +08:00
}
}
2024-01-05 14:48:47 +08:00
return result;
2023-12-25 15:38:15 +08:00
}
}