- Home /
Editing mesh vertices in Unity
Is there a way to move mesh vertices directly in the Unity3D editor? I thought I saw something about this in the documentation one time, but I can't find it...
http://forum.unity3d.com/threads/226406-Dynamic-$$anonymous$$esh-Vertex-Selection-for-ingame-or-editor-modification Here is a more pro solution it makes it very easy and also makes u control the indices alone ins$$anonymous$$d of the vertices which u can find more than 3 in the same position but I can still move them
Answer by clunk47 · Nov 25, 2012 at 06:15 PM
Here's a pretty simple script I created that you can use and tweak however you wish. It isn't the best but will give u an idea of what you need to do. This fits my needs, but like I said change it however you wish :D
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class VertHandler : MonoBehaviour
{
Mesh mesh;
Vector3[] verts;
Vector3 vertPos;
GameObject[] handles;
void OnEnable()
{
mesh = GetComponent<MeshFilter>().mesh;
verts = mesh.vertices;
foreach(Vector3 vert in verts)
{
vertPos = transform.TransformPoint(vert);
GameObject handle = new GameObject("handle");
handle.transform.position = vertPos;
handle.transform.parent = transform;
handle.tag = "handle";
//handle.AddComponent<Gizmo_Sphere>();
print(vertPos);
}
}
void OnDisable()
{
GameObject[] handles = GameObject.FindGameObjectsWithTag("handle");
foreach(GameObject handle in handles)
{
DestroyImmediate(handle);
}
}
void Update()
{
handles = GameObject.FindGameObjectsWithTag ("handle");
for(int i = 0; i < verts.Length; i++)
{
verts[i] = handles[i].transform.localPosition;
}
mesh.vertices = verts;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
}
This solution. Works great. Thank you very much. I needed it to modify some prefabs i bought
No problem. If this solution worked for you better than the old accepted answer, perhaps you could accept this answer for others to easier find the solution :)
Sorry, I'm sorta slow...how would I use this script?
thank you:)
for newbies(like me): select your object -> Add Component -> New Script, Name: VertHandler. Simple paste this text in VertHandler.cs. Usage: Enable/Disable by checkbox.
So I've just come across this in 2017. I have managed to get this to work, but it wasn't quite as simple as the comments above suggest. I had to make a new script in my assets folder, paste this in and save as VertHandler. I then had to add two tags of "h" and "handle" so that the script functioned as intended.
Once the following is done, you can then drop this on to any gameobject with a mesh component, then activate the script to edit verts and deactivate when you're done with it. At one point I managed to make it appear in the component menu under Component>$$anonymous$$esh>Vert Handler, but now its not there. I'm sure this is user error on my part.
Seriously guys, this is fantastic, it does exactly what I was looking for. Please note, I also tried the version below with the gizmos but it didn't work very quickly and I was happy enough with this version to not feel the need to persist.
Answer by Luke Kellett · Nov 26, 2013 at 06:18 AM
Ok so it might be a few years late, but I needed something for this recently and found this code... I tweaked it a little to add nice little gizmo handles to each vert to make it easier to move things around. That a component menu entry under mesh to add the component and a bool property on each handle to remove them all...
Drop the script in and you're good to go.
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
[AddComponentMenu("Mesh/Vert Handler")]
[ExecuteInEditMode]
public class VertHandler : MonoBehaviour
{
public bool _destroy;
private Mesh mesh;
private Vector3[] verts;
private Vector3 vertPos;
private GameObject[] handles;
private const string TAG_HANDLE = "VertHandle";
void OnEnable()
{
mesh = GetComponent<MeshFilter>().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<VertHandleGizmo>()._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();
}
}
[ExecuteInEditMode]
public class VertHandleGizmo : MonoBehaviour
{
private static float CURRENT_SIZE = 0.1f;
public float _size = CURRENT_SIZE;
public VertHandler _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
Can someone elaborate on the "Drop the script in and you're good to go."?
Thanks!
What should the actual script file be named, as there are two $$anonymous$$onoBehaviors?
Since VertHandler calls VertHandleGizmo, the script attached to a GO should be named VertHandler.
Answer by KvanteTore · Apr 07, 2010 at 06:59 AM
Take a look at the procedural example: http://unity3d.com/support/resources/example-projects/procedural-examples
Almost what I want, but I was hoping to be able to actually drag vertices around in the editor, not change them in a script.
I'm not aware that this is possible out of the box, but you could write an editor script for that.
Oh. Sorry, on the second reading of your question I saw that was what you meant
Is there any documentation for using editor scripts? I can't find any information about them.
the procedural examples daunted me at first, too, but if you go through and try to understand each one, eventually they all seem to make sense... working on putting together an overview guide to the unity procedural examples - check back on http://www.brighthub.com/members/ina.aspx soon
Answer by jonas-echterhoff · Apr 07, 2010 at 09:52 AM
There are no built-in mesh editing tools in Unity. As spree wrote, you could write some yourself inside Unity using editor scripting, but unless you have some very specific needs, you will probably be much better of using any of the commonly available 3d modeling tools Unity supports - why reinvent the wheel, when there are plenty of well-developed tools to address this?
Answer by midomido · Apr 02, 2014 at 10:25 AM
http://forum.unity3d.com/threads/226406-Dynamic-Mesh-Vertex-Selection-for-ingame-or-editor-modification Here is a more pro solution it makes it very easy and also makes u control the indices alone instead of the vertices which u can find more than 3 in the same position but I can still move them