This site uses strictly necessary cookies. More Information
X- Home /
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: http://blogs.unity3d.com/2012/10/25/unity-serialization 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;
}
}
Answer by Loius · Apr 03, 2013 at 07:00 AM
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();
Thanks Louis, I ended up getting serialization to work finally, the $$anonymous$$akeTexture() method does not create a serializable Texture2D, so ins$$anonymous$$d I created prebuilt texture2d's under a Resources folder and used LoadResource, but your approach would work great for dynamically created content which is what i was after - ty!
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
How to make a serialized editor only field 2 Answers
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
How do you save changes made with custom editor? 3 Answers
Seperating editor and runtime data with ScriptableObjects 1 Answer
Using DuplicateCommand and DeleteCommand with Properties 0 Answers