Changes to Mesh Vertex Colors Reset on Build

Hey folks,

I created a more complex version of [Alex’s Vertex Color Method][1] for my artist a few days ago, allowing her to change multiple vertex colors in her meshes : the idea being that she could (e.g.) create a model of a house with a different vertex colour for the roof, door and walls, then give the house a vertex colour material and change the colours of these elements individually.

The vertex colour editing scrip looks like this :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
public class UpdateVertexColors : MonoBehaviour
{
	private int[] colorPosition;

	private List<Color> colors = new List<Color>();
	
	private MeshFilter meshFilter;

	private Mesh mesh;
	
	private MeshFilter MeshFilter
	{
		get
		{
			return this.GetComponent<MeshFilter>();
		}
	}
	
	private Mesh Mesh
	{
		get
		{
			return this.MeshFilter.mesh;
		}
		set
		{
			this.MeshFilter.mesh = value;
		}
	}
	
	public List<Color> Colors
	{
		get
		{
			if(this.colors.Count == 0)
			{
				this.GetColors();
			}
			
			return this.colors;
		}
		set
		{
			this.colors = value;
		}
	}

	public void GetColors()
	{
		int i = 0;
		
		colorPosition = new int[this.Mesh.vertices.Length];
		
		while(i < this.Mesh.vertices.Length)
		{
			if(!colors.Contains(this.Mesh.colors*))*
  •  	{*
    

_ this.colors.Add(this.Mesh.colors*);_
_
}*_

colorPosition = this.colors.IndexOf(this.Mesh.colors*);*
* i++;*
* }*
* }*

* public void SetColors()*
* {*
* int i = 0;*

* Color[] newColors = new Color[this.Mesh.vertices.Length];*

* while(i < this.Mesh.vertices.Length)*
* {*
newColors = this.colors[this.colorPosition*];*
* i++;*
* }*

* this.Mesh.colors = newColors;*
* }*
}

… and in order to simplify the artist’s life, it’s manipulated by a custom inspector that looks like this :
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(UpdateVertexColors))]
public class UpdateVertexColorInspector : Editor
{
* UpdateVertexColors script;*

* public override void OnInspectorGUI()*
* {*
* base.OnInspectorGUI();*

* int i = 0;*

* while(i < this.Script.Colors.Count)*
* {*
this.Script.Colors = EditorGUILayout.ColorField(“§”, this.Script.Colors*);*
* i++;*
* }*

* if(GUI.changed)*
* {*
* this.Script.SetColors();*
* }*
* }*

* private UpdateVertexColors Script*
* {*
* get*
* {*
* if(script == null)*
* {*
* script = (UpdateVertexColors)target;*
* }*

* return script;*
* }*
* }*
}
The script works absolutely fine, and the artist can adjust colours to her little heart’s content BUT almost all the changes disappeared the moment we make a Build (originally to test the Shaders on Android devices, but I have since tested the problem with a Standalone build and the same thing happens).
I say “almost all the changes” because a number of trees, which she appears to have copied and pasted after changing their colour, appear to retain their new colour.
The colours are also wiped out in the Editor. I’ve tried various combinations of serializing the Mesh / Meshfilter etc. and it doesn’t seem to have any kind of effect.
Thoughts? I’m out of ideas at this point.
R.
_*[1]: CG Cookie | Learn Blender, Online Tutorials and Help - CG Cookie | Learn Blender, Online Tutorials and Feedback

On changes, mark the Mesh with EditorUtility.SetDirty() so that Unity knows to save it.