Editor Serialization

I’m building an editor component and have run into a real snag - when creating Texture2D’s dynamically they do not appear to support serialization. I’ve tried following the directions at: Unity Serialization | Unity Blog and this works if and only if I replace the MakeTexture() call with EditorGUIUtility.whiteTexture - which is a Texture2D! So something is happening under the covers here and I’m not sure what.

My question - what can I do to ensure a created texture is serialized properly so that it stays around between runs?

using System;
using UnityEditor;
using UnityEngine;

[Serializable]
public class TestSer
{
	[SerializeField]
	public string m_Name;

	[SerializeField]
	private int m_Value;
	
	[SerializeField]
	public Texture2D m_Texture;

	public TestSer ()
	{
		m_Name = "chris";
		m_Value = 5;
		m_Texture = MakeTexture( Color.red );
	}
	
	private Texture2D MakeTexture( Color col )
    {
        Texture2D result = new Texture2D( 1, 1 );
        result.SetPixels( new Color[ ] { col } );
        result.Apply();
        return result;
	}		
}

I believe you need to create and save an asset to keep asset-type references around in edit mode or between play and edit mode.

This is what I used to build persistent meshes in edit mode; just replace Mesh with Texture2D and it should be a pretty good guide for keeping them around. You’ll want to set your m_Texture to the result of LoadAssetAtPath after saving it, I believe.

        var assetName : String = meshName + ".asset";
        var m : Mesh = AssetDatabase.LoadAssetAtPath("Assets/Meshes/" + assetName,Mesh) as Mesh;
		if ( m && overwrite ) {
			AssetDatabase.DeleteAsset("Assets/Meshes/"+assetName);
			m = null;
		}

        ...

            AssetDatabase.CreateAsset(m, "Assets/Meshes/" + assetName);
            AssetDatabase.SaveAssets();