Q&A
前提
バージョン : 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の中には配列がないように思うのですが、どこでエラーが出ているのでしょうか。
回答よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/04/07 00:05