コードインスペクション:マネージドインデクサーへのアクセスはサポートされていません
このインスペクションは、Unity の Burst コンパイラーでコンパイルされたメソッド内で、管理対象オブジェクト (クラス) に対してインデクサ (obj[index] など) を使用しようとする試みを検出します。
Burst は高性能コード向けに設計されており、C# のサブセットのうち「ブリッタブル」なもののみをサポートし、マネージドオブジェクトとガベージコレクションを回避します。 マネージドクラスはヒープに格納され、ガベージコレクションを必要とするため、Burst でコンパイルされたコード内から安全にアクセスすることはできません。
仕組み
アナライザーは、Burst コンパイルされたコンテキスト(たとえば、 [BurstCompile] 属性を持つジョブ)内のすべての要素アクセス式ノード(myList[0] や myDictionary["key"] のようなコード)を監視します。
これは、要素の基となるプロパティ(インデクサー)へのアクセスを解決し、包含する型をチェックします。 包含する型がクラス(管理型)であり、標準の C# 配列(Burst では特別な処理が行われます)でない場合、警告が発生します。
サンプル
この例では、 List<T> がマネージドクラスであるため、 managedList[0] へのアクセスがフラグ付けされています。 NativeArray<T> に置き換えることで、この問題は解決します。これは Blittable 構造体だからです。
using Unity.Burst;
using Unity.Jobs;
using System.Collections.Generic;
[BurstCompile]
public struct MyJob : IJob
{
public List<int> managedList; // Managed reference!
public void Execute()
{
// Reported: Accessing a managed indexer from 'System.Collections.Generic.List<int>' is not supported
int value = managedList[0];
}
}
using Unity.Burst;
using Unity.Jobs;
using Unity.Collections;
[BurstCompile]
public struct MyJob : IJob
{
// Use Native containers instead of managed ones
public NativeArray<int> nativeArray;
public void Execute()
{
// Safe: NativeArray is a blittable struct and its indexer is supported by Burst
int value = nativeArray[0];
}
}
2026 年 6 月 12 日