UniWebViewChannelMessageResponse
Summary
Represents a response for a channel message.
This class is used to send structured responses back to JavaScript from Unity. It supports both success and error responses, with flexible data formats. Response objects are typically created using the static factory methods Success()
and Error()
.
Properties Summary
Response data for successful responses. | |
Whether this response represents an error. | |
Error data if hasError is true. |
Methods Summary
Static factory method for success responses. | |
Static factory method for error responses. |
Properties
Response data for successful responses.
This property contains the data that will be sent back to JavaScript when the response represents success. For error responses, this property is null.
Whether this response represents an error.
When true, the response will be delivered to JavaScript as a Promise rejection or error. When false, the response will be delivered as a successful result.
Error data if hasError is true.
This property contains the error information that will be sent back to JavaScript when the response represents an error. For success responses, this property is null. Supports any JSON-compatible type.
Methods
Static factory method for success responses.
Creates a response object that represents a successful operation. The provided data will be serialized to JSON and sent back to JavaScript.
- object data
The data to include in the response. Can be any JSON-serializable type.
Example
webView.OnChannelMessageReceived += (view, message) => {
if (message.action == "getUserInfo") {
var userInfo = new {
userId = 12345,
userName = "Player1",
level = 25,
score = 150000
};
// Create success response
return UniWebViewChannelMessageResponse.Success(userInfo);
}
if (message.action == "getSimpleValue") {
// Success with simple value
return UniWebViewChannelMessageResponse.Success("Hello from Unity!");
}
if (message.action == "getNumberValue") {
// Success with numeric value
return UniWebViewChannelMessageResponse.Success(42);
}
return null;
};
Static factory method for error responses.
Creates a response object that represents an error condition. The provided error data will be serialized to JSON and sent back to JavaScript as a rejection.
- object errorData
The error data (supports any JSON-compatible type). Can be a string, object, or any serializable data.
Example
webView.OnChannelMessageReceived += (view, message) => {
if (message.action == "processPayment") {
if (!message.TryGetData<PaymentData>(out var paymentData)) {
// Return error for invalid data
return UniWebViewChannelMessageResponse.Error(new {
code = "INVALID_DATA",
message = "Payment data is malformed"
});
}
if (paymentData.amount <= 0) {
// Return error for invalid amount
return UniWebViewChannelMessageResponse.Error(new {
code = "INVALID_AMOUNT",
message = "Payment amount must be greater than zero",
providedAmount = paymentData.amount
});
}
// Return error with simple string
if (!IsPaymentMethodValid(paymentData.method)) {
return UniWebViewChannelMessageResponse.Error("Invalid payment method");
}
// Process payment...
return UniWebViewChannelMessageResponse.Success("Payment processed");
}
return null;
};