Skip to content

Instantly share code, notes, and snippets.

@sapphire-al2o3
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sapphire-al2o3/1387dae04b718769f3c6 to your computer and use it in GitHub Desktop.
Save sapphire-al2o3/1387dae04b718769f3c6 to your computer and use it in GitHub Desktop.
Unityでキューブマップのテクスチャを取り出すエディタスクリプト
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
public class GetCubemapTextureEditor : Editor
{
public static void FlipXY(Color[] pixels, int width, int height)
{
for (int i = 0; i < height / 2; i++)
{
for (int j = 0; j < width; j++)
{
int p = i * width + j;
int q = (height - i - 1) * width + (width - j - 1);
Color t = pixels[p];
pixels[p] = pixels[q];
pixels[q] = t;
}
}
}
[MenuItem("Editor/Get Cubemap Texture")]
public static void GetCubemapTexture()
{
Cubemap cubemap = Selection.activeObject as Cubemap;
if (cubemap)
{
int width = cubemap.width;
int height = cubemap.height;
Texture2D[] textures = new Texture2D[6];
for (int i = 0; i < 6; i++)
{
Color[] p = cubemap.GetPixels((CubemapFace)i);
FlipXY(p, width, height);
textures[i] = new Texture2D(width, height);
textures[i].SetPixels(p);
textures[i].name = cubemap.name + "_" + i.ToString();
string path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(cubemap)) + "/" + textures[i].name + ".png";
byte[] bytes = textures[i].EncodeToPNG();
File.WriteAllBytes(path, bytes);
bytes = null;
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment