if (value is Single)
{
var retVal = ((Single)value).Map(InputRange.x, InputRange.y, InputRange.x, OutputRange.y);
return _floorToInt ? System.Convert.ChangeType(Mathf.FloorToInt(retVal), targetType) : retVal;
}
else if (value is Single?)
{
var retVal = (value as Single?).Map(InputRange.x, InputRange.y, InputRange.x, OutputRange.y);
return retVal == null ? null : _floorToInt ? System.Convert.ChangeType(Mathf.FloorToInt(retVal.Value), targetType) : retVal;
}
else if (value is Double)
{
var retVal = ((Double)value).Map(InputRange.x, InputRange.y, InputRange.x, OutputRange.y);
return _floorToInt ? System.Convert.ChangeType(Mathf.Floor((float)retVal), targetType) : retVal;
}
else if (value is Double?)
{
var retVal = (value as Double?).Map(InputRange.x, InputRange.y, InputRange.x, OutputRange.y);
return retVal == null ? null : _floorToInt ? System.Convert.ChangeType(Mathf.FloorToInt((float)retVal.Value), targetType) : retVal;
}
else
throw new NotImplementedException();
And that is strange
Firstly nullable must be first
static void Main(string[] args)
{
float? value = 1;
if (value is Single)
{
Console.WriteLine("float");
}
else if (value is Single?)
{
Console.WriteLine("nullable float");
}
//Console: float
if (value is Single?)
{
Console.WriteLine("nullable float");
}
else if (value is Single)
{
Console.WriteLine("float");
}
//Console: nullable float
}
Secondly why not used pattern matching?
I mean, I don't see some big difference between nullable and not nullable values, (because now it works)
so i think it might be rewritten
Something like this
public override object Convert(object value, Type targetType, object parameter)
{
switch (value)
{
case float number:
number = number.Map(InputRange.x, InputRange.y, InputRange.x, OutputRange.y);
return _floorToInt ? number.ToInt(targetType) : number;
case double number:
number = number.Map(InputRange.x, InputRange.y, InputRange.x, OutputRange.y);
return _floorToInt ? number.ToInt(targetType) : number;
case null:
return null;
default:
Debug.LogWarning(
$"The property {value} is of an unsupported types. " +
$"Please use the float double or their nullable types");
return null;
}
}
And maybe rewrite Map()
from this
Map(this float value, float from1, float to1, float from2, float to2)
to this
Map(this float value, Vector2 from, Vector2 to)
for clean up the code
now
And that is strange Firstly nullable must be first
Secondly why not used pattern matching? I mean, I don't see some big difference between nullable and not nullable values, (because now it works) so i think it might be rewritten Something like this
And maybe rewrite Map() from this
Map(this float value, float from1, float to1, float from2, float to2)
to thisMap(this float value, Vector2 from, Vector2 to)
for clean up the code