Skip to content

Instantly share code, notes, and snippets.

@azeitler
Created November 23, 2011 17:57
Show Gist options
  • Save azeitler/1389369 to your computer and use it in GitHub Desktop.
Save azeitler/1389369 to your computer and use it in GitHub Desktop.
Colored Material Cache
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// manages instances of the same material with random colors
/// if a Material+Color combination does not yet exist it will be created
/// if the combination has been created before it will be returned from the cached
/// </summary>
public static class ColoredMaterialCache {
/// <summary>
/// cached materials with random colors
/// </summary>
public static Dictionary<Material, Dictionary<int, Material>> ColorVariations {
get {
if (_ColorVariations == null) {
Initialize ();
}
return _ColorVariations;
}
}
private static Dictionary<Material, Dictionary<int, Material>> _ColorVariations = null;
/// <summary>
/// array of random colors, initialize with base colors of Unity
/// </summary>
public static Color[] Colors = new Color[] {
Color.black,
Color.blue,
Color.red,
Color.yellow,
Color.green,
Color.white,
Color.magenta,
Color.cyan,
Color.gray
};
/// <summary>
/// number of colors
/// </summary>
public static int NumberOfColors {
get {
if (_NumberOfColors < 0) {
Initialize ();
}
return _NumberOfColors;
}
}
public static int _NumberOfColors = -1;
/// <summary>
/// last generated color
/// </summary>
private static int LastColor;
/// <summary>
/// set colors used for randomization
/// </summary>
/// <param name="colors">
/// A list of <see cref="Color"/> that will be used for randomization
/// </param>
public static void SetColors (params Color[] colors) {
int newNumber = colors.Length;
if (newNumber != NumberOfColors) {
Colors = new Color[NumberOfColors];
}
colors.CopyTo (Colors, 0);
_NumberOfColors = newNumber;
}
private static void Initialize () {
_ColorVariations = new Dictionary<Material, Dictionary<int, Material>> ();
_NumberOfColors = Colors.Length;
LastColor = -1;
}
/// <summary>
/// Get a variation of Material with a random color picked from the set Colors
/// </summary>
/// <param name="mat">
/// the prototype <see cref="Material"/> based on which the randomization will be generated
/// </param>
/// <returns>
/// A <see cref="Material"/> instance with a random color set to Material.color
/// </returns>
public static Material GetVariationOnMaterial (Material mat) {
Random.seed = Random.Range (1, 23474);
int colorID = Random.Range (0, NumberOfColors);
if (colorID == LastColor)
colorID = (colorID + 1) % NumberOfColors;
LastColor = colorID;
if (!ColorVariations.ContainsKey (mat)) {
ColorVariations.Add (mat, new Dictionary<int, Material> (NumberOfColors));
}
if (ColorVariations[mat].ContainsKey (colorID)) {
return ColorVariations[mat][colorID];
}
else {
Material newMat = new Material (mat);
newMat.color = Colors[colorID];
ColorVariations[mat].Add (colorID, newMat);
return newMat;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment