うにてぃブログ

主にUnityとC#に関する記事を書いていきます

2021-03-01から1ヶ月間の記事一覧

【C#】負の値にも対応したIndexの計算式

C#

public static class Utility { /// <summary> /// 0~maxの範囲のIndex を返す /// 負の値にも対応 /// </summary> public static int LoopIndex(int index, int max) { var ret = index % max; if (index < 0) ret += max; return ret; } } 実行例 Utility.LoopIndex(10, 20); …