With Component#
Math / With Component
Can Read
Cannot Write
Description#
The same vector with one of its two components replaced by another value
Parameters#
| Name | Description |
|---|---|
| Vector | The vector that is copied |
| Axis | Which of the two 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 two components replaced by another value")]
[Image(typeof(IconVector2), ColorTheme.Type.Yellow, typeof(BadgeSync))]
[Parameter("Vector", "The vector that is copied")]
[Parameter("Axis", "Which of the two components is replaced")]
[Parameter("Value", "The value the selected component takes")]
[Keywords("Vector", "Replace", "Override", "Set", "Change", "Axis", "X", "Y")]
[Serializable]
public class TypeVector2MathWithComponent : TypeVector2
{
private enum Axis
{
X,
Y
}
[SerializeField] private GetVector2 m_Vector = new GetVector2();
[SerializeField] private Axis m_Axis = Axis.X;
[SerializeField] private GetNumber m_Value = new GetNumber();
public override Vector2 GetPreview(GameObject source) => With(
this.m_Vector.GetPreview(source),
this.m_Axis,
this.m_Value.GetPreview(source)
);
public override Vector2 Get(Args args) => With(
this.m_Vector.Get(args),
this.m_Axis,
this.m_Value.Get(args)
);
private static Vector2 With(Vector2 vector, Axis axis, double value)
{
return axis switch
{
Axis.X => new Vector2((float) value, vector.y),
Axis.Y => new Vector2(vector.x, (float) value),
_ => vector
};
}
public override string ToString()
{
return $"{this.m_Vector} with {this.m_Axis} = {this.m_Value}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}