Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Procedural Mesh to Asset, Mesh asset to Particle System

Discussion in 'Editor & General Support' started by michailmoiropoulos, Nov 19, 2019.

  1. michailmoiropoulos

    michailmoiropoulos

    Joined:
    Aug 13, 2019
    Posts:
    2
    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:

    Mesh used in Particle System Shape Module is not valid, possibly due to missing read/write flag
    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:
    Code (CSharp):
    1. AssetDatabase.CreateAsset(new Mesh(), filePath);
    And of course during runtime I check the mesh if it's writable (which is...):
    Code (CSharp):
    1. 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! :)
     
    Last edited: Nov 19, 2019
  2. michailmoiropoulos

    michailmoiropoulos

    Joined:
    Aug 13, 2019
    Posts:
    2
    Anyone? Has no one ever faced this issue before?
     
  3. Bdelcast

    Bdelcast

    Joined:
    Jul 11, 2014
    Posts:
    23
    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.
     
    michailmoiropoulos likes this.
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    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.
     
  5. LavaPatrik

    LavaPatrik

    Joined:
    Feb 21, 2017
    Posts:
    12
    I'm having the exact same issue. I remember this working without any warnings before updating to 2019.2 or 2019.3.
     
  6. snlehton

    snlehton

    Joined:
    Jun 11, 2010
    Posts:
    99
    This is how I fixed it (at least in edit mode):

    Code (CSharp):
    1. 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 :)
     
  7. ctudzi40

    ctudzi40

    Joined:
    Apr 29, 2022
    Posts:
    1
    Hi!
    I solved this problem by not leaving vertices empty, e.g., we add zero vector in create mesh:
    Code (CSharp):
    1. _vertices.Add(Vector3.zero);
    In Update we assign this empty list:
    Code (CSharp):
    1. Mesh.vertices = _vertices.ToArray();
    Something like this at the end:
    Code (CSharp):
    1. public class MeshGenerator : MonoBehaviour {
    2.     public Mesh Mesh;
    3.     private List<Vector3> _vertices = new List<Vector3>();
    4.  
    5.     // Start is called before the first frame update
    6.     void Start() {
    7.         Mesh = new Mesh();
    8.         GetComponent<MeshFilter>().mesh = Mesh;
    9.  
    10.         CreateShape();
    11.         UpdateMesh();
    12.     }
    13.  
    14.     private void UpdateMesh() {
    15.         Mesh.Clear();
    16.         Mesh.vertices = _vertices.ToArray();
    17.     }
    18.  
    19.     private void CreateShape() {
    20.         _vertices.Add(Vector3.zero);
    21.     }
    22. }
     
  8. apparition

    apparition

    Joined:
    Jan 11, 2012
    Posts:
    116
    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:
    https://gist.github.com/tomvoros/312438fa600b3d6904c26555eeaa8153

    Sample usage:

    Code (CSharp):
    1. using (new MeshWarningWorkArounder(meshRenderer))
    2. {
    3.     mesh.Clear();
    4.            
    5.     // Update mesh here...
    6. }
    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.