Can't Get Mesh.vertices of GameObject Loaded from AssetBundle

I am trying to get a more accurate bounding box of a 3D model loaded from an AssetBundle at runtime. I tried using the Render, but the bounds become too big as the model is rotated.

I’ve read suggestions to use the MeshFilter.mesh.vertices instead. However, I keep getting the error: “Not allowed to access vertices on mesh ‘default’ (isReadable is false; Read/Write must ben enabled in import settings)”

The problem is that I don’t have access to the Read/Write checkbox in the Inspector because the model is loaded at runtime.

1 Like

But how do you create the AssetBundle? When you put your mesh inside you could chagge your settings there, or not? I mean the model must be in the project to be added to an AssetBundle so you should have the inspector of the mesh in your project view.

It’s a bit unfortunate that this setting is read only via code. If all fails you could try to set it via reflection.

For textures I have created a “dummy” one write enabled in Unity and loaded the images inside (from png) then manipualated this. But I’m not sure how that could be applied to meshes since there is no custom load method. There are some obj importers out there so you could try this way too.

I found one last year that has been treating me well:

// From: https://answers.unity.com/questions/988174/create-modify-texture2d-to-readwrite-enabled-at-ru.html
//
// Used for real in Jetpack Kurt

Texture2D duplicateTexture(Texture2D source)
{
    RenderTexture renderTex = RenderTexture.GetTemporary(
                source.width,
                source.height,
                0,
                RenderTextureFormat.Default,
                RenderTextureReadWrite.Linear);

    Graphics.Blit(source, renderTex);
    RenderTexture previous = RenderTexture.active;
    RenderTexture.active = renderTex;
    Texture2D readableText = new Texture2D(source.width, source.height);
    readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
    readableText.Apply();
    RenderTexture.active = previous;
    RenderTexture.ReleaseTemporary(renderTex);
    return readableText;
}
1 Like