質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

1974閲覧

Unity SetPixelsでのエラー”needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats”

Linkins

総合スコア82

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/10/09 16:54

Unity2Dでいわゆるお絵描きアプリを作っています。
あらかじめ用意したSprite上でマウスをドラッグして色を塗りたいのですが、
"Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats"
というエラーに阻まれて上手くいきません。
どこを間違えているのか、ご教授お願い致します。

試したこと:
Spriteの元の画像のImportSettingsで、"Read/Write Enabled"にチェック。
PC,Mac&Linux Standalone Settingsから、"Override for PC~~"にチェック。
Formatを"RGBA 32bit"に変更。
RGBA32bit以外にもエラー文で推奨されているformatは全て試したのですが、どれも変わらず上記のエラーが出てしまいました。

以下が色を塗るスクリプトです。

public class Nuru : MonoBehaviour { public SpriteRenderer sp; private Texture2D tx; private Brush brush; public class Brush { public int tate = 5; public int yoko = 5; public Color brushColor = new Color(0,0,0,0);//元が真っ黒なSpriteで、塗った場所が透明になるようにしようとしています。 public Color[] brushArea; public void BrushSet() { brushArea = new Color[tate * yoko]; for(int x = 0; x < brushArea.Length; x++) { brushArea[x] = brushColor; } } } // Use this for initialization void Start () { tx = sp.sprite.texture; brush = new Brush(); } // Update is called once per frame void Update () { if (Input.GetMouseButton(0)) { DrawLine(Input.mousePosition); } } private void DrawLine(Vector2 pos) { tx.SetPixels((int)pos.x, (int)pos.y, brush.yoko, brush.tate, brush.brushArea); tx.Apply(); } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

追記以来欄に書くべき内容かと思いますが、画像を載せたかったため回答欄を使用しました。ご容赦ください。

インスペクタの表示はどうなっているでしょうか?特に、最下部のプレビュー欄にフォーマットが表示されるかと思いますが、正しく「RGBA 32bit」になっているでしょうか。
インスペクタ
あるいは、コード中にDebug.Log(tx.format);などと入れてみますと、「RGBA32」と出るでしょうか?
ご質問者さんのスクリプトを動かしてみましたが、試した限りではちゃんと動作するようでした。
プレビュー
念のためその時のスクリプトも記載しますが、Debug.Log(tx.format);brush.BrushSet();を入れたことぐらいしか違いはないかと思います。

C#

1using UnityEngine; 2 3public class Nuru : MonoBehaviour 4{ 5 6 public SpriteRenderer sp; 7 private Texture2D tx; 8 private Brush brush; 9 10 public class Brush 11 { 12 public int tate = 5; 13 public int yoko = 5; 14 public Color brushColor = new Color(0, 0, 0, 0);//元が真っ黒なSpriteで、塗った場所が透明になるようにしようとしています。 15 public Color[] brushArea; 16 17 public void BrushSet() 18 { 19 brushArea = new Color[tate * yoko]; 20 for (int x = 0; x < brushArea.Length; x++) 21 { 22 brushArea[x] = brushColor; 23 } 24 } 25 26 } 27 28 // Use this for initialization 29 void Start() 30 { 31 tx = sp.sprite.texture; 32 Debug.Log(tx.format); 33 brush = new Brush(); 34 brush.BrushSet(); // BrushSetが行われていなかったようなので追加しました 35 } 36 37 // Update is called once per frame 38 void Update() 39 { 40 41 if (Input.GetMouseButton(0)) 42 { 43 DrawLine(Input.mousePosition); 44 } 45 } 46 47 private void DrawLine(Vector2 pos) 48 { 49 tx.SetPixels((int)pos.x, (int)pos.y, brush.yoko, brush.tate, brush.brushArea); 50 tx.Apply(); 51 } 52}

投稿2018/10/09 22:04

Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Linkins

2018/10/10 08:58

解決しました! 原因はBrushSet()の書き忘れと、PlatformをiOSにしていたのにImageのImportSettingsでiOS向けの設定をしていなかったことでした。 Debug.Log(tx.format)のおかげで気付くことができました、ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問