Search Unity

Scriptable Object not serializing

Discussion in 'Scripting' started by Sock Puppet, Jul 27, 2012.

  1. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    Can anyone help figure out why my scriptable object asset won't serialize? It generates the data fine but when I reopen Unity the values are all reset.

    It's probably something stupid and I'm too sleepy to see it.

    Here's the editor script that creates the scriptable object asset, pretty sure this all correct.

    Code (csharp):
    1.  
    2. public class FontDataExtractor
    3. {
    4.     [MenuItem ("Custom/Extract Font Data")]
    5.     public static void ExtractFontData()
    6.     {
    7.         var font = Selection.activeObject as Font;
    8.  
    9.         if (font == null)
    10.         {
    11.             EditorUtility.DisplayDialog("No Font Selected","Select font from project tree","OK");
    12.             return;
    13.         }
    14.        
    15.         SerializedObject info = new SerializedObject(font);    
    16.         FontData fontData = null;
    17.            
    18.         string path = "Assets/FontData/" + font.name + "Data" + ".asset";      
    19.         object asset = AssetDatabase.LoadAssetAtPath(path, typeof(FontData));
    20.        
    21.         if(asset == null)
    22.         {
    23.             fontData = ScriptableObject.CreateInstance<FontData>();
    24.            
    25.             string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path);
    26.             AssetDatabase.CreateAsset(fontData, assetPathAndName);
    27.         }
    28.         else
    29.         {
    30.             fontData = asset as FontData;
    31.         }
    32.  
    33.  
    34.         fontData.kerning = info.FindProperty("m_Kerning").floatValue;
    35.         fontData.lineSpacing = info.FindProperty("m_LineSpacing").floatValue;
    36.         fontData.convertCase = info.FindProperty("m_ConvertCase").intValue;
    37.  
    38.         int size = info.FindProperty("m_CharacterRects.Array.size").intValue;
    39.        
    40.         fontData.glyphs = new FontData.Glyph[size];
    41.        
    42.         for (int i = 0; i < size; i++)
    43.         {
    44.             var glyph = new FontData.Glyph();
    45.            
    46.             glyph.index = info.FindProperty("m_CharacterRects.Array.data["+i+"].index").intValue;
    47.             glyph.uvs = info.FindProperty("m_CharacterRects.Array.data["+i+"].uv").rectValue;
    48.             glyph.verts = info.FindProperty("m_CharacterRects.Array.data["+i+"].vert").rectValue;
    49.             glyph.spacing = info.FindProperty("m_CharacterRects.Array.data["+i+"].width").floatValue;
    50.            
    51.             fontData.glyphs[i] = glyph;
    52.         }
    53.        
    54.  
    55.         AssetDatabase.SaveAssets();
    56.         EditorUtility.FocusProjectWindow();
    57.         Selection.activeObject = fontData;
    58.     }
    59. }
    60.  

    Here's the scriptable object code...

    Code (csharp):
    1.  
    2. public class FontData : ScriptableObject
    3. {
    4.     public float kerning;
    5.     public float lineSpacing;
    6.     public int convertCase;
    7.     public Glyph[] glyphs;
    8.    
    9.    
    10.     [System.Serializable]
    11.     public class Glyph
    12.     {
    13.         public Rect verts;
    14.         public Rect uvs;
    15.         public float spacing;
    16.     }
    17. }
    18.  
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,655
    Demigiant likes this.
  3. Sock Puppet

    Sock Puppet

    Joined:
    Jan 9, 2011
    Posts:
    77
    Yep, that did the trick. Knew it would be something simple. Thanks Superpig.