Skip to content

Instantly share code, notes, and snippets.

@JauntyBear
Created November 21, 2021 23:02
Show Gist options
  • Save JauntyBear/c3402886c5e895317dbf1698ce2ae0e5 to your computer and use it in GitHub Desktop.
Save JauntyBear/c3402886c5e895317dbf1698ce2ae0e5 to your computer and use it in GitHub Desktop.
Unity RangeInt.cs extension
using System;
using UnityEngine;
namespace JauntyBear
{
/// <summary>
/// Provides input for a value range in the editor and various convencience functions to work with that range.
/// </summary>
[Serializable]
public class RangeInt
{
/// <summary>
/// The range start.
/// </summary>
[SerializeField] int from;
/// <summary>
/// The range end.
/// </summary>
[SerializeField] int to;
/// <summary>
/// The range start.
/// </summary>
public int From { get { return from; } }
/// <summary>
/// The range end.
/// </summary>
public int To { get { return to; } }
/// <summary>
/// Returns how wide the range between from and to is.
/// </summary>
public int Range { get { return to - from; } }
/// <summary>
/// Returns a random number between from [inclusive] and to [inclusive].
/// </summary>
public int RandomInclusive { get { return UnityEngine.Random.Range(from, to + 1); } }
/// <summary>
/// Returns a random number between from [inclusive] and to [exclusive].
/// </summary>
public int RandomExclusive { get { return UnityEngine.Random.Range(from, to); } }
/// <summary>
/// Create a RangeFloat with 0-0 as the range. Needed for the editor.
/// </summary>
public RangeInt()
{
}
/// <summary>
/// Create a RangeFloat with a certain range.
/// </summary>
/// <param name="from">Lower range bound.</param>
/// <param name="to">Upper range bound.</param>
public RangeInt(int from, int to)
{
this.from = from;
this.to = to;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment