Procedural Mesh to Asset, Mesh asset to Particle System

Hey all!

So, I’ve been trying to set a procedural generated mesh asset on a particle system, as a mesh shape of course, but although it does work, I get the following warning:

UnityEngine.Mesh:Clear()```
The thing is, procedural generated meshes are always flagged as writable unless specified otherwise, which baffles me because I create the mesh as plain as possible:

```csharp
AssetDatabase.CreateAsset(new Mesh(), filePath);

And of course during runtime I check the mesh if it’s writable (which is…):

meshAsset.isReadable

Edit:
What I’m trying to do is edit the mesh during editor mode. That’s why I call Mesh.Clear() (which shows in the warning). But the warning pops because the mesh is already assigned to the ParticleSystem.

Thanks guys! If you require more info, let me know. Your contribution is always appreciated! :slight_smile:

Anyone? Has no one ever faced this issue before?

I’m having the same issue now (unity 2019.2.13), not sure if I should take this warning seriously.
I have a procedurally generated flame mesh which gets cleared before rebuilding and brings up the warning in runtime. Can’t seem to correct it, and I have found no checks to prevent it from happening. Mesh itself gets created and modified just fine, though.

A little frustrating.

1 Like

Having same problem here. I’m working on a system to allow designer to paint regions on map wit particles. My code is generating the mesh while artist moving the mouse and the console is flooded with those warnings, no solution yet.

I’m having the exact same issue. I remember this working without any warnings before updating to 2019.2 or 2019.3.

This is how I fixed it (at least in edit mode):

mesh.SetTriangles(new int[0], 0);

Not calling Clear of course leads to a problem where old triangles might reference vertices that are no present in the new mesh… so clearing the triangles is the way to go to solve that :slight_smile:

Hi!
I solved this problem by not leaving vertices empty, e.g., we add zero vector in create mesh:

_vertices.Add(Vector3.zero);

In Update we assign this empty list:

Mesh.vertices = _vertices.ToArray();

Something like this at the end:

public class MeshGenerator : MonoBehaviour {
    public Mesh Mesh;
    private List<Vector3> _vertices = new List<Vector3>();

    // Start is called before the first frame update
    void Start() {
        Mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = Mesh;

        CreateShape();
        UpdateMesh();
    }

    private void UpdateMesh() {
        Mesh.Clear();
        Mesh.vertices = _vertices.ToArray();
    }

    private void CreateShape() {
        _vertices.Add(Vector3.zero);
    }
}

I created a hacky workaround to suppress this warning. I wrote a small struct called MeshWarningWorkArounder that takes a MeshRenderer, finds all particle systems that are using this MeshRenderer, temporarily removes said MeshRenderer from those particle systems, then restores them later.

See the gist here for the code:

Sample usage:

using (new MeshWarningWorkArounder(meshRenderer))
{
    mesh.Clear();
           
    // Update mesh here...
}

The struct uses the C# disposable pattern for convenience.

Note this assumes that that the mesh you want to modify is hooked up through a MeshRenderer but the code could be adapted to work directly for the mesh as well.