Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
3
Question by zombience · Sep 14, 2013 at 06:02 PM · c#editorserializationscriptableobject

Cannot serialize System.Type field

I've run into an issue serializing a System.Type field.

in a scriptable object, I have the following code:

 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 
 [Serializable]
 public class TypeStorage : ScriptableObject
 {
     [SerializeField]
     public Type type;
     [SerializeField]
     public string typeName;
 
     protected void OnEnable() { hideFlags = HideFlags.HideAndDontSave; }
 
     public virtual void OnGUI()
     {
         if(type != null)
             GUILayout.Label(type.ToString(), EditorStyles.whiteLabel);
         else
             GUILayout.Label("no type set");
         
         GUILayout.Label(typeName);
     }
 }

the typeName field serializes properly, but the Type field gets lost on reload. Is there a way around this? I'm trying to avoid string comparisons for type verification.

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

3 Replies

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

Answer by Bunny83 · Sep 14, 2013 at 06:48 PM

Unity can only serialize a few types which are listed here (Serializable types) The only way you have is to use the string representation of the Type. Make sure you use the AssemblyQualifiedName.


Since the question got already bumped I like to add that i've written a System.Type wrapper some time ago. It should support any System.Type, even generic types. I've also made a serializable method info which depends on the SerializableType. It basically stores the whole type definition in a compact binary format which is then stored as byte array

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 zombience · Sep 14, 2013 at 07:03 PM 0
Share

ah, ok. I didn't realize there were limited serializable types. Thanks for the quick response!

avatar image
9

Answer by numberkruncher · Oct 10, 2014 at 07:01 PM

I have created a serializable wrapper for System.Type along with a couple of attributes and property drawer for editor interfaces:

https://bitbucket.org/rotorz/classtypereference-for-unity

Comment
Add comment · Show 10 · 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 aidesigner · Oct 12, 2014 at 09:49 PM 0
Share

Excellent work and really nice touch on the selection menu. However, I noticed that anything derived from UnityEngine.Object can be serialized. Therefore $$anonymous$$onoScript fields can be serialized normally. In turn $$anonymous$$onoScript instances give you access to the System.Type (ie. my$$anonymous$$onoScript.GetClass()) of a class, along with other useful info. Note that a my$$anonymous$$onoScript instance can be obtained with AssetDatabase methods (ie. AssetDatabase.LoadAssetAtPath(...)). Perhaps your menu system can be adapted to the $$anonymous$$onoScript type. Am I correct in my assessment?

avatar image numberkruncher · Oct 13, 2014 at 06:02 PM 0
Share

@aidesigner $$anonymous$$y class allows you to select any class including from .NET assemblies or the UnityEngine/UnityEditor libraries. Not just asset representations of scripts ($$anonymous$$onoScript).

avatar image numberkruncher · Oct 17, 2014 at 10:01 AM 0
Share

@aidesigner Also it should be pointed out that $$anonymous$$onoScript is an editor-only class.

avatar image aidesigner · Feb 20, 2015 at 03:50 PM 0
Share

Thanks for your very useful utility! Do you know how I can use your ClassTypeReference/PropertyDrawer to directly display a pop-up selection in my custom editor window. Basically it would be something like below, but greetingLoggerType is not of type SerializedProperty.

 ClassTypeReference greetingLoggerType;
 EditorGUILayout.PropertyField(greetingLoggerType);
avatar image numberkruncher · Feb 20, 2015 at 05:20 PM 1
Share

@aidesigner: You need to use serializedObject.FindProperty if you want to use EditorGUILayout.PropertyField. Believe it or not you can actually create a SerializedObject for an editor window since it contains serializable properties!

Show more comments
avatar image
0

Answer by SolidAlloy · May 09, 2020 at 04:52 PM

You can try this wrapper for newer versions of Unity since numberkruncher's one doesn't work properly in Unity 2019+: https://github.com/SolidAlloy/ClassTypeReference-for-Unity

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Why does the value of the public property of [Serializable] class persist in ScriptableObject between Editor runs ? 2 Answers

Stop a MonoBehavior from being addable as a script? 0 Answers

EditorApplication.projectWindowItemOnGUI and sub assets 1 Answer

Get list of all "Action" classes 1 Answer


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