Description
The Vector2Int.TryParse
method attempts to convert a string representation of a 2D integer vector into a Vector2Int
object. This method is useful for safely parsing strings that may or may not represent valid vectors, without throwing exceptions if the parsing fails.
Usage
To use the TryParse
method, provide the string to be parsed, an IFormatProvider
for culture-specific formatting, and an output parameter to store the result if parsing is successful. The method returns a boolean indicating whether the parsing was successful.
Parameters:
str
(System.String): The string representation of the vector to parse.
info
(System.IFormatProvider): An object that provides culture-specific formatting information.
result
(out Vector2Int): When this method returns, contains the parsed Vector2Int
if the parsing was successful, or Vector2Int.Zero
if it failed.
Returns: System.Boolean
- true
if the string was successfully parsed; otherwise, false
.
Example
// Example usage of Vector2Int.TryParse
string vectorString = "10,20";
IFormatProvider formatProvider = CultureInfo.InvariantCulture;
Vector2Int result;
bool success = Vector2Int.TryParse(vectorString, formatProvider, out result);
if (success)
{
// Parsing was successful, use the result
// result now contains the Vector2Int(10, 20)
}
else
{
// Parsing failed, handle the error
}