Search Unity

Making a custom mesh asset readable at runtime

Discussion in 'Editor & General Support' started by customphase, Sep 16, 2019.

  1. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Im generating a mesh asset in the editor, which data i then access in the script. It works fine inside the editor, but after the build the mesh asset is marked as non readable, and as a result its failing to read any data from it in the script. There is a settable isReadable property in ModelImporter class, but my mesh asset is not a model, its just a mesh, therefore it doesnt work. These are all the settings i have available in there:

    upload_2019-9-16_15-51-49.png

    Did anyone encounter similar problem? How do i make it readable at runtime as well?
     
    radiantboy likes this.
  2. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    Alright, figured it out. You need to use the Force Text serialization mode, then you can just edit the asset file as a text file and change the m_IsReadable: 0 to 1. E.g:

    Code (CSharp):
    1. ...Save asset...
    2. string filePath = Path.Combine(Directory.GetCurrentDirectory(), assetPath);
    3. filePath = filePath.Replace("/", "\\");
    4. string fileText = File.ReadAllText(filePath);
    5. fileText = fileText.Replace("m_IsReadable: 0", "m_IsReadable: 1");
    6. File.WriteAllText(filePath, fileText);
    7. AssetDatabase.Refresh();
    If youre using mixed or binary serialization modes, then you could probably do the same but by using reflection instead.
     
  3. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    250
    The real question is: Why can't the Unity Editor edit .asset files with the importer (same as other formats)
     
    radiantboy and customphase like this.
  4. radiantboy

    radiantboy

    Joined:
    Nov 21, 2012
    Posts:
    1,633
    yes highly frustrating
     
  5. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    300
    Just use VSCode to edit this
    Code_K7yexT0hFo.png
     
    Whatever560 and AndBje like this.
  6. Ex6tra

    Ex6tra

    Joined:
    Oct 31, 2019
    Posts:
    14
    Use it carefully as it ruins the asset sometimes
     
  7. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    246
    It cant ruin anything if done correctly.
     
  8. Ex6tra

    Ex6tra

    Joined:
    Oct 31, 2019
    Posts:
    14
    You're right, I thought it was responsible for messing up my materials, days later I found out that another script did it but forgot I posted this reply.
     
  9. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    A better way to do this is to use SerializedObject:

    Code (CSharp):
    1. SerializedObject s = new SerializedObject(myMeshAsset);
    2. s.FindProperty("m_IsReadable").boolValue = true;