前提・実現したいこと
Unityのエディタ拡張として、Tilemapをファイルに書き込まれた情報を基に生成する処理を作っています。「該当のソースコード」に記載したGenerateTile()
関数をボタン押下で同一クラス内から呼び出しています。ファイルの全文は字数超過で記載できませんでした。
発生している問題・エラーメッセージ
「該当のソースコード」の後半にある、Addressables.LoadAssetAsync<TileBase>(tileLayout[a,b,c]) += op => {(省略)}
が実行されていないと思われます。
直前のDebug.Log("a");は問題無く実行されますが、ラムダ式(?)内のDebug.Log(op.Result);は実行されません。
何か分かる方は教えて戴けると幸いです。
該当のソースコード
※EventFunction.TILEMAP_MAXRANGE_X(Y)
はただの定数、中身は100です。
C#
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.IO; 5using System.Linq; 6using UnityEditor; 7using UnityEngine; 8using UnityEngine.AddressableAssets; 9using UnityEngine.ResourceManagement.AsyncOperations; 10using UnityEngine.Tilemaps; 11 12public class MapEditor : EditorWindow 13{ 14 (省略) 15 private void GenerateTile() 16 { 17 bool returns = EditorUtility.DisplayDialog("Attention", "If you continue this function, the tilemap detas will lose. If you want to continue, click \"continue\".", "continue", "cancel"); 18 if (!returns) 19 { 20 return; 21 } 22 23 Tilemap[] tilemaps = GameObject.FindGameObjectWithTag("tilemap").GetComponents<Tilemap>(); 24 25 //Debug.Log(tilemaps.Length); 26 27 for (int a = 0; a < tilemaps.Length; a++) 28 { 29 tilemaps[a].ClearAllTiles(); 30 tilemaps[a].size = new Vector3Int(EventFunctions.TILEMAP_MAXRANGE_X, EventFunctions.TILEMAP_MAXRANGE_Y, 1); 31 } 32 33 for (int a = 0; a < asyncOperationHandleTileBases.Count; a++) Addressables.Release(asyncOperationHandleTileBases[a]); 34 35 // 3次元配列定義(第1引数はレイヤー、第2、3は直交xy座標) 36 string[,,] tileLayout = new string[tilemaps.Length, EventFunctions.TILEMAP_MAXRANGE_X, EventFunctions.TILEMAP_MAXRANGE_Y]; 37 38 // タイル配列作成処理 39 if (tiles.Count > 0) 40 { 41 for (int a = 0; a < tiles.Count; a++) 42 { 43 int startX, startY, endX, endY; 44 string[] places = tiles[a].Place; 45 46 foreach (string place in places) 47 { 48 startX = int.Parse(place.Split(':')[1].Split('-')[0]); 49 startY = int.Parse(place.Split(':')[2].Split('-')[0]); 50 51 if (place.Split(':')[1].Split('-').Length == 2) 52 endX = int.Parse(place.Split(':')[1].Split('-')[1]); 53 else endX = startX; 54 55 if (place.Split(':')[2].Split('-').Length == 2) 56 endY = int.Parse(place.Split(':')[2].Split('-')[1]); 57 else endY = startY; 58 59 string tileName = tiles[a].Tile_Name; 60 int layerNum = int.Parse(place.Split(':')[0]); 61 62 //Debug.Log(place); 63 //Debug.Log(tileName); 64 //Debug.Log(layerNum); 65 66 for (int b = startX; b <= Math.Min(endX, EventFunctions.TILEMAP_MAXRANGE_X); b++) 67 { 68 for (int c = startY; c <= Math.Min(endY, EventFunctions.TILEMAP_MAXRANGE_Y); c++) 69 { 70 tileLayout[layerNum, b, c] = tileName; 71 } 72 } 73 } 74 } 75 } 76 77 Dictionary<string, AsyncOperationHandle<TileBase>> tileBaseStack = new Dictionary<string, AsyncOperationHandle<TileBase>>(); 78 79 // 配置 80 for (int a = 0; a < tilemaps.Length; a++) 81 { 82 for (int b = 0; b < EventFunctions.TILEMAP_MAXRANGE_X; b++) 83 { 84 for (int c = 0; c < EventFunctions.TILEMAP_MAXRANGE_Y; c++) 85 { 86 if (string.IsNullOrEmpty(tileLayout[a, b, c])) continue; 87 88 Debug.Log(tileLayout[a, b, c]); 89 90 if (!tileBaseStack.ContainsKey(tileLayout[a, b, c])) 91 { 92 Debug.Log("a"); 93 //問題発生箇所 94 Addressables.LoadAssetAsync<TileBase>(tileLayout[a, b, c]).Completed += op => 95 { 96 Debug.Log(op.Result); 97 tilemaps[a].SetTile(new Vector3Int(b, c, 0), op.Result); 98 asyncOperationHandleTileBases.Add(op); 99 tileBaseStack.Add(tileLayout[a, b, c], op); 100 }; 101 } 102 else 103 { 104 Debug.Log("b"); 105 tilemaps[a].SetTile(new Vector3Int(b, c, 0), tileBaseStack[tileLayout[a, b, c]].Result); 106 } 107 } 108 } 109 } 110 } 111 (省略) 112}
試したこと
- GenerateTile関数をIEnumeratorとしてみようと考えましたが、MapEditorクラスがMonoBehaviourを継承していないのでStartCoroutineが使えず断念しました。
補足情報(FW/ツールのバージョンなど)
Unity 2020.3.26f1

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/29 09:34
退会済みユーザー
2022/01/29 22:49