using System; using UnityEngine; namespace EditorAttributes.Editor { public static class VectorUtils { /// /// Adds a value to a vector /// /// The vector to add the value to /// The value to add /// A vector with the value added to all axis public static Vector3 AddVector(Vector3 vector, float addend) => vector + new Vector3(addend, addend, addend); /// /// Subtracts a value from a vector /// /// The vector to subtract the value from /// The value to subtract /// A vector with the value subtracted from all axis public static Vector3 SubtractVector(Vector3 vector, float subtrahend) => vector - new Vector3(subtrahend, subtrahend, subtrahend); /// /// Converts a Vector3Int to a Vector2Int /// /// The Vector3Int to convert /// The converted Vector2Int public static Vector2Int Vector3IntToVector2Int(Vector3Int vector3Int) => new(vector3Int.x, vector3Int.y); /// /// Converts a Vector2Int to a Vector2 /// /// The Vector2Int to convert /// The converted Vector2 public static Vector2 Vector2IntToVector2(Vector2Int vector2int) => new(vector2int.x, vector2int.y); /// /// Converts a Vector2 to a Vector2Int /// /// The Vector2 to convert /// The converted Vector2Int public static Vector2Int Vector2ToVector2Int(Vector2 vector2) => new((int)vector2.x, (int)vector2.y); /// /// Converts a Vector3 to a Vector3Int /// /// The Vector3 to convert /// The converted Vector3Int public static Vector3Int Vector3ToVector3Int(Vector3 vector3) => new((int)vector3.x, (int)vector3.y, (int)vector3.z); /// /// Converts a Bounds to a BoundsInt /// /// The bounds to convert /// The converted BoundsInt public static BoundsInt BoundsToBoundsInt(Bounds bounds) => new(Vector3ToVector3Int(bounds.center), Vector3ToVector3Int(bounds.extents)); /// /// Parses a string to a Vector2 /// /// The string representing the vector /// The vector value from the string public static Vector2 ParseVector2(string vectorString) { var vector = new Vector2(); var vectorValues = vectorString.Split(','); try { vector.x = float.Parse(vectorValues[0].Replace("(", "")); vector.y = float.Parse(vectorValues[1].Replace(")", "")); } catch (Exception) { Debug.LogError($"Failed to parse Vector2 from string: \"{vectorString}\""); } return vector; } /// /// Parses a string to a Vector2Int /// /// The string representing the vector /// The vector value from the string public static Vector2Int ParseVector2Int(string vectorString) => Vector2ToVector2Int(ParseVector2(vectorString)); /// /// Parses a string to a Vector3 /// /// The string representing the vector /// The vector value from the string public static Vector3 ParseVector3(string vectorString) { var vector = new Vector3(); var vectorValues = vectorString.Split(','); try { vector.x = float.Parse(vectorValues[0].Replace("(", "")); vector.y = float.Parse(vectorValues[1]); vector.z = float.Parse(vectorValues[2].Replace(")", "")); } catch (Exception) { Debug.LogError($"Failed to parse Vector3 from string: \"{vectorString}\""); } return vector; } /// /// Parses a string to a Vector3Int /// /// The string representing the vector /// The vector value from the string public static Vector3Int ParseVector3Int(string vectorString) => Vector3ToVector3Int(ParseVector3(vectorString)); /// /// Parses a string to a Vector4 /// /// The string representing the vector /// The vector value from the string public static Vector4 ParseVector4(string vectorString) { var vector = new Vector4(); var vectorValues = vectorString.Split(','); try { vector.x = float.Parse(vectorValues[0].Replace("(", "")); vector.y = float.Parse(vectorValues[1]); vector.z = float.Parse(vectorValues[2]); vector.w = float.Parse(vectorValues[3].Replace(")", "")); } catch (Exception) { Debug.LogError($"Failed to parse Vector4 from string: \"{vectorString}\""); } return vector; } } }