The XrAiAssets
class is a MonoBehaviour component that manages AI model assets required by local inference models. This class serves as a container for model files, configuration data, and other resources needed by AI pipelines.
public class XrAiAssets : MonoBehaviour
The XrAiAssets
component is designed to hold references to model assets that are used by local AI implementations, particularly for models that run inference directly within Unity using frameworks like Sentis.
// Attach XrAiAssets component to a GameObject
public class AIModelSetup : MonoBehaviour
{
[SerializeField] private XrAiAssets aiAssets;
void Start()
{
// Load object detector with assets
IXrAiObjectDetector objectDetector = XrAiFactory.LoadObjectDetector(
"Yolo",
new Dictionary<string, string>
{
{ "confidence", "0.5" }
},
aiAssets // Pass the assets component
);
}
}
The XrAiAssets
component is passed to factory methods that require local model assets:
// Object detection with local models
IXrAiObjectDetector detector = XrAiFactory.LoadObjectDetector(
name: "Yolo",
options: detectionOptions,
assets: aiAssetsComponent
);
While the current implementation is minimal, the class is designed to be extended for specific asset management needs:
// Example of how XrAiAssets might be extended
public class XrAiAssets : MonoBehaviour
{
[Header("YOLO Model Assets")]
[SerializeField] private TextAsset yoloModel;
[SerializeField] private TextAsset yoloLabels;
[Header("Custom Model Assets")]
[SerializeField] private TextAsset[] customModels;
// Properties to access assets
public TextAsset YoloModel => yoloModel;
public TextAsset YoloLabels => yoloLabels;
public TextAsset[] CustomModels => customModels;
}
// Create a GameObject in your scene
GameObject assetsObject = new GameObject("AI Assets");
XrAiAssets assets = assetsObject.AddComponent<XrAiAssets>();
XrAiAssets
component to a GameObjectThe assets component can be extended to support various model types:
[System.Serializable]
public class ModelAssetGroup
{
public string modelName;
public TextAsset modelFile;
public TextAsset labelFile;
public TextAsset configFile;
}
public class XrAiAssets : MonoBehaviour
{
[SerializeField] private ModelAssetGroup[] modelGroups;
public ModelAssetGroup GetModelGroup(string modelName)
{
return modelGroups.FirstOrDefault(g => g.modelName == modelName);
}
}
public class XrAiAssets : MonoBehaviour
{
private Dictionary<string, byte[]> loadedAssets = new Dictionary<string, byte[]>();
public byte[] GetModelData(string modelName)
{
if (!loadedAssets.ContainsKey(modelName))
{
// Load and cache model data
var modelAsset = GetModelAsset(modelName);
loadedAssets[modelName] = modelAsset.bytes;
}
return loadedAssets[modelName];
}
private void OnDestroy()
{
// Clean up cached assets
loadedAssets.Clear();
}
}
Implement validation to ensure required assets are properly configured:
public class XrAiAssets : MonoBehaviour
{
public bool ValidateAssets()
{
// Check if required assets are assigned
bool isValid = true;
if (yoloModel == null)
{
Debug.LogError("YOLO model asset is not assigned");
isValid = false;
}
if (yoloLabels == null)
{
Debug.LogError("YOLO labels asset is not assigned");
isValid = false;
}
return isValid;
}
}
MonoBehaviour
to leverage Unity’s asset system