Hello guys!
i got the problem with my script for edit mesh. I’m trying to achieve edit mesh with vertices. When i’m using my script in prefab mode everything works fine.
But when i’m reopening my project my modifed prefab mesh back to the original form(vertices positions saving).
Someone culd help me to figure out how to save my modified mesh even i’m reopen the project? I wan’t to edit my mesh with vertices without assign script everytime when i reopen the project.
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 help :)