- Home /
Recursive extension methods
So what I want to do is to have my extension have "sub -extension" as transform.position has .x , .y and .z I want my code to have similiar things but i can't think of anything that would work and nested classes don't work.
Answer by UnityCoach · Jul 26, 2017 at 06:32 PM
Hi, what you mention are members of a struct. Something similar to
public struct Vector3
{
public float x;
public float y;
public float z;
}
The Transform
component has a position
member of type Vector3
.
Now "extension" methods extend a type. Like this :
static public class Extensions
{
static public Vector3 MyMethod (this Vector3 v3)
{
return v3;
}
}
This allows you to call the new method on a variable of that type. Like
Vector3 _position;
_position = _position.MyMethod();
The return type must be the same as the type passed with this
keyword.
Deleted my post, UnityCoach was quicker to respond and it was basically the same.
Althou not exactly what I intended to ask, it basically makes sense to me what you are trying to convey, I understand and my code worked out, thanks ! :D
P.S. what i actually inteded was like : $$anonymous$$y$$anonymous$$ethod.Sub$$anonymous$$ethod(); where my $$anonymous$$ethod was just a parent where under went the Sub$$anonymous$$ethod(s), althou your answear made me figure out a slight workaround of just using Vector3 because it was practically what i was using
Answer by grizzly48 · Jul 26, 2017 at 06:34 PM
Did you add "[System.Serializable]" to your nested class? Check here, if you wanna know what I mean: https://docs.unity3d.com/ScriptReference/Serializable.html