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
4
Question by Torre · Apr 13, 2010 at 07:06 AM · editorverticesedit

Modifying mesh vertices in an editor script

I'm wondering if it's possible to change the position of mesh vertices in the editor. Here's my current code:

function OnSceneGUI() { var mesh : Mesh = target.GetComponent(MeshFilter).mesh; var verts = mesh.vertices;

 for (var v in verts)
     v = v + Handles.PositionHandle( target.transform.position + v,
                                     Quaternion.identity            );

 mesh.vertices = verts;

}

Unity complains: "Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes. Please use MeshFilter.sharedMesh instead." But using sharedMesh wouldn't make a lot of sense, either.

Comment
Add comment · Show 1
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
avatar image midomido · Apr 02, 2014 at 10:29 AM 0
Share

http://forum.unity3d.com/threads/226406-Dynamic-$$anonymous$$esh-Vertex-Selection-for-ingame-or-editor-modification Here is a more pro solution it makes it very easy and also makes u control the indices alone ins$$anonymous$$d of the vertices which u can find more than 3 in the same position but I can still move them

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Eric5h5 · Apr 13, 2010 at 07:58 AM

Actually you do have to do what Unity says and use sharedMesh. Otherwise, as it says, you'd leak meshes since they would only exist in the scene and not the project. Here is an editor script that makes custom planes, which uses sharedMesh. If it needs to be unique you can make a new mesh.

Comment
Add comment · Show 8 · 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
avatar image ina · Dec 30, 2010 at 12:37 AM 0
Share

do you know if a box collider can be used for a plane generated using that script? http://answers.unity3d.com/questions/33818/box-collider-on-a-default-plane

avatar image Eric5h5 · Dec 30, 2010 at 02:13 AM 0
Share

You can use any collider on any object.

avatar image HarvesteR · Apr 05, 2011 at 10:04 PM 0
Share

You can also give it a meshcollider and feed it the mesh you just created.

AddComponent($$anonymous$$eshCollider).shared$$anonymous$$esh = mesh;

avatar image rakkarage · Mar 24, 2016 at 05:58 PM 0
Share

since they would only exist in the scene and not the project

that is the behavior i was trying to recreate. i am just generating mesh for tile map layers. i used to assign the new mesh to meshFilter.mesh and it would then persist in the scene so my changes to the map would last. now i get that error and when i uses shared$$anonymous$$esh my changes to not persist. how can i simulate the old behaviour? thank you
avatar image Bunny83 rakkarage · Mar 24, 2016 at 06:28 PM 1
Share

$$anonymous$$eshes aren't serialized inside a scene. When you create a copy or new instance of a mesh it will be gone once you reload the scene or if you restart Unity. If you want a $$anonymous$$esh to persist, it need to be stored as asset in the project. The same holds true for most other assets like $$anonymous$$aterial, Texture2D, AudioClip. All those can be cloned or created from scratch, but if you don't create an actual asset using the AssetDatabase class the object just exists in memory and will leak as soon as you change playmode, load a different scene or if you restart Unity.

Just forget about .mesh / .material inside an editor script. Those can only be used at runtime. If you want to create a clone of an existing object you have to create it manually using Instantiate:

 mf.shared$$anonymous$$esh = ($$anonymous$$esh)Instantiate(mf.shared$$anonymous$$esh);

This will create a temporary clone of the original $$anonymous$$esh and assign that to this mesh filter. Again, if that mesh isn't stored as asset in the project and not destroyed with DestroyImmediate when done, it will leak in the editor.

avatar image rakkarage Bunny83 · Mar 24, 2016 at 09:02 PM 0
Share

Thank you. I think i was mistaking some ExecuteInEdit$$anonymous$$ode code which rebuilds them for them being saved.

Show more comments
avatar image rakkarage · Mar 24, 2016 at 09:07 PM 0
Share

thanks. new url http://wiki.unity3d.com/index.php?title=CreatePlane

avatar image
1

Answer by ianjosephfischer · May 05, 2013 at 06:32 AM

Here is my code for accessing/creating meshes at runtime or edit mode.

 using UnityEngine;
 
 [ExecuteInEditMode]
 
 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 
 public abstract class DynamicMesh : MonoBehaviour
 {
 
     #region Protected Abstract Properties
 
     protected abstract Vector3[] vertices { get; }
 
 
     protected abstract Vector2[] uv { get; }
 
 
     protected abstract int[] triangles { get; }
 
     #endregion
 
     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
     #region Protected Properties
 
     protected MeshFilter meshFilter
     {
         get
         {
             if(this._meshFilter == null)
             {
                 this._meshFilter = gameObject.GetComponent();
             }
             return this._meshFilter;
         }
     }
 
 
     protected Mesh mesh
     {
         get
         {
             if(_mesh != null)
             {
                 return _mesh;
             }
             else
             {
 
                 if(meshFilter.sharedMesh == null)
                 {
                     Mesh newMesh = new Mesh();
                     _mesh = meshFilter.sharedMesh = newMesh;
                 }
                 else
                 {
                     _mesh = meshFilter.sharedMesh;
                 }
                 return _mesh;
             }
         }
     }
 
     #endregion
 
     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
     #region Protected Methods
 
     protected virtual void OnEnable()
     {
         ReCalculateMesh(true);
     }
 
     #endregion
     
     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
     #region Private Methods
 
     private void ReCalculateMesh(bool allAttributes)
     {
         if(allAttributes)
         {
             if(mesh == null)
             {
                 Debug.LogError("Could not access or create a mesh", this);
                 return;
             }
             mesh.Clear();
         }
         mesh.vertices = vertices;
 
         if(allAttributes)
         {
             mesh.uv = uv;
             mesh.triangles = triangles;
         }
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
     }
 
 
     #endregion
     
     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
     #region Private Members
 
     private MeshFilter _meshFilter = null;
     private Mesh _mesh = null;
 
     #endregion
 }
 
Comment
Add comment · Show 2 · 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
avatar image hxsdup1 · Mar 28, 2014 at 02:36 PM 0
Share

hi ianjosephfischer,please tell how to use your Dynamic$$anonymous$$esh class ? Dynamic$$anonymous$$esh class is abstract,all function is protected,how to use it modifying mesh???

avatar image ianjosephfischer · Apr 02, 2014 at 04:21 AM 0
Share

The methods are the important part to focus on. I would not recommend a strait "copy-paste" of this code, but rather to see the methods called and learn the steps in procedurally generating and modifying meshes.

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

7 People are following this question.

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

Related Questions

Manually triggering a script from the editor (utility, macro etc.) 1 Answer

Editor script - any callback when target is first instantiated? 1 Answer

Stop Unity snap settings rounding to nearest hundred 2 Answers

Monodevelop crash my PC 2 Answers

how to get 2 vertices from mesh edge? 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