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

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

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

1644閲覧

「SpriteProcessorScript」で「Retrieving array element that was out of bounds」というエラーがでる

kurokko

総合スコア15

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2022/04/06 02:19

前提

バージョン : Unity 2020.3.32f1

発生している問題・エラーメッセージ

https://github.com/dithyrambs/SpriteProcessorScript
のSpriteProcessorを利用して、スプライトを Multiple にして自動でスライスできるようにしようと思ったところ、以下のようなエラーが出てしまいました。
イメージ説明

Retrieving array element that was out of bounds UnityEditor.AssetPostprocessingInternal:PostprocessSprites (UnityEngine.Texture2D,string,UnityEngine.Sprite[])
NullReferenceException: Object reference not set to an instance of an object UnityEditor.U2D.Sprites.SpriteBoneDataTransfer.Load (UnityEditor.SerializedObject importer, UnityEditor.SpriteImportMode mode, System.Int32 index) (at Library/PackageCache/com.unity.2d.sprite@1.0.0/Editor/SpriteEditorModule/TextureImporterDataProviderImplementation.cs:37) UnityEditor.U2D.Sprites.SpriteBoneDataTransfer.GetBones (UnityEditor.GUID guid) (at Library/PackageCache/com.unity.2d.sprite@1.0.0/Editor/SpriteEditorModule/TextureImporterDataProviderImplementation.cs:27) UnityEditor.U2D.Animation.SpritePostProcess.PostProcessBoneData (UnityEditor.U2D.Sprites.ISpriteEditorDataProvider spriteDataProvider, System.Single definitionScale, UnityEngine.Sprite[] sprites) (at Library/PackageCache/com.unity.2d.animation@5.1.1/Editor/SpritePostProcess.cs:107) UnityEditor.U2D.Animation.SpritePostProcess.OnPostprocessSprites (UnityEngine.Texture2D texture, UnityEngine.Sprite[] sprites) (at Library/PackageCache/com.unity.2d.animation@5.1.1/Editor/SpritePostProcess.cs:29) System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <695d1cc93cca45069c528c15c9fdd749>:0) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <695d1cc93cca45069c528c15c9fdd749>:0) UnityEditor.AssetPostprocessingInternal.InvokeMethodIfAvailable (System.Object target, System.String methodName, System.Object[] args) (at <294703faec74477f8b275446429fba82>:0) UnityEditor.AssetPostprocessingInternal.CallPostProcessMethods (System.String methodName, System.Object[] args) (at <294703faec74477f8b275446429fba82>:0) UnityEditor.AssetPostprocessingInternal.PostprocessSprites (UnityEngine.Texture2D tex, System.String pathName, UnityEngine.Sprite[] sprites) (at <294703faec74477f8b275446429fba82>:0)

該当のソースコード

c#

1using System.Collections.Generic; 2using System.IO; 3using UnityEngine; 4using UnityEditor; 5 6/// <summary> 7/// Note: Will only work when manually triggering "Reimport" on a file, not when importing a file to Unity for the first time. 8/// In order for this to work the grandparent directory must be names "AutoProcessedSpriteSheets" 9/// The immediate parent directory must be named the desired horizontal tile size of the sliced spritesheet in pixels, plus underscore... 10/// plus the desired horizontal tile size of the sliced spritesheet in pixels, plus underscore... 11/// plus the desired pixels per unit 12/// eg. "16__12_100" 13/// </summary> 14public class SpriteProcessor : AssetPostprocessor 15{ 16 bool continueProcess = false; 17 int ppu = 16; 18 int spriteSizeX = 16; 19 int spriteSizeY = 16; 20 21 void OnPreprocessTexture() 22 { 23 if (Directory.GetParent(assetPath).Parent.Name != "AutoProcessedSpriteSheets") return; 24 25 TextureImporter ti = (TextureImporter)assetImporter; 26 ti.textureType = TextureImporterType.Sprite; 27 ti.spriteImportMode = SpriteImportMode.Multiple; 28 ti.mipmapEnabled = false; 29 ti.filterMode = FilterMode.Point; 30 ti.textureCompression = TextureImporterCompression.Uncompressed; 31 32 string[] parentDir = Directory.GetParent(assetPath).Name.Split('_'); 33 34 //If we can't parse the parent directory values as integers, don't continue processing the sprite 35 if ( System.Int32.TryParse(parentDir[0], out spriteSizeX) && System.Int32.TryParse(parentDir[1], out spriteSizeY) && System.Int32.TryParse(parentDir[2], out ppu) ) 36 { 37 ti.spritePixelsPerUnit = ppu; 38 continueProcess = true; 39 } 40 } 41 42 public void OnPostprocessTexture(Texture2D texture) 43 { 44 //If we failed requirement during the PreProcess stage, return 45 Debug.LogWarning("******"); 46 if (!continueProcess) return; 47 Debug.LogWarning("***returnされなかったため、メインの処理に入ります***"); 48 49 50 if ( spriteSizeX > 0 && spriteSizeY > 0) 51 { 52 int colCount = texture.width / spriteSizeX; 53 int rowCount = texture.height / spriteSizeY; 54 string fileName = System.IO.Path.GetFileNameWithoutExtension(assetPath); 55 int i = 0; 56 57 58 List<SpriteMetaData> metas = new List<SpriteMetaData>(); 59 60 61 for (int r = rowCount - 1; r > -1; r--) 62 { 63 for (int c = 0; c < colCount; c++) 64 { 65 66 SpriteMetaData meta = new SpriteMetaData(); 67 meta.rect = new Rect(c * spriteSizeX, r * spriteSizeY, spriteSizeX, spriteSizeY); 68 meta.name = fileName + "_" + i; 69 metas.Add(meta); 70 i++; 71 } 72 } 73 74 TextureImporter textureImporter = (TextureImporter)assetImporter; 75 textureImporter.spritesheet = metas.ToArray(); 76 77 } 78 } 79 80} 81

