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 a valid vector, 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
: The string representation of the vector to parse.
info
: An IFormatProvider
that provides culture-specific formatting information.
result
: An output parameter that will contain the parsed Vector2Int
if the method returns true
.
Example
string vectorString = "3,4";
IFormatProvider formatProvider = CultureInfo.InvariantCulture;
Vector2Int result;
bool success = Vector2Int.TryParse(vectorString, formatProvider, out result);
if (success)
{
// Use the result Vector2Int
// result.x == 3, result.y == 4
}
else
{
// Handle the parsing failure
}