Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by aardvarkk · May 03, 2012 at 04:21 AM · meshprocedural meshscene view

Make procedural mesh show in Scene Editor, not just in "Play" mode

Hi there,

I have a Mesh that I generate in code. It shows up properly when I'm in "play" mode, but nothing at all appears in the Scene when I'm trying to edit it. I'm assuming it's possible to show what the Mesh looks like after a Start() call in the Scene Editor? It's kind of frustrating to have to keep "running" the scene to see what it looks like.

I've checked out similar questions, but all the answers seem to involve Mesh Colliders, and I don't have one. I've got a MeshFilter, my script to generate the vertices, triangles, uvs, normals, etc., and then the MeshRenderer. Again, it shows up properly when I am in Play mode, but not in the Scene Editor.

Thanks for any 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

5 Replies

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

Answer by AlucardJay · Mar 14, 2013 at 06:12 AM

I don't use the evil ExecuteInEditMode, but instead have a ContextMenu function at the top of the script. With this you can call functions on the script in edit mode. While this is not a wysiwyg approach, by simply making changes then running the ContextMenu reassigns the values and the mesh is updated.

Just an alternative to think about : http://docs.unity3d.com/Documentation/ScriptReference/ContextMenu.html

 #pragma strict
 @script RequireComponent(MeshFilter, MeshRenderer)
 
 #if UNITY_EDITOR
 // instead of @script ExecuteInEditMode()
 @ContextMenu("Construct sQuad")
 function ConstructQuad() 
 {
     Debug.Log("Constructing sQuad from ContextMenu");
     ConstructMesh();
 }
     
 function ConstructMesh() 
 {
     Startup();
 }
 #endif
 
 public class SQuad extends MonoBehaviour
 {
     private var mesh : Mesh;
     // other mesh variables here
     
     function Start() 
     {
         Startup();
     }
     
     function Startup() 
     {
         if ( !mesh )
         {
             //GetComponent(MeshFilter).mesh = mesh = new Mesh();
             mesh = new Mesh();
             GetComponent(MeshFilter).mesh = mesh;
             mesh.name = "sQuadMesh";
         }
         
         Construct();
     }
     
     function Construct()
     {
         
         mesh.Clear();
         verts = new Vector3[8]; 
         // all your mesh calculations here
         
         mesh.vertices = verts; 
         mesh.uv = uvs;
         mesh.triangles = tris;
         
         mesh.RecalculateBounds();
         mesh.RecalculateNormals();
     }
 }
Comment
Add comment · Show 1 · 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 camillo777 · Mar 24, 2021 at 10:37 AM 0
Share

Interesting, but how do you call the "Startup" function that is inside the class?

avatar image
1

Answer by rutter · May 03, 2012 at 05:53 AM

It won't behave exactly the same as during gameplay, but you might try giving your behavior script the ExecuteInEditMode attribute.

As an alternative, you could try writing a custom inspector that uses OnSceneGUI(). I find that's a little less intuitive, but might be easier to work with depending on what exactly you're trying to do.

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 aardvarkk · May 03, 2012 at 12:48 PM 0
Share

@rutter Interesting -- so what is it about, say, a cylinder that allows it to appear in the scene editor whereas my procedural mesh does not? I could easily change both during runtime, but one of them shows up as expected and the other is a blank. Are cylinders, cubes, etc. all marked as ExecuteInEdit$$anonymous$$ode?

avatar image rutter · May 03, 2012 at 06:46 PM 0
Share

That's an insightful question, but unfortunately one I can't answer off the top of my head. ;) Probably something to do with some quirk of interaction between a mesh and its renderer?

avatar image
0

Answer by GADoyle · Mar 14, 2013 at 05:40 AM

Having a similar problem. I have a procedurally generated mesh that I wish to display in the editor. I am using ExectueInEditMode, and the mesh is being passed correctly to the mesh filter. When run in the game, the mesh displays successfully.

However when Im in the editor it does not.

One thing I have noticed is that when previewing the mesh in the editor is says that it has the correct number of vertices, but NO TRIANGLES.

Both values are being assigned in the code in both play and editor mode. I am still trying to figure out the difference as well.

Have you found any answer?

and do you get the same issue when previewing the mesh?

Does anyone know why the triangles would not update the same way the vertices do in editor mode.

edit

Also worth noting, I do not use the triangles attribute to assign the triangle array. Instead I use the SetTriangles method to assign them to three separate submeshes. While in the editor, the submeshCount is still zero, after this assignment. I still can not figure out why though.

Comment
Add comment · Show 1 · 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 Mizipzor · May 20, 2014 at 01:02 PM 0
Share

This should be a comment, not an answer.

avatar image
0

Answer by Yamin-Nather · Jan 10, 2016 at 07:23 AM

you could add the [ExecuteInEditMode] attribute but it doesnt work exactly same like in runtime. for example, update runs only when there is a particular change and not every frame. But there's a way around this. Make a OnEnable() function and OnDisable() function and type in the code below. void OnEnable() { EditorApplication.update += Update; } void OnDisable() { EditorApplication.update -= Update; } Adding these lines makes the update function run every frame.

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
avatar image
0

Answer by BorisOkunskiy · May 21, 2020 at 08:23 AM

Something along the lines of this, combined with [ExecuteInEditMode], works for me:

     public Mesh mesh;
 
     void Update() {
         #if UNITY_EDITOR
         if (!Application.isPlaying) {
             bool needsUpdate = mesh == null ||
                 UnityEditor.Selection.activeGameObject == gameObject;
             if (needsUpdate) {
                 GenerateMesh();
                 var mf = GetComponent<MeshFilter>();
                 mf.sharedMesh = mesh;
             }
         }
         #endif
     }
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

10 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

Related Questions

How can I make my adjacent meshes' edges line up? 1 Answer

Vertex Colours flickering blue 0 Answers

Perlin Resources system 1 Answer

SpriteSheet/Mesh Hybrid to reduce overdraw on iOS? 2 Answers

Can and should a procedural mesh be shared between multiple GameObjects 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