Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by oprat · Sep 09, 2019 at 07:16 AM · serializationscriptableobjectimporter

ScriptedImporter and ScriptableObject

I have an issue with ScriptedImporter and ScriptableObjects where a ScriptableObject imported through a custom ScriptedImporter appears empty when referenced in another object.

Here is the ScriptableObject class that is imported which is basically a container of Vector2:

 public class SampleSequences : ScriptableObject
 {
     public List<List<Vector2>> Sequences = new List<List<Vector2>>();
 }

The ScriptedImporter is in charge of reading an asset file and filing in the Sequences member with the Vector2s, as such:

 [ScriptedImporter(1, "sseq")]
 public class SampleSequenceImporter : ScriptedImporter
 {
     public override void OnImportAsset(AssetImportContext ctx)
     {
         SampleSequences sampleSequences = ScriptableObject.CreateInstance<SampleSequences>();
         var lines = File.ReadLines(ctx.assetPath);
         var culture = CultureInfo.GetCultureInfo("en-US");
         Vector2Sequence currentSequence = null;
 
         foreach (string line in lines)
         {
             if (line.StartsWith("//"))
             {
                 if (line.StartsWith("// Sequence"))
                 {
                     currentSequence = new Vector2Sequence();
                     sampleSequences.Sequences.Add(currentSequence);
                 }
             }
             else
             {
                 var tokens = line.Split(new char[] { ' ' });
                 var sample = new Vector2();
                 if (!float.TryParse(tokens[0], NumberStyles.Float, culture, out sample.x))
                 {
                     DestroyImmediate(sampleSequences);
                     return;
                 }
                 if (!float.TryParse(tokens[1], NumberStyles.Float, culture, out sample.y))
                 {
                     DestroyImmediate(sampleSequences);
                     return;
                 }
                 currentSequence.Add(sample);
             }
         }
 
         ctx.AddObjectToAsset("MainAsset", sampleSequences);
         ctx.SetMainObject(sampleSequences);
     }
 }


When I break in the OnImportAsset method, I see the SampleSequences object is properly filled with 100 elements in the Sequences property. Now the other ScriptableObject referencing this "imported" SampleSequences object goes like this:

 [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/Config", order = 1)]
 public class Config : ScriptableObject
 {
     public SampleSequences SampleSource;
     public int SampleSequenceIndex;
 
     private void OnEnable()
     {
         var sequenceIndex = SampleSequenceIndex % SampleSource.Sequences.Count;
     }
 }

I created an instance of this Config object through the asset menu and set its SampleSource property to the imported SampleSequences object. So, on paper, I would assume that my Config object should have a reference to a SampleSource object with 100 elements in it. Unfortunately, when I put a breakpoint inside the OnEnable() method of the Config object, I see that there is indeed a SampleSequences object in the SampleSource property but with 0 elements in it! What am I doing wrong?

PS: I've also tried calling directly AssetDatabase.LoadAssetAtPath in the OnEnable method of the Config object but get the same SampleSequences object with empty elements.

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
0

Answer by oprat · Sep 09, 2019 at 07:57 AM

Problem solved!!!

The problem was related to serialization as the SampleSequences class had a nested container property in the form of a List of List and this is not serializable (see "Serialization Rules" in Script Serialization

The fix was to break down the innermost List into a custom internal class.

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

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

How can I load an custom file from the Assets folder? 1 Answer

Unity3D ScriptableObject with UnityEvent not saving event parameter 0 Answers

SerializableProperty for a gameObject inside an array of a custom class inside of a ScriptableObject not setting. 0 Answers

How to break reference from .asset classes and runtime copied values? 2 Answers

Serializable Class On ScriptableObject Null when loaded from AssetBundle 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