The XrAiResult
class provides a unified result type for all AI operations in the XR AI Library. It encapsulates both success and error states, following a result pattern for error handling.
public abstract class XrAiResult
Indicates whether the operation completed successfully.
public bool IsSuccess { get; protected set; }
Contains the error message if the operation failed.
public string ErrorMessage { get; protected set; }
Creates a new instance of XrAiResult
.
protected XrAiResult(bool isSuccess, string errorMessage = null)
Parameters:
isSuccess
(bool): Whether the operation was successfulerrorMessage
(string, optional): Error message if the operation failedCreates a successful result with data.
public static XrAiResult<T> Success<T>(T data)
Parameters:
data
(T): The result dataReturns:
XrAiResult<T>
: A successful result containing the dataCreates a failed result with an error message.
public static XrAiResult<T> Failure<T>(string errorMessage)
Parameters:
errorMessage
(string): Description of the errorReturns:
XrAiResult<T>
: A failed result with the error messageThe generic XrAiResult<T>
class extends XrAiResult
to include typed data for successful operations.
public class XrAiResult<T> : XrAiResult
Contains the result data for successful operations.
public T Data { get; private set; }
Creates a new instance of XrAiResult<T>
.
internal XrAiResult(T data, bool isSuccess, string errorMessage = null)
Parameters:
data
(T): The result dataisSuccess
(bool): Whether the operation was successfulerrorMessage
(string, optional): Error message if the operation failedXrAiResult<string> result = await imageToText.Execute(imageBytes, "image/jpeg");
if (result.IsSuccess)
{
Debug.Log($"Generated text: {result.Data}");
}
else
{
Debug.LogError($"Error: {result.ErrorMessage}");
}
// Creating a successful result
var successResult = XrAiResult.Success("Generated text content");
// Creating a failed result
var failureResult = XrAiResult.Failure<string>("API request failed");
public async Task ProcessImage(Texture2D image)
{
var result = await imageToTextModel.Execute(imageBytes, "image/jpeg");
if (!result.IsSuccess)
{
HandleError(result.ErrorMessage);
return;
}
ProcessTextResult(result.Data);
}
IsSuccess
before accessing Data
ErrorMessage
is only meaningful when IsSuccess
is falseT
matches the expected return type of the AI operation