• 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 /
  • Help Room /
avatar image
Question by Jeff-Kesselman · May 06, 2016 at 05:05 PM · serializationtestingserializedpropertyserializedobject

How to test an objects Unity serializability?

I am trying to write a test harness to test an custom class''s Serializability with the Unity Serializer. My current solution is to make it a field on a parent ScriptableObject, serialize that ScriptableObject, and then attempt to read it back as a property off the SerializedObject.

Its all working except the last part, when I try to read it back I get null. I've examined the property and it seems to have null fro its object value, but says it has chidren (which I've been unable to drill down into)>

Here are the classes:

Class being tested:

     using UnityEngine;
     using System.Collections;
     
     [System.Serializable]
     public class SerializableTypeReference : UnityEngine.Object{
         [SerializeField]
         private string _name;
        
         private System.Type _type=null;
     
         public SerializableTypeReference()
         {
     
         }
     
         public SerializableTypeReference(System.Type t)
         {
             _name = t.AssemblyQualifiedName;
             _type = t;
         }
     
         public SerializableTypeReference(string name)
         {
             _name = name;
             _type = null;
         }
     
         public System.Type Type
         {
             get
             {
                 if (_type == null)
                 {
                     _type = System.Type.GetType(_name);
                 }
                 return _type;
             }
            
         }
     
     }


ScriptableObject wrapper:

 using UnityEngine;
     using System.Collections;
     
     public class SerializationTestClass : ScriptableObject
     {
         
         public SerializableTypeReference stype;
     
     }
     

Test Code:

 using UnityEngine;
 using UnityEditor;
 using NUnit.Framework;
 
 [TestFixture]
 public class SerializableTypesTest {
     
     [Test]
     public void TypeReferenceTest()
     {
         Assert.DoesNotThrow(delegate ()
         {
             System.Type t = typeof(SerializableTypesTest);
             Assert.IsNotNull(t);
             SerializationTestClass test = ScriptableObject.CreateInstance<SerializationTestClass>();
             Assert.IsNotNull(test);
             test.stype = new SerializableTypeReference(t);
             Assert.IsNotNull(test.stype);
             SerializedObject ser = (SerializedObject)new SerializedObject(test);
             Assert.IsNotNull(ser);
             SerializedProperty prop = ser.FindProperty("stype");
             Assert.IsNotNull(prop);
             SerializableTypeReference test2 = prop.objectReferenceValue as SerializableTypeReference;
             Assert.IsNotNull(test2);
             Assert.IsTrue(t == test2.Type);
         });
         
     }
 }
 

Exception in test:

 TypeReferenceTest [0.013s]
 ---
 Expected: No Exception to be thrown
   But was:   (  Expected: not null
   But was:  null
 )
   at NUnit.Framework.Assert.That (System.Object actual, IResolveConstraint expression, System.String message, System.Object[] args) [0x00000] in <filename unknown>:0 
   at NUnit.Framework.Assert.IsNotNull (System.Object anObject) [0x00000] in <filename unknown>:0 
   at SerializableTypesTest.<TypeReferenceTest>m__2 () [0x00060] in C:\Users\jeffp\Plastic\UA\GGVRExp\Assets\Scripts\Utilities\Types\UnitySerializable\Editor\SerializableTypesTest.cs:24 
   at NUnit.Framework.Constraints.VoidInvocationDescriptor.Invoke () [0x00000] in <filename unknown>:0 
   at NUnit.Framework.Constraints.ExceptionInterceptor.Intercept (System.Object invocation) [0x00000] in <filename unknown>:0
 ---
 at SerializableTypesTest.TypeReferenceTest () [0x00000] in C:\Users\jeffp\Plastic\UA\GGVRExp\Assets\Scripts\Utilities\Types\UnitySerializable\Editor\SerializableTypesTest.cs:11




Comment

People who like this

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

0 Replies

· Add your reply
  • Sort: 

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

56 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 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 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

Serialization Placeholder Value for Different Fields? 1 Answer

Using EditorGUILayout.PropertyField on local attributes of the Editor Scripts 0 Answers

Cannot read past end of stream, MemoryStream 1 Answer

Keep editor variable value 0 Answers

SerializableProperty for a gameObject inside an array of a custom class inside of a ScriptableObject not setting. 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