With Component#
Math / With Component
Can Read
Cannot Write
Description#
The same vector with one of its three components replaced by another value
Parameters#
| Name | Description |
|---|---|
| Vector | The vector that is copied |
| Axis | Which of the three components is replaced |
| Value | The value the selected component takes |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("With Component")]
[Category("Math/With Component")]
[Description("The same vector with one of its three components replaced by another value")]
[Image(typeof(IconVector3), ColorTheme.Type.Yellow, typeof(BadgeSync))]
[Parameter("Vector", "The vector that is copied")]
[Parameter("Axis", "Which of the three components is replaced")]
[Parameter("Value", "The value the selected component takes")]
[Keywords("Vector", "Replace", "Override", "Set", "Change", "Axis", "X", "Y", "Z")]
[Serializable]
public class TypeVector3MathWithComponent : TypeVector3
{
private enum Axis
{
X,
Y,
Z
}
[SerializeField] private GetVector3 m_Vector = new GetVector3();
[SerializeField] private Axis m_Axis = Axis.Y;
[SerializeField] private GetNumber m_Value = new GetNumber();
public override Vector3 GetPreview(GameObject source) => With(
this.m_Vector.GetPreview(source),
this.m_Axis,
this.m_Value.GetPreview(source)
);
public override Vector3 Get(Args args) => With(
this.m_Vector.Get(args),
this.m_Axis,
this.m_Value.Get(args)
);
private static Vector3 With(Vector3 vector, Axis axis, double value)
{
return axis switch
{
Axis.X => new Vector3((float) value, vector.y, vector.z),
Axis.Y => new Vector3(vector.x, (float) value, vector.z),
Axis.Z => new Vector3(vector.x, vector.y, (float) value),
_ => vector
};
}
public override string ToString()
{
return $"{this.m_Vector} with {this.m_Axis} = {this.m_Value}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}