This question was
closed Jun 24, 2019 at 12:40 AM by
Bunny83 for the following reason:
Duplicate Question https://answers.unity.com/questions/1643023/edit-skinnedmeshrenderer.html
Edit SkinnedMesh with vertices
Hello guys :) I have problem with my script for edit SkinnedMeshRenderer. When i added component with my script into gameObject i can modify my mesh using vertices even in prefab editmode. Look screenshot:
When I'm closing Unity and reopen the project my mesh back to original shape(vertices position are saved). Look screen.
Someone could help me how to save my modified Mesh save even when i reopen the project? I want to edit my mesh shape with vertices even i'm reopening the project. Here is code:
using System;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
[DisallowMultipleComponent]
public class BoneEditor : MonoBehaviour
{
public SkinnedMeshRenderer skinned;
public MeshCollider meshCollider;
public Mesh meshCreated;
Transform[] bones;
Matrix4x4[] bindPoses;
BoneWeight[] boneWeights;
public int count = 0;
Vector3[] vertices;
BoneWeight[] weights;
private void Awake()
{
skinned = GetComponent<SkinnedMeshRenderer>();
meshCreated = GetComponent<SkinnedMeshRenderer>().sharedMesh;
meshCollider = GetComponent<MeshCollider>();
}
void Start()
{
CreateLoadBone();
}
private void CreateLoadBone()
{
count += 1;
if (count > 1) { count = 2; }
if (this.gameObject.GetComponent<BoneEditor>().count == 1)
{
SkinnedMeshRenderer skinned = this.GetComponent<SkinnedMeshRenderer>();
if (skinned.sharedMesh == null || skinned.sharedMesh.vertexCount == 0)
return;
//Mesh mesh = (Mesh)UnityEngine.Object.Instantiate(skinned.sharedMesh);
meshCreated.RecalculateBounds();
meshCreated.RecalculateNormals();
meshCreated.RecalculateTangents();
bones = new Transform[meshCreated.vertexCount];
bindPoses = new Matrix4x4[meshCreated.vertexCount];
boneWeights = new BoneWeight[meshCreated.vertexCount];
// AssetDatabase.CreateAsset(meshCreated, "Assets/body.mesh");
// AssetDatabase.SaveAssets();
for (int index = 0; index < meshCreated.vertexCount; index++)
{
bones[index] = new GameObject(string.Format("Bone [{0}]", index)).AddComponent<VerticalesMidifierGizmo>().transform;
bones[index].SetParent(this.transform);
bones[index].position = this.transform.TransformPoint(meshCreated.vertices[index]);
bindPoses[index] = bones[index].worldToLocalMatrix * this.transform.localToWorldMatrix;
boneWeights[index].boneIndex0 = index;
boneWeights[index].weight0 = 1;
}
meshCreated.bindposes = bindPoses;
meshCreated.boneWeights = boneWeights;
skinned.sharedMesh = meshCreated;
skinned.bones = bones;
}
else
{
for (int index = 0; index < meshCreated.vertexCount; index++)
{
bones[index] = GetComponent<VerticalesMidifierGizmo>().transform;
bones[index].SetParent(this.transform);
bones[index].position = this.transform.TransformPoint(meshCreated.vertices[index]);
bindPoses[index] = bones[index].worldToLocalMatrix * this.transform.localToWorldMatrix;
boneWeights[index].boneIndex0 = index;
boneWeights[index].weight0 = 1;
}
}
}
private void Update()
{
vertices = meshCreated.vertices;
meshCreated.RecalculateBounds();
meshCreated.RecalculateNormals();
meshCreated.RecalculateTangents();
skinned.sharedMesh = meshCreated;
meshCollider.sharedMesh = skinned.sharedMesh;
}
}
public class BoneBuilder : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (GUILayout.Button("Create Bone from Verts"))
{
MakeBoneFromVerts();
}
}
[MenuItem("Bone Editor/Create Bone")]
public static void MakeBoneFromVerts()
{
if (Selection.gameObjects.Length == 0)
return;
GameObject go = new GameObject("Bone:SetPartName");
go.transform.parent = Selection.activeTransform.parent;
go.transform.position = new Vector3(0f, 0f, 0f);
foreach (GameObject g in Selection.gameObjects)
{
g.transform.parent = go.transform;
}
}
[MenuItem("Bone Editor/Add Vertecs")]
public static void MakeVertecsFromGO()
{
if (Selection.gameObjects.Length == 0)
return;
Selection.activeGameObject.AddComponent<BoneEditor>();
}
}
Thanks for the answers :)
Comment