Making a custom mesh asset readable at runtime

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:

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

Did anyone encounter similar problem? How do i make it readable at runtime as well?

1 Like

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:

...Save asset...
string filePath = Path.Combine(Directory.GetCurrentDirectory(), assetPath);
filePath = filePath.Replace("/", "\\");
string fileText = File.ReadAllText(filePath);
fileText = fileText.Replace("m_IsReadable: 0", "m_IsReadable: 1");
File.WriteAllText(filePath, fileText);
AssetDatabase.Refresh();

If youre using mixed or binary serialization modes, then you could probably do the same but by using reflection instead.

5 Likes

The real question is: Why can’t the Unity Editor edit .asset files with the importer (same as other formats)

2 Likes

yes highly frustrating

Just use VSCode to edit this

2 Likes

Use it carefully as it ruins the asset sometimes

It cant ruin anything if done correctly.

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.

A better way to do this is to use SerializedObject:

SerializedObject s = new SerializedObject(myMeshAsset);
s.FindProperty("m_IsReadable").boolValue = true;
8 Likes