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 /
  • Help Room /
avatar image
0
Question by Ebamber · Oct 22, 2015 at 09:50 PM · meshmeshfilterprimitivesquads

Create a custom quad mesh from script

Hi, I'm trying to develop a maze game in which the user has to navigate a ball towards the edge of the platform. The problem is that the way I'm doing it right now creates a number of game objects which massively slow everything down and I was wondering if there was some way I could either combine all of these game objects into one mesh or create a single game object which spans all of the area I need covered in a script.

I've checked out the code at http://wiki.unity3d.com/index.php/ProceduralPrimitives and I'm trying to do something similar to the cube example in which my vertices would be defined by randomly selected locations in my grid (like so):

 void createMesh(){
         MeshFilter mf = GetComponent<MeshFilter>();
         Mesh mesh = new Mesh();
         mesh.Clear();
         mf.mesh = mesh;
         //vertices
         Vector3[] verts = vertices.ToArray();    
         
         Vector3 up     = Vector3.up;
         Vector3 down     = Vector3.down;
         Vector3 front     = Vector3.forward;
         Vector3 back     = Vector3.back;
         Vector3 left     = Vector3.left;
         Vector3 right     = Vector3.right;
         
         Vector3[] normales = new Vector3[]
         {
             // Bottom
             down, down, down, down,
  
             // Left
             left, left, left, left,
  
             // Front
             front, front, front, front,
  
             // Back
             back, back, back, back,
  
             // Right
             right, right, right, right,
          
             // Top
             up, up, up, up
         };
         
         //triangles
         int[] tri = triangles.ToArray();
         //uvs
         Vector2[] uv = uvs.ToArray();
         
         mesh.vertices = verts;
         mesh.triangles = tri;
         mesh.uv = uv;
         mesh.RecalculateBounds();
         mesh.Optimize();
     }

 void Start () {    
         int n = (int)Random.Range(width*height*0.5f,width*height*0.8f);
         for(int i = 0;i < n;i++){
             x = Random.Range(0,temp.Count);
             foreach(Vector3 vec in temp[x].GetComponent<MeshFilter>().mesh.vertices){
                 vertices.Add(vec);
                 vertices.Add(vec + new Vector3(0,1,0));
             }
             temp.RemoveAt(x);
         }    
     }


As can be seen, all of my needed vectors (SHOULD) be in place for the algorithm and the normals are all there, my problem is with creating the triangles and uvs for the algorithm - I'm kind of stuck at this point, I don't really know how to define all of the triangles or uvs for the vertices I set (or even if I set the vertices right) so I was wondering if there was some tutorial somewhere on creating complex meshes programatically in Unity or if there was some easier way to do this.

Sincere thanks in advance for your help guys :)

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 Statement · Oct 24, 2015 at 01:01 AM 0
Share

Not a tutorial, but an example that maybe is similar to what you want to do. Someone asked how to generating a sprite tilemap from an oldschool ascii tilemap. It procedurally generates a mesh with uv, triangles, color etc. Question. Repo. $$anonymous$$eshBuilder.cs.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mesh missing in prefab 0 Answers

[Mesh] Unity 4 to Unity 5 Mesh Export 0 Answers

Changing a mesh with code but it doesn't react to the lighting 1 Answer

How to merge meshes in script for 65536 instances of the same primitive 0 Answers

trying to skew preafabs 0 Answers


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