When should Mesh.MarkDynamic be called?

I’m currently prototyping a voxel terrain system and wondered when to call Mesh.MarkDynamic?

According to the documentation (Unity - Scripting API: Mesh.MarkDynamic):

What does it mean exactly? Should I be calling Mesh.MarkDynamic every time before I call Mesh.SetVertices? Should I call it once upon mesh creation? Or should I not bother calling it at all?

I forgot to mention that I had searched the web for an answer and couldn’t find one that cleared up the confusion.

You don’t need to call it every time. If you’ve already called it, it’ll do nothing.

I don’t believe that flag persists if the mesh is serialized however so its initial state is not dynamic.

1 Like

Thanks for your reply.

So, If I understand correctly now, I should call it once upon mesh creation, right?

Hmm, can you elaborate?

As it says, before you start changing it. It’s a performance optimization for when you intend to continually update it. It doesn’t have to be immediately after you create it.

If you were to, say, create an asset from the mesh, it doesn’t save this flag. If you’re not doing that, ignore the comment.

1 Like

I have similar question.
(Sorry to revive this topic…)

When I called Mesh.Clear(), should I call Mesh.MarkDynamic() again?
Or don’t need to call it again?

On the C++ side, Mesh::MarkDynamic is basically just:

void Mesh::MarkDynamic()
{
    if (!m_IsDynamic)
    {
        m_IsDynamic = 1;
        SetChannelsDirty(GetAvailableChannels(), true, MeshUpdateFlags::kDefault);
    }
}

Looking at the implementation of Mesh::Clear, it doesn’t seem to touch the m_IsDynamic field. I would assume that once you call Mesh::MarkDynamic, it is permanent for that instance.

2 Likes

Thank you so much! I’m relieved.

BTW, for the further study, could you please tell me how to check C++ implementation code like above?

I tried to build my code with IL2CPP, and grepped the source with “MarkDynamic” in “il2cppOutput” folder, but I couldn’t find above function…

Mesh::MarkDynamic is part of the Unity engine code, you unfortunately won’t see it in your IL2CPP output.

The code in my post is my own decompilation of the function that I made with the assistance of a disassembler (such as IDA, Ghidra, Binary Ninja, etc). Some versions of Unity come with unstripped PDB files which significantly improve the results you can get with those tools.

The alternative is to purchase a Unity source license if you want to see the real code, but those don’t come cheap.

1 Like

Thanks again!
I was surprised that you checked the native code with disassembler.

I think it’s a near last resort method.
You are awesome!

I’ll also consider to try it if I really want to investigate the native implementation on Unity.

(But, fundamentally, I think the disassembler is not necessary if detail information like above is available in Unity documentation…)

1 Like