Clamp#
Math / Arithmetic / Clamp Number, Min, Max
Can Read
Cannot Write
Description#
Clamps the specified value between Min and Max, both included
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Clamp")]
[Category("Math/Arithmetic/Clamp", "Number, Min, Max")]
[Description("Clamps the specified value between Min and Max, both included")]
[Image(typeof(IconContrast), ColorTheme.Type.Yellow)]
[Keywords("Max", "Within", "Between", "Min", "Limit")]
[Serializable]
public class TypeNumberMathClamp : TypeNumber
{
[SerializeField] private GetNumber m_Number = new GetNumber();
[SerializeField] private GetNumber m_Min = new GetNumber();
[SerializeField] private GetNumber m_Max = new GetNumber();
public override double GetPreview(GameObject source) =>
Math.Clamp(
this.m_Number.GetPreview(source),
this.m_Min.GetPreview(source),
this.m_Max.GetPreview(source)
);
public override double Get(Args args)
{
double number = this.m_Number.Get(args);
double min = this.m_Min.Get(args);
double max = this.m_Max.Get(args);
return Math.Clamp(number, min, max);
}
public override string ToString()
{
return $"Clamp({this.m_Number}, {this.m_Min}, {this.m_Max})";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}