Description
The Vector2.TryParse
method attempts to convert a string representation of a 2D vector into a Vector2
object. This method is useful when you need to safely parse a string input that is expected to represent a vector, without throwing exceptions if the input is invalid.
Usage
To use the Vector2.TryParse
method, provide the string to be parsed and an uninitialized Vector2
variable to store the result. The method returns a boolean indicating whether the parsing was successful.
The method signature is as follows:
public static bool TryParse(string str, out Vector2 result)
Parameters:
str
: The string representation of the vector to parse.
result
: When this method returns, contains the Vector2
value equivalent to the vector contained in str
, if the conversion succeeded, or Vector2.Zero
if the conversion failed. This parameter is passed uninitialized.
Returns:
true
if str
was converted successfully; otherwise, false
.
Example
// Example usage of Vector2.TryParse
string input = "1.0, 2.0";
Vector2 vector;
if (Vector2.TryParse(input, out vector))
{
// Parsing succeeded, use the vector
// vector.x is 1.0, vector.y is 2.0
}
else
{
// Parsing failed, handle the error
}