x


Modifying mesh vertices in an editor script

I'm wondering if it's possible to change the position of mesh vertices in the editor. Here's my current code:

function OnSceneGUI()
{
    var mesh : Mesh = target.GetComponent(MeshFilter).mesh;
        var verts = mesh.vertices;

    for (var v in verts)
        v = v + Handles.PositionHandle( target.transform.position + v,
                                        Quaternion.identity            );

    mesh.vertices = verts;
}

Unity complains: "Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes. Please use MeshFilter.sharedMesh instead." But using sharedMesh wouldn't make a lot of sense, either.

more ▼

asked Apr 13 '10 at 07:06 AM

Torre gravatar image

Torre
127 4 4 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Actually you do have to do what Unity says and use sharedMesh. Otherwise, as it says, you'd leak meshes since they would only exist in the scene and not the project. Here is an editor script that makes custom planes, which uses sharedMesh. If it needs to be unique you can make a new mesh.

more ▼

answered Apr 13 '10 at 07:58 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

do you know if a box collider can be used for a plane generated using that script? http://answers.unity3d.com/questions/33818/box-collider-on-a-default-plane

Dec 30 '10 at 12:37 AM ina

You can use any collider on any object.

Dec 30 '10 at 02:13 AM Eric5h5

You can also give it a meshcollider and feed it the mesh you just created.

AddComponent(MeshCollider).sharedMesh = mesh;

Apr 05 '11 at 10:04 PM HarvesteR
(comments are locked)
10|3000 characters needed characters left

Here is my code for accessing/creating meshes at runtime or edit mode.

using UnityEngine;

[ExecuteInEditMode]

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]

public abstract class DynamicMesh : MonoBehaviour
{

    #region Protected Abstract Properties

    protected abstract Vector3[] vertices { get; }


    protected abstract Vector2[] uv { get; }


    protected abstract int[] triangles { get; }

    #endregion

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #region Protected Properties

    protected MeshFilter meshFilter
    {
       get
       {
         if(this._meshFilter == null)
         {
          this._meshFilter = gameObject.GetComponent();
         }
         return this._meshFilter;
       }
    }


    protected Mesh mesh
    {
       get
       {
         if(_mesh != null)
         {
          return _mesh;
         }
         else
         {

          if(meshFilter.sharedMesh == null)
          {
              Mesh newMesh = new Mesh();
              _mesh = meshFilter.sharedMesh = newMesh;
          }
          else
          {
              _mesh = meshFilter.sharedMesh;
          }
          return _mesh;
         }
       }
    }

    #endregion

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #region Protected Methods

    protected virtual void OnEnable()
    {
       ReCalculateMesh(true);
    }

    #endregion

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #region Private Methods

    private void ReCalculateMesh(bool allAttributes)
    {
       if(allAttributes)
       {
         if(mesh == null)
         {
          Debug.LogError("Could not access or create a mesh", this);
          return;
         }
         mesh.Clear();
       }
       mesh.vertices = vertices;

       if(allAttributes)
       {
         mesh.uv = uv;
         mesh.triangles = triangles;
       }
       mesh.RecalculateNormals();
       mesh.RecalculateBounds();
    }


    #endregion

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    #region Private Members

    private MeshFilter _meshFilter = null;
    private Mesh _mesh = null;

    #endregion
}
more ▼

answered May 05 at 06:32 AM

ianjosephfischer gravatar image

ianjosephfischer
31 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3340
x1677
x139
x62

asked: Apr 13 '10 at 07:06 AM

Seen: 6454 times

Last Updated: May 05 at 06:33 AM