7

I would like to edit 1 vertex on a cube, but I don't know how to. I've tried looking everywhere for this function, but I can't find a solution.

Here is an image of what I want to achieve:

enter image description here

Serlite
  • 12,130
  • 5
  • 38
  • 49

4 Answers4

4

http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html

This code is not mine. Bellow is the same code as the link above. I just separated it into two files. (one per class)

It work quite good. BUT make sure to save you scene before using it, it's a bit buggy.

  • Don't forget to leave edit mode when you are done whit your modification.

  • You don't need to add "editMesh" tag to the gameobject you are modifying, or it will be erased when you leave edit mode.

  • Lastly, if you modify a primitive from unity, the modification will be applied for every instance of that primitive ! (if you change a cube for a pyramid, every cube will become a pyramid)

To avoid that make a copy of your primitive mesh, change the mesh you are using in your mesh renderer by your copy and then modify it. (bellow a script to add simple copy function in unity menu)

EditMesh.cs

#if UNITY_EDITOR
using UnityEngine;
using System.Collections;

/// <summary>
/// http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html
/// </summary>
[AddComponentMenu("Mesh/Vert Handler")]
[ExecuteInEditMode]
public class EditMesh : MonoBehaviour {

    public bool _destroy;

    private Mesh mesh;
    private Vector3[] verts;
    private Vector3 vertPos;
    private GameObject[] handles;

    private const string TAG_HANDLE = "editMesh";

    void OnEnable() {
        mesh = GetComponent<MeshFilter>().sharedMesh; // sharedMesh seem equivalent to .mesh
        verts = mesh.vertices;
        foreach (Vector3 vert in verts) {
            vertPos = transform.TransformPoint(vert);
            GameObject handle = new GameObject(TAG_HANDLE);
            //         handle.hideFlags = HideFlags.DontSave;
            handle.transform.position = vertPos;
            handle.transform.parent = transform;
            handle.tag = TAG_HANDLE;
            handle.AddComponent<EditMeshGizmo>()._parent = this;

        }
    }

    void OnDisable() {
        GameObject[] handles = GameObject.FindGameObjectsWithTag(TAG_HANDLE);
        foreach (GameObject handle in handles) {
            DestroyImmediate(handle);
        }
    }

    void Update() {
        if (_destroy) {
            _destroy = false;
            DestroyImmediate(this);
            return;
        }

        handles = GameObject.FindGameObjectsWithTag(TAG_HANDLE);

        for (int i = 0; i < verts.Length; i++) {
            verts[i] = handles[i].transform.localPosition;
        }

        mesh.vertices = verts;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();


    }

}

#endif

EditMeshGizmo.cs

#if UNITY_EDITOR
using UnityEngine;
using System.Collections;

/// <summary>
/// http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html
/// </summary>
[ExecuteInEditMode]
public class EditMeshGizmo : MonoBehaviour {

    private static float CURRENT_SIZE = 0.1f;

    public float _size = CURRENT_SIZE;
    public EditMesh _parent;
    public bool _destroy;

    private float _lastKnownSize = CURRENT_SIZE;

    void Update() {
        // Change the size if the user requests it
        if (_lastKnownSize != _size) {
            _lastKnownSize = _size;
            CURRENT_SIZE = _size;
        }

        // Ensure the rest of the gizmos know the size has changed...
        if (CURRENT_SIZE != _lastKnownSize) {
            _lastKnownSize = CURRENT_SIZE;
            _size = _lastKnownSize;
        }

        if (_destroy)
            DestroyImmediate(_parent);
    }

    void OnDrawGizmos() {
        Gizmos.color = Color.red;
        Gizmos.DrawCube(transform.position, Vector3.one * CURRENT_SIZE);
    }

}
#endif

CopyMesh.cs (put it in a directory named "Editor") (you should then find it in your menu bar)

using UnityEditor;
using UnityEngine;

namespace Assets {

    /// <summary>
    /// 
    /// </summary>
    public class CopyMesh : MonoBehaviour {

        [MenuItem("Assets/CopyMesh")]
        static void DoCopyMesh() {
            Mesh mesh = Selection.activeObject as Mesh;
            Mesh newmesh = new Mesh();
            newmesh.vertices = mesh.vertices;
            newmesh.triangles = mesh.triangles;
            newmesh.uv = mesh.uv;
            newmesh.normals = mesh.normals;
            newmesh.colors = mesh.colors;
            newmesh.tangents = mesh.tangents;
            AssetDatabase.CreateAsset(newmesh, AssetDatabase.GetAssetPath(mesh) + " copy.asset");
        }

        [MenuItem("Assets/CopyMeshGameObject")]
        static void DoCopyMeshGameObject() {
            Mesh mesh = (Selection.activeGameObject.GetComponent<MeshFilter>()).sharedMesh;
            Mesh newmesh = new Mesh();
            newmesh.vertices = mesh.vertices;
            newmesh.triangles = mesh.triangles;
            newmesh.uv = mesh.uv;
            newmesh.normals = mesh.normals;
            newmesh.colors = mesh.colors;
            newmesh.tangents = mesh.tangents;
            print(AssetDatabase.GetAllAssetPaths()[0]);
            AssetDatabase.CreateAsset(newmesh, AssetDatabase.GetAllAssetPaths()[0] + "/mesh_copy.asset");
        }
    }
}
Ambroise Rabier
  • 3,636
  • 3
  • 27
  • 38
3

Lately Unity has added access to the ProBuilder package via "Package Manager".

More info here: https://docs.unity3d.com/Packages/com.unity.probuilder@4.0/manual/index.html

Wiseman
  • 1,059
  • 2
  • 13
  • 24
1

Unity editor has no built-in mesh editor capabilities at the moment. I can advise you using Prototype plugin for that.

Skyblade
  • 2,354
  • 4
  • 25
  • 44
1

You can easily do that by iterating through the Vertices, which Unity will give you as a Vector3[] through the someObject.GetComponent<MeshFilter>().vertices field. See http://docs.unity3d.com/ScriptReference/Mesh-vertices.html for an example where the vertices are moved upwards with time.

Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
  • But there's no built-in way to persist these changes, correct? (Not that it makes the answer less valid--it's possible asker only wants runtime changes.) – 31eee384 Sep 23 '15 at 20:07
  • Yes, I'm really not aware of any *built-in* methods to also save that mesh. Now, you can real berserk on this and export your vertices data into some filefomat (.obj, whatever) and reimport the meshes, but you're right there. But then again, if you want to persisently change the vertices of a mesh, why not do the changes in a modelling editor and save it directly? – Maximilian Gerhardt Sep 23 '15 at 20:57
  • In this Unity answer there's a script that allows editing vertices. http://answers.unity3d.com/questions/14567/editing-mesh-vertices-in-unity.html – Carlos Ferreyra Nov 03 '16 at 20:15