10
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Peethor
    Mar 8, 2016 at 6:09

2 Answers 2

1
\$\begingroup\$

Adding a new answer because I cannot reply with a comment. PatrickSharbaughs code works for me in Unity 2019 using the Cloth component with some small changes. Basically, the state of the cloth simulation can be accessed using the BakeMesh method on the SkinnedMeshRenderer (which is a required component for the Cloth simulation).

using UnityEngine;
using UnityEditor;
using System.Collections;

public class SaveMeshBehaviour : MonoBehaviour
{
    public Mesh GetMesh()
    {

        Mesh newMesh = new Mesh();
        GetComponent<SkinnedMeshRenderer>().BakeMesh(newMesh);

        return newMesh;
    }
}

[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();
    }
}
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – RebDev
    Jul 10, 2015 at 6:40
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 10, 2015 at 13:41
  • \$\begingroup\$ Unfortunately it gives the error "Mesh.vertices is too small. The supplied vertex array has less vertices than are referenced by the triangle array." \$\endgroup\$
    – RebDev
    Jul 14, 2015 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .