Hey all,
Is there a way I can do these? I’m trying to add a function to the Vector3 class in the UnityEngine. Right now it doesn’t have a parse like bool.parse and int.parse but I want to add my own.
My code below I want to add to it so I can call Vector3.Parse(my string);
Any ideas on how to do it, or should I be doing something else?
public static Vector3 Parse(string value)
{
string[] temp = value.Substring(1, value.Length - 2).Split(',');
float x = float.Parse(temp[0]);
float y = float.Parse(temp[1]);
float z = float.Parse(temp[2]);
Vector3 returnVector3 = new Vector3(x, y, z);
return new Vector3();
}
I was thinking something on the borderlines of this.
using UnityEngine;
using System.Collections;
public class Convert : UnityEngine.Vector3 {
public static Vector3 Parse(string value)
{
string[] temp = value.Substring(1, value.Length - 2).Split(',');
float x = float.Parse(temp[0]);
float y = float.Parse(temp[1]);
float z = float.Parse(temp[2]);
Vector3 returnVector3 = new Vector3(x, y, z);
return new Vector3();
}
}
You can’t extend the Unity types. The only possibility would be extension methods, but they are most likely not the best solution for that situation. Just create a static class that doesn’t inherit anything (e.g. Parser), write a static method for the parsing (e.g. ParseVector3).
Tip: If you compute the result and have it in returnVector3, you should return that one and not a newly created one.
To extend a class is necessary rewrite all methods and properties, for example I created a custom Vector3 class using Vector3 UnityEngine.
namespace CustomClasses{
public class Vector3{
UnityEngine.Vector3 vector;
public Vector3(){ this.vector = UnityEngine.Vector3.zero; }
public Vector3(UnityEngine.Vector3 vector3){ this.vector = vector3; }
public static UnityEngine.Vector3 back { get{ return UnityEngine.Vector3.back; }}
public static UnityEngine.Vector3 forward { get{ return UnityEngine.Vector3.forward; }}
//etc...
public float magnitude { get{ return this.vector.magnitude; } }
public UnityEngine.Vector3 normalized { get{ return this.vector.normalized; } }
//etc...
//Custom members
public string HelloWorld{ get{ return "HelloWorld"; } }
public static implicit operator Vector3(UnityEngine.Vector3 vector3){ return new Vector3(vector3); }
public static implicit operator UnityEngine.Vector3(Vector3 vector3){ return vector3.vector; }
}
}
After, indicate the compiler to use our custom class instead of the class with the same name in Unity. Example code:
using UnityEngine;
using System.Collections;
using Vector3 = CustomClasses.Vector3; //THIS LINE
public class temp : MonoBehaviour {
void Start(){
Vector3 myVector = new Vector3();
Debug.Log(myVector.HelloWorld);
myVector = new UnityEngine.Vector3(4f,2f,1f);
Debug.Log("Vector3: "+(UnityEngine.Vector3)myVector);
Debug.Log("Magnitude: "+myVector.magnitude);
Debug.Log("Normalized: "+myVector.normalized);
}
}
As a consequence people would have different UnityEngine.dll files, which leads to incompatibilities if you e.g. get something from the Unity Asset Store. With all due respect, that’s a stupid idea which will earlier or later cause issues that are completely unnecessary.
Create your own namespace where you have all the extensions. Then you might have to write:
Vector3Extension.Parse (...)
That’s the way it works. There is no practical solution to get what you want and this is the closest thing, so take it and go on. The only thing you are achieving at the moment is that you are wasting your time because of something that is almost irrelevant. Sure, it is your time, but think of all the things you can achieve when you just pick that solution and go on…
public static class Vector3Extension
{
public static Vector3 ToVector3(this string value)
{
string[] temp = value.Substring(1, value.Length - 2).Split(',');
float x = float.Parse(temp[0]);
float y = float.Parse(temp[1]);
float z = float.Parse(temp[2]);
return new Vector3(x, y, z);
}
}