Skip to content

Instantly share code, notes, and snippets.

@romainPechot
Created July 22, 2015 13:45
Show Gist options
  • Save romainPechot/c64b54a1bf22cc432b0e to your computer and use it in GitHub Desktop.
Save romainPechot/c64b54a1bf22cc432b0e to your computer and use it in GitHub Desktop.
int extensions. Mostly usefull for index array manipulation.
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