Get mesh vertex colors

Posted in | Code, Unity, VThree

I use Probuilder & Polybrush with vertex colors for all the art in VThree. Keeping track of all the colors can be a chore, and if I lose one figuring out which it is visually is impossible. So I whipped this up quickly this evening. It’ll print all the colors on your mesh (if you’re using vertex colors) to the console.

Right click on an object in the hierarchy and choose “Get Vertex Colors”

Hopefully useful for someone.

using System;
using System.Linq;
using UnityEngine;
using UnityEditor;

public class NWZGetMeshColors : MonoBehaviour {
    [MenuItem("GameObject/Get Vertex Colors", false, 0)]
	private static void GetVertexColors(){
		Debug.Log("Getting mesh colors for " + Selection.activeTransform.gameObject.name);
		Mesh mesh = Selection.activeTransform.gameObject.GetComponent<MeshFilter>().sharedMesh;
        Vector3[] vertices = mesh.vertices;
		Color[] colors = new Color[vertices.Length];
		int i = 0;

		foreach (Color c in mesh.colors){
			if(!colors.Contains(c)){
				colors[i] = c;
				i++;
			}
        }

		Color[] uniqueColors = new Color[i];
		Array.Copy(colors, uniqueColors, uniqueColors.Length);

		foreach (Color c in uniqueColors){
			Debug.Log("<color=" + ToRGBHex(c) + ">▉</color> " + ToRGBHex(c));
        }
	}

	public static string ToRGBHex(Color c){
		return string.Format("#{0:X2}{1:X2}{2:X2}", ToByte(c.r), ToByte(c.g), ToByte(c.b));
	}
 
	private static byte ToByte(float f){
		f = Mathf.Clamp01(f);
		return (byte)(f * 255);
	}
}

C9McEc-U0AAKSZt

C9McIvWVYAAY3f1