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:
![alt text][1]
When I'm closing Unity and reopen the project my mesh back to original shape(vertices position are saved). Look screen.
![alt text][2]
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();
meshCreated = GetComponent().sharedMesh;
meshCollider = GetComponent();
}
void Start()
{
CreateLoadBone();
}
private void CreateLoadBone()
{
count += 1;
if (count > 1) { count = 2; }
if (this.gameObject.GetComponent().count == 1)
{
SkinnedMeshRenderer skinned = this.GetComponent();
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().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().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();
}
}
Thanks for the answers :)
[1]: https://i.ibb.co/yB3gtvD/bonebefore.png
[2]: https://i.ibb.co/bXMtG3W/boneafter.png
↧