3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Texture2Dが半透明ピクセルを持っているかどうか調べる

Last updated at Posted at 2020-08-04

☆記事最後に追記あり

Twitterに書いたTextureが半透明ピクセルを持っているかどうか調べるコードを貼っておきます

いや、特に簡単に調べる方法は見つかっていないんだけれども…。
それでもまぁ再利用することを考えると、ピクセルを舐めるコードでも良いから記事化しておこうと思います。

TextureUtility.cs
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;

namespace ScreenPocket
{
	public static class TextureUtility
	{
		//Texture2Dに半透明ピクセルがあるかどうか調べる
		public static bool ExistsTransparentPixel(Texture2D texture)
		{
			var path = AssetDatabase.GetAssetPath(texture);
			var importer = AssetImporter.GetAtPath(path) as TextureImporter;
			if (importer == null)
			{
				Debug.LogError("Not Found TextureImporter! : " + path);
				return false;
			}

			bool isExist = false;
			//readableでないとGetPixels()出来ないので変更
			var keepReadable = importer.isReadable;
			importer.isReadable = true;
			importer.SaveAndReimport();
			var pixels = texture.GetPixels32();
			for (int i = 0, count = pixels.Length; i < count; ++i)
			{
				if (pixels[i].a != 0xff)
				{
					isExist = true;
					break;
				}
			}

			//readableを戻す
			importer.isReadable = keepReadable;
			importer.SaveAndReimport();
			AssetDatabase.Refresh();
			return isExist;
		}

		[MenuItem("Assets/ScreenPocket/Texture/ExistsTransparentPixel")]
		static void CheckExistsTransparentPixel()
		{
			var texture = Selection.activeObject as Texture2D;
			if (texture == null)
			{
				Debug.LogError("No Texture.");
				return;
			}

			if (ExistsTransparentPixel(texture))
			{
				Debug.Log("Transparency Texture!");
			}
			else
			{
				Debug.Log("Opaque Texture!");
			}
		}
	}
}

Projectツリーでテクスチャを右クリック > ScreenPocket > Texture > ExistsTransparentPixel で、
半透明ピクセルを持っているかどうかでLogが変わります。
もっとスマートな方法はないものか・・・。

☆追記

とのことで、ピクセルを舐めている部分を**importer.DoesSourceTextureHaveAlpha()**に置き換えることで解決しました! コレが欲しかったんや…! 修正後のコードはこちら↓
TextureUtility.cs
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;

namespace ScreenPocket
{
    public static class TextureUtility
    {
        //Texture2Dに半透明ピクセルがあるかどうか調べる
        public static bool ExistsTransparentPixel(Texture2D texture)
        {
            var path = AssetDatabase.GetAssetPath(texture);
            var importer = AssetImporter.GetAtPath(path) as TextureImporter;
            if (importer == null)
            {
                Debug.LogError("Not Found TextureImporter! : " + path);
                return false;
            }
            
            return importer.DoesSourceTextureHaveAlpha();
        }

        [MenuItem("Assets/ScreenPocket/Texture/ExistsTransparentPixel")]
        static void CheckExistsTransparentPixel()
        {
            var texture = Selection.activeObject as Texture2D;
            if (texture == null)
            {
                Debug.LogError("No Texture.");
                return;
            }

            if (ExistsTransparentPixel(texture))
            {
                Debug.Log("Transparency Texture!");
            }
            else
            {
                Debug.Log("Opaque Texture!");
            }
        }
    }
}
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?