Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by raqbiel · Jun 23, 2019 at 04:38 PM · meshverticesmesh renderermesh verticesmesh-deformation

Edit SkinnedMeshRenderer

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. alt text

But when i'm reopening my project my modifed prefab mesh back to the original form(vertices positions saving). alt text

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 :)
 
 
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 23, 2019 at 05:03 PM

Where is the mesh you're editing actually stored? If you edit the built-in cube mesh you will directly modify the mesh in memory. However the built-in asset is read-only. So the actual asset is not changed (which wouldn't make any sense since you could completely destroy the default mesh, forever). So when restarting Unity it will load the mesh again from the internal asset.


If you want to edit / change a mesh in the editor and want those changes to persist in the editor, you have to save that mesh as an asset in your project. From your code it's hard to tell where the mesh is actually comming from. We can see this commented line:

 //Mesh mesh = (Mesh)UnityEngine.Object.Instantiate(skinned.sharedMesh);

which assumes that in the past you probably instantiated the mesh before modifying it. This is the correct way if you try to manipulate a built-in mesh. However if you create a clone and modify it, you have to save it as asset. If you don't save the cloned mesh, the mesh instance will simply not exist after a restart and the reference to the mesh would be null.


Finally, be careful with "ExecuteInEditMode". You seem to abuse this attribute to add editor functionality to your game. However that's not what this attribute is meant for. It's meant to provide a certain runtime feature during edit time (an example would be a particle system which is playing during edit time). Any actual editor functionality should be implemented in an actual editor script.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to manipulate(flatten) the Mesh of an object 0 Answers

Accessing mesh vertices is extremely inefficient; any workarounds? 0 Answers

Adding vertices to procedural mesh generator 2 Answers

Multiple Meshes 0 Answers

Handles does not modify mesh... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges