NumberUtil.cs 920 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace DunGen
  7. {
  8. public static class NumberUtil
  9. {
  10. public static float ClampToNearest(float value, params float[] possibleValues)
  11. {
  12. float[] diffs = new float[possibleValues.Length];
  13. for (int i = 0; i < possibleValues.Length; i++)
  14. diffs[i] = Mathf.Abs(value - possibleValues[i]);
  15. float smallestDiff = float.MaxValue;
  16. int smalledDiffIndex = 0;
  17. for (int i = 0; i < diffs.Length; i++)
  18. {
  19. float diff = diffs[i];
  20. if (diff < smallestDiff)
  21. {
  22. smallestDiff = diff;
  23. smalledDiffIndex = i;
  24. }
  25. }
  26. return possibleValues[smalledDiffIndex];
  27. }
  28. }
  29. }