Search Unity

I broke Unity Cube mesh with Pure ECS, via shared component.

Discussion in 'Entity Component System' started by Antypodish, Aug 21, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    So I have edited few cubes vertices via pure ECS. I had little loop with vertices position update for test and assigned results back to shared component. I would expect, that result will be rather broken. But what I didn't expect, is that Unity Cube will be overwritten. So all prefabs and objects having cube, now are broken. Regardless Unity is in play mode or not.

    Is this Unity expected behavior? I understand references to prefab and shared component. But Modifying Unity cube model itself?

    upload_2018-8-21_2-32-59.png

    There are 8 green cubes. Well what left of them. See Watchdog vertices, of shared component, with infinity position. Thing is I use that Unity Cubes on OOP Game Object as well. They not linked to ECS. So when I stop play, the cube stays broken.

    So now, if I create GameObject with Unity Cube Mesh, is just plane (quad), with broken vertices.

    I try to restart Unity, as I haven't saved project/scene yet.


    Edit
    Restart of unity fixes issue. But not sure, what will be the result, if I save project, if I repeat exercise.

    Question now is, how do I manipulate mesh safely? I should made a copy for shared component somehow I presume?
     
    Last edited: Aug 21, 2018
  2. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    Hi! This is standard, expected behavior in Unity. When the Editor launches, it loads some built-in resources from disk (primitive meshes, GUI skins/styles, etc), and those objects remain live for the Editor session (i.e. until the next time they are loaded from disk). Changes made to those live objects will persist for the Editor session. (This is also true of e.g., user-created ScriptableObjects.)

    The way to prevent this problem is to clone the source asset at start time via Object.Instantiate(), so any modifications are only done to the copy. In the MonoBehaviour paradigm, the MeshFilter component has two API points, mesh and sharedMesh, where the former creates a clone on first access precisely to bypass this issue. When you're accessing the mesh on the shared component data, it is akin to accessing sharedMesh on a MeshFilter component.

    In short, as long as you Instantiate the mesh assigned to your shared component data and reassign that clone to it at start time, you should be free to modify it without worrying about affecting the asset between runs.
     
    Antypodish likes this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Thx for a response.

    I will keep in mind, to instantiate prefabs in future, if expecting to modify shared mesh.