Created
July 22, 2015 13:45
-
-
Save romainPechot/c64b54a1bf22cc432b0e to your computer and use it in GitHub Desktop.
int extensions. Mostly usefull for index array manipulation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public static class intExtensions | |
{ | |
public static bool CheckIndex(this int index, ICollection col) | |
{ | |
return ((index >= 0) && (index < col.Count)); | |
}// CheckIndex() | |
public static int Next(this int index, ICollection col) { return Step(index, 1, col); } | |
public static int Previous(this int index, ICollection col) { return Step(index, -1, col); } | |
public static int Step(this int index, int step, ICollection col) | |
{ | |
index += step; | |
if(index < 0) index = col.Count - 1; | |
if(index > col.Count) index = 0; | |
return index; | |
}// Step() | |
public static int ClampIndex(this int index, ICollection col) | |
{ | |
return Mathf.Clamp(index, 0, col.Count - 1); | |
}// ClampIndex() | |
}// intExtensions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment