![Dan.San :mrgreen:](./images/smilies/14.gif)
Quelques exemples d'utilisations :
Code : Tout sélectionner
transform.position = new Vector3 (transform.position.x, 5, transform.position.z);
// Devient
transform.position = transform.position.WithY (5);
Vector2 to2D = new Vector2 (transform.position.x, transform.position.z);
// Devient
Vector2 to2D = transform.position.ToXZ ();
Code : Tout sélectionner
using UnityEngine;
public static class VectorExtensions {
#region Vector3
// Replace
public static Vector3 WithX(this Vector3 from, float x) {
return new Vector3(x, from.y, from.z);
}
public static Vector3 WithY(this Vector3 from, float y) {
return new Vector3(from.x, y, from.z);
}
public static Vector3 WithZ(this Vector3 from, float z) {
return new Vector3(from.x, from.y, z);
}
public static Vector2 ToXY(this Vector3 from) {
return new Vector2(from.x, from.y);
}
public static Vector2 ToXZ (this Vector3 from) {
return new Vector2(from.x, from.z);
}
#endregion
#region Vector2
// Replace
public static Vector2 WithX(this Vector2 from, float x) {
return new Vector2(x, from.y);
}
public static Vector2 WithY(this Vector2 from, float y) {
return new Vector2(from.x, y);
}
public static Vector3 ToXZ(this Vector2 from, float y = 0) {
return new Vector3(from.x, y, from.y);
}
public static float LerpXY(this Vector2 from, float time) {
return Mathf.Lerp(from.x, from.y, time);
}
#endregion
}