質問

OnPostprocessTetureの中には配列がないように思うのですが、どこでエラーが出ているのでしょうか。

回答よろしくお願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

どうやら2D Animationがインストールされていると発生するようですね。エラーメッセージは出るもののスプライトの縦横分割自体はできているように見えましたので、お気になさらない...というのでも実害はないかもしれません。

あれを回避するとなると、プリプロセスの段階でスプライトの分割を行ってしまえばいいのではないか...と思って下記のような形を考えてみましたが、いろんなテクスチャで試したわけではないので不具合を見落としているかもしれません。

C#

1using System; 2using System.Collections.Generic; 3using System.IO; 4using System.Reflection; 5using UnityEngine; 6using UnityEditor; 7 8/// <summary> 9/// In order for this to work the grandparent directory must be names "AutoProcessedSpriteSheets" 10/// The immediate parent directory must be named the desired horizontal tile size of the sliced spritesheet in pixels, plus underscore... 11/// plus the desired vertical tile size of the sliced spritesheet in pixels, plus underscore... 12/// plus the desired pixels per unit 13/// eg. "16_12_100" 14/// </summary> 15public class SpriteProcessor : AssetPostprocessor 16{ 17 int ppu = 16; 18 int spriteSizeX = 16; 19 int spriteSizeY = 16; 20 21 delegate void GetWidthAndHeightDelegate(TextureImporter importer, ref int width, ref int height); 22 23 GetWidthAndHeightDelegate getWidthAndHeight; 24 25 GetWidthAndHeightDelegate GetWidthAndHeight => 26 getWidthAndHeight ??= (GetWidthAndHeightDelegate)Delegate.CreateDelegate( 27 typeof(GetWidthAndHeightDelegate), 28 typeof(TextureImporter).GetMethod( 29 "GetWidthAndHeight", 30 BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic)); 31 32 void OnPreprocessTexture() 33 { 34 if (Directory.GetParent(assetPath).Parent.Name != "AutoProcessedSpriteSheets") return; 35 36 TextureImporter ti = (TextureImporter)assetImporter; 37 ti.textureType = TextureImporterType.Sprite; 38 ti.spriteImportMode = SpriteImportMode.Multiple; 39 ti.mipmapEnabled = false; 40 ti.filterMode = FilterMode.Point; 41 ti.textureCompression = TextureImporterCompression.Uncompressed; 42 43 string[] parentDir = Directory.GetParent(assetPath).Name.Split('_'); 44 45 //If we can't parse the parent directory values as integers, don't continue processing the sprite 46 if (int.TryParse(parentDir[0], out spriteSizeX) && int.TryParse(parentDir[1], out spriteSizeY) && int.TryParse(parentDir[2], out ppu)) 47 { 48 ti.spritePixelsPerUnit = ppu; 49 if (spriteSizeX > 0 && spriteSizeY > 0) 50 { 51 int textureWidth = 0; 52 int textureHeight = 0; 53 GetWidthAndHeight(ti, ref textureWidth, ref textureHeight); 54 int colCount = textureWidth / spriteSizeX; 55 int rowCount = textureHeight / spriteSizeY; 56 string fileName = Path.GetFileNameWithoutExtension(assetPath); 57 int i = 0; 58 59 List<SpriteMetaData> metas = new List<SpriteMetaData>(); 60 61 for (int r = rowCount - 1; r > -1; r--) 62 { 63 for (int c = 0; c < colCount; c++) 64 { 65 SpriteMetaData meta = new SpriteMetaData(); 66 meta.rect = new Rect(c * spriteSizeX, r * spriteSizeY, spriteSizeX, spriteSizeY); 67 meta.name = fileName + "_" + i; 68 metas.Add(meta); 69 i++; 70 } 71 } 72 73 ti.spritesheet = metas.ToArray(); 74 } 75 } 76 } 77}

投稿2022/04/06 22:52

編集2022/04/06 23:43
Bongo

総合スコア10807

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

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

kurokko

2022/04/07 00:05

回答ありがとうございます! 無事、エラーなく分割できるようになりました。 全ての処理をOnPreprocessTextureに書くことによって、スプライトを最初に読み込んだ時にも分割が行われるようになったんですね。 これでゲーム開発が捗ります。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問