• 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
Question by IdDuka · Oct 30, 2014 at 11:10 PM · editorserializationcomponent

Serializing auto-generated values into Unity scene

I've been plagued by the same issue for a couple of days now and can't seem to be able to come up with or find a satisfactory solution to it.

I have created a simple system which lets me define functionality for a component which is executed exclusively in editor mode (and which is not executed when the player is running in the editor either). This makes it much easier and less time consuming for me to manage my scenes, as much of the work becomes automated once I've defined the necessary functions. The problem I've run into, however, seems to be related.

In Unity, public member variables for a component are serialized into the scene automatically. Pretty nifty feature. Unfortunately, it appears that Unity does not serialize values which are set through my automated editor-mode-only system. As far as I can see, Unity only serializes a variable value if it is input directly by the user into the inspector.

Here's the particular class which is giving me grief at the moment:

 /// <summary>
 /// Component to be attached to game objects which have a local Mesh Filter component which is reset due to the Unity
 /// bug whereby game objects created from prefabs do not persist procedurally generated meshes.
 /// </summary>
 public class THMeshPersistor : MonoBehaviour
 {
     public Vector3[] vertices = null; /// Recorded mesh vertices.
     public Vector2[] uvs = null; /// Recorded mesh UV co-ordinates.
     public int[] triangles = null;  /// Recorded mesh index buffer.
 
     // Local components
     private MeshFilter meshFilter = null;  /// Local Mesh Filter component.
 
     private Mesh latestRecordedMesh = null; /// Latest recorded mesh.
 
     /// <summary>
     /// Called upon first activity.
     /// </summary>
     public void Start()
     {
         // Do nothing.
     }
 
     /// <summary>
     /// Performs necessary initialisation.
     /// </summary>
     public void Init()
     {
         // Obtain references to local components.
         if(meshFilter == null)
             meshFilter = GetComponent<MeshFilter>();
     }
 
     /// <summary>
     /// Records the mesh from the Mesh Filter.
     /// </summary>
     private void RecordMesh()
     {
         // Update last recorded mesh.
         latestRecordedMesh = meshFilter.sharedMesh;
 
         // Record vertices.
         vertices = new Vector3[latestRecordedMesh.vertexCount];
         System.Array.Copy(latestRecordedMesh.vertices, vertices, latestRecordedMesh.vertexCount);
         // Record UVs.
         uvs = new Vector2[latestRecordedMesh.vertexCount];
         System.Array.Copy(latestRecordedMesh.uv, uvs, latestRecordedMesh.vertexCount);
         // Record indices.
         triangles = new int[latestRecordedMesh.triangles.Length];
         System.Array.Copy(latestRecordedMesh.triangles, triangles, latestRecordedMesh.triangles.Length);
     }
 
     /// <summary>
     /// Restores the Mesh Filter's mesh from the latest record.
     /// </summary>
     private void ReassembleMesh()
     {
         if(vertices == null || uvs == null || triangles == null)
             return;
 
         Mesh mesh = new Mesh();
         // Reassemble vertices.
         mesh.vertices = new Vector3[vertices.Length];
         System.Array.Copy(vertices, mesh.vertices, vertices.Length);
         // Reassemble UVs.
         mesh.uv = new Vector2[uvs.Length];
         System.Array.Copy(uvs, mesh.uv, uvs.Length);
         // Reassemble indices.
         mesh.triangles = new int[triangles.Length];
         System.Array.Copy(triangles, mesh.triangles, triangles.Length);
 
         meshFilter.sharedMesh = mesh;
 
 
     }
 
     /// <summary>
     /// Update function reserved for use in editor mode.
     /// </summary>
     public void EditorUpdate()
     {
         Init();
 
         // Record mesh if it has changed.
         if(meshFilter.sharedMesh != null && meshFilter.sharedMesh != latestRecordedMesh)
             RecordMesh();
         else if(meshFilter.sharedMesh == null) // Reassemble mesh if it is absent.
             ReassembleMesh();
     }
 }

As you've probably figured out, the code in the EditorUpdate() function is what gets executed to do the automated work for me.

Is there a way of forcing Unity to serialize my member variables programatically? I can forfeit Unity serialization and serialize it myself to some other file on disk, but I would prefer to avoid this situation.

Comment
rakkarage

People who like this

1 Show 3
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 AlwaysSunny · Oct 30, 2014 at 10:54 PM 0
Share

Have a look at http://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html

Didn't proof the code in detail, but I didn't see you using it. :)

avatar image IdDuka · Oct 31, 2014 at 08:29 AM 0
Share

This is probably what I'm looking for. I'll try it out later today and get back with the result. Thanks!

avatar image IdDuka · Nov 02, 2014 at 10:01 AM 0
Share

Worked like a charm! Thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by frarees · Nov 03, 2014 at 01:16 AM

I think it would be best to move all your editor related code to a proper editor script instead of making it reside at runtime (note that despite the code there won't be executed in the player, it will be there anyway). If that's a limitation for you, every time you change a serializable field in your script, call EditorUtility.SetDirty (this).

When you work directly with editor scripts you get access to SerializedObject and SerializedProperty among other great APIs to do exactly what you want. Using serialized properties help you track the state of your assets (e.g. bold on prefabs, dirty state, undo support). And also, you make sure all this code is compiled on an editor pass and it's not going to be compiled on the runtime assembly (i.e. smaller assembly size). Here's an example.

Comment

People who like this

0 Show 0 · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

pass child class to ScriptableObject.CreateInstance<> ? 1 Answer

List which is Serializable does not get values from prefab when instantiated 1 Answer

Access another objects property in an editor script 1 Answer

Create a Component Script for Editor Only 1 Answer

Is there an editor function you can call on the moment a new object is created? 0 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