トップ 一覧 検索 ヘルプ RSS ログイン

unity_script_texture2d_getpixelの変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!Texture2DからGetPixelする

Assetとして読み込んだテクスチャやResourcesとして読み込める外部テクスチャは、
そのままではGetPixel/GetPixelsでテクスチャのピクセル情報にアクセスできません。

「GetPixel」「GetPixels」を呼ぶとエラーになります。
 UnityException: Texture 'leaf_chip_01' is not readable,
  the texture memory can not be accessed from scripts.
 You can make the texture readable in the Texture Import Settings.

これのアクセス設定はImport Settingsにあります。
Assetsの画像を選択したInspectorで
「Texture Type」を「Advanced」に変更、
「Read/Write Enabled」のチェックボックスをOn、
「Default」のFormatを「RGB 24bit」または「RGBA 32bit」に変更、
としてApplyボタンを押します。
{{ref_image texture_resource_load.png}}
これで、リソースとして読み込んだテクスチャのGetPixelsを使用できます。

!!スクリプトで自動的にGetPixelできるようにする

以下はAssetDatabaseを使用しますので、android/iOSでは動作しません。
 要 : using UnityEditor;
以下のように一時的にテクスチャにアクセスできるようにすると、以降はGetPixel/GetPixelsも使用できるようになります。
 Texture2D tex = インポートしたテクスチャ.
 
 string assetPath   = AssetDatabase.GetAssetPath(m_tex);
 TextureImporter ti = TextureImporter.GetAtPath(assetPath) as TextureImporter;
 bool oldIsReadable = ti.isReadable;
 TextureImporterFormat oldImporterFormat = ti.textureFormat;
 ti.isReadable    = true;
 ti.textureFormat = TextureImporterFormat.RGBA32;
 AssetDatabase.ImportAsset(assetPath);

使い終わった後で、
 ti.isReadable    = oldIsReadable ;
 ti.textureFormat = oldImporterFormat ;
 ti.textureFormat = oldImporterFormat;
 AssetDatabase.ImportAsset(assetPath);
として元に戻せばOKです。

----
{{lastmodified}}