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
0
Question by DavidD · Aug 14, 2012 at 09:11 PM · cubecreatevoxelminecraftface

Creating multiple faces (cubes)

Hey there!

I skimmed through the "After playing Minecraft" thread and the "Star, a Unity C# Tutorial" tutorial and came up with this code:

 using UnityEngine;

 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
 public class NewMesh : MonoBehaviour {
 private Mesh mesh;

 private Vector3[] vertices;
 private int[] triangles;
 private Vector2[] uv;

 private readonly byte size = 16;

 void Start()
 {
     mesh = new Mesh();
     mesh = createNewMesh(Vector3.zero);
 }

 Mesh createNewMesh(Vector3 position)
 {
     Mesh returnMesh = new Mesh();
     GetComponent<MeshFilter>().mesh = returnMesh;
     returnMesh.name = "Mesh" + position;

     vertices = new Vector3[4] { new Vector3(Mathf.Floor(position.x / size) * size, 0, Mathf.Floor(position.x / size) * size), 
                                 new Vector3((Mathf.Floor(position.x / size) + 1) * size, 0, Mathf.Floor(position.x / size) * size),
                                 new Vector3(Mathf.Floor(position.x / size) * size, 0, (Mathf.Floor(position.x / size) + 1) * size), 
                                 new Vector3((Mathf.Floor(position.x / size) + 1) * size, 0, (Mathf.Floor(position.x / size) + 1) * size) };

     triangles = new int[6] { 0, 2, 1, 1, 2, 3 };

     uv = new Vector2[4] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1) };

     returnMesh.vertices = vertices;
     returnMesh.triangles = triangles;
     returnMesh.uv = uv;

     returnMesh.RecalculateNormals();

     return returnMesh;
 }
 }

Which I then attached to an empty GameObject. It creates a plane at a given position which is aligned to a grid. My questions are: How would I go about creating multiple planes? Is having this script attached to an empty GameObject the best way to do it?

No worries, I'm not trying to re-create Minecraft! Just experimenting.

I would appreciate help!

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 OperationDogBird · Aug 15, 2012 at 12:14 AM 0
Share

To create multiple planes just call createNew$$anonymous$$esh as many times as needed, just change the position overload so you can view them(otherwise theyll stack).

A better way may be to create a variable to hold the first plane and then just instantiate that plane over and over, this would avoid having to keep creating the same mesh from scratch. Not sure which is heavier since the mesh is so simple.

1 Reply

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

Answer by Paulo-Henrique025 · Aug 15, 2012 at 12:38 AM

Lets say you want 100 of these planes you created:

 for(int i =0; i < 100; i++)
 {
       GameObject g = new GameObject();
       g.AddComponent<MeshFilter>();
       g.AddComponent<MeshRenderer>();
       g.AddComponent<NewMesh>();
 }

And its done, as your script auto execute itself its just a case of creating new objects and them put your script on to them.

But the way I've done it above you'll have 100 GameObjects with useless script inside them. I think its better to have a generator that creates objects, add MeshFilter and MeshRender and them change the mesh.

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 DavidD · Aug 15, 2012 at 08:40 AM 0
Share

Thank you! This is exactly what I was looking for!

Yes, I will put the generator in a static class and put it in a separate script. If that's what you mean?

avatar image Paulo-Henrique025 · Aug 15, 2012 at 05:55 PM 0
Share

Thats exactly what I meant, this way there will be no useless scripts.

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

9 People are following this question.

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

Related Questions

Cube style terrain 1 Answer

Best way to render bunch of cubes/planes? 3 Answers

How to get minecraft-like game to generate blocks and still run OK and not slow afterward? 1 Answer

Unity - cube primitive clipping issues? 1 Answer

How to assign texture to a specific mesh in a chunk for a minecraft game? 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