At the start of a scene I have GameObject called 'sheet' positioned above a 'ball' GameObject. The sheet has an Interactive Cloth component on it and a Cloth Renderer. The Mesh for the Interactive Cloth component is a plane with a high number of vertices. At runtime the sheet drops over the ball and creates the shape I want. If I disable the Interactive Cloth component at this point the sheet mesh stay exactly as I want it but I cannot pull it out as a Prefab.

Does anyone know a way to get a copy of that shape?

For example by reading the mesh from the Interactive Cloth at runtime and then using that to change the mesh back how I want it without the Interactive Cloth component.

Any help would be greatly appreciated.

  • Alternatively, you could do cloth simulation in something like blender, save the mesh, and import that into unity. There's plenty of tutorials on how to do that online. – Peethor Mar 8 '16 at 6:09

A simple behaviour and custom inspector will help. Add this to your scripts:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class SaveMeshBehaviour : MonoBehaviour
{
    public Mesh GetMesh()
    {
        return gameObject.GetComponent<MeshFilter>().sharedMesh;
    }
}

[CustomEditor(typeof(SaveMeshBehaviour))]
public class SaveMeshInspector : Editor
{
    public override void OnInspectorGUI()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Save"))
        {
            var saveB = target as SaveMeshBehaviour;
            if (saveB != null)
            {
                var mesh = saveB.GetMesh();

                var copy = Mesh.Instantiate(mesh);
                AssetDatabase.CreateAsset(copy, "Assets/NewMesh.asset");
                AssetDatabase.SaveAssets();
            }
        }
        GUILayout.EndHorizontal();
    }
}

Then in the scene add the SaveMeshBehaviour to your cloth. The custom inspctor will make the new component have a Save button. When you are playing your scene, click the save button and it will save out a mesh that is a copy of the plane.

Hope this helps!

  • 2
    Thanks but it won't work for my example as there is no MeshFilter attached. That's because the Interactive Cloth component holds the Mesh. I tried modifying your script but I can only access "<InteractiveCloth>().vertices" and "<InteractiveCloth>().mesh". I think that the mesh it holds doesn't change as when I use your script to access the mesh it just outputs the original mesh. I think that the vertices are being modified but I don't know how to use that to create a new mesh. – RebDev Jul 10 '15 at 6:40
  • If you can get the array of vertices and the mesh. Try copying the mesh and then copy.vertices = cloth.vertices Follow that with creating an asset from the copy as above. Not sure it will work, but it might. – PatrickSharbaugh Jul 10 '15 at 13:41
  • Unfortunately it gives the error "Mesh.vertices is too small. The supplied vertex array has less vertices than are referenced by the triangle array." – RebDev Jul 14 '15 at 9:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.