• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lypheus · Apr 03, 2013 at 06:27 AM · editorserialization

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;
     }        
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lypheus · Apr 04, 2013 at 01:53 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scriptable Object resets after Unity is closed 2 Answers

Is EditorUtility.SetDirty restricted to prefabs or inspected GameObject? 5 Answers

Some Text crash after few force closing of Unity Editor 2 Answers

Why does my Unity Serialization not work? Do i miss something? 1 Answer

object list not serializing 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges