Skip to content

Commit af308a1

Browse files
committed
Adding RoundTo extension
1 parent 9c24fee commit af308a1

8 files changed

+75
-0
lines changed

Scripts/Extensions/FloatExtensions.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using UnityEngine;
2+
3+
namespace ElasticSea.Framework.Extensions
4+
{
5+
public static class FloatExtensions
6+
{
7+
public static float RoundTo(this float value, float roundTo)
8+
{
9+
return (float)(Mathf.RoundToInt((value) / roundTo) * (double)roundTo);
10+
}
11+
}
12+
}

Scripts/Extensions/FloatExtensions.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Extensions/VectorExtensions.cs

+14
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ public static float Snap(this float value, float roundTo, float precision = floa
176176
var rounded = (float)(Mathf.RoundToInt((value + offset) / roundTo) * (decimal)roundTo) - offset;
177177
return Mathf.Abs(rounded - value) < precision ? rounded : value;
178178
}
179+
180+
public static Vector3 RoundTo(this Vector3 vector, float x, float y, float z)
181+
{
182+
return vector.RoundTo(new Vector3(x, y, z));
183+
}
184+
185+
public static Vector3 RoundTo(this Vector3 vector, Vector3 roundTo)
186+
{
187+
var x = vector.x.RoundTo(roundTo.x);
188+
var y = vector.y.RoundTo(roundTo.y);
189+
var z = vector.z.RoundTo(roundTo.z);
190+
191+
return new Vector3(x, y, z);
192+
}
179193

180194
public static Vector3 GetClosestPointOnLine(this Vector3 point, Vector3 a, Vector3 b)
181195
{

Tests.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Editor.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/Editor/Extensions.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using NUnit.Framework;
2+
using ElasticSea.Framework.Extensions;
3+
using Assert = UnityEngine.Assertions.Assert;
4+
5+
namespace ElasticSea.Framework.Tests.Extensions
6+
{
7+
public class FloatExtensionsTests
8+
{
9+
[TestCase(-0.06f, 0.02f, -0.06f)]
10+
[TestCase(-0.058f, 0.02f, -0.06f)]
11+
[TestCase(-0.062f, 0.02f, -0.06f)]
12+
public void TestApprox2(float input, float roundTo, float expected)
13+
{
14+
TestContext.AddFormatter<float>(val => ((float)val).ToString("F6"));
15+
16+
var output = input.RoundTo(roundTo);
17+
18+
Assert.AreEqual(expected, output);
19+
}
20+
}
21+
}

Tests/Editor/Extensions/FloatExtensionsTests.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)