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 /
This question was closed Jan 04, 2017 at 10:04 AM by Eric5h5.
avatar image
0
Question by BigYellow333 · Jan 04, 2017 at 09:58 AM · draw calls

Too many cubes in my program, and FPS is low...

Hi guys, I have a program which could generate cubes infinitely, this is it:

using UnityEngine; using System.Collections;

public class GOL: MonoBehaviour {

 private bool[,] grid;

 // X Size of grid
 public int rows = 10;

 // Density of initial random squares
 public int density = 5;

 // Speed of animation
 public int speed = 10;

 // Current row
 private int currentRow = 0;

 public GameObject prefabCube;

 // Use this for initialization
 void Start () {
     grid = new bool[rows, rows];
     
     // [. . .]
     // [. . .]
     // [. . .]
     // Iterate over the rows in order to randomly choose cubes to turn on.
     for(int i = 0; i < rows; i++)
     {
         for(int j = 0; j < rows; j++)
         {
             // 
             grid[i, j] = Mathf.Floor(Random.value * density) == 0;
             //GameObject.CreatePrimitive(PrimitiveType.Cube);
         }
     }
 }
 
 // Update is called once per frame
 void Update () {

     // Every speed number of frames we add another row
     if (Time.frameCount % speed == 0)
     {

         // Tick the game of life
         for (int i = 0; i < rows; i++)
         {
             for (int j = 0; j < rows; j++)
             {
                 // Get the indices of the adjacent cubes
                 int[,] indices = new int[8, 2]{ { j , i + 1 },
                                                 { j + 1, i + 1 },
                                                 { j + 1, i },
                                                 { j + 1, i - 1 },
                                                 { j, i - 1 },
                                                 { j - 1, i - 1 },
                                                 { j - 1, i },
                                                 { j - 1, i + 1 } };

                 // Check to see if these cubes exist
                 int count = 0;
                 for(int k = 0; k < 8; k++)
                 {
                     int x = indices[k, 0];
                     int y = indices[k, 1];
                     if ( x < 0
                         || y < 0
                         || x > (rows - 1)
                         || y > (rows -1))
                     {
                         continue;
                     }

                     if(grid[x, y])
                     {
                         count++;
                     }
                 }

                 if(grid[j, i])
                 {
                     // cell is currently alive
                     if(count < 2)
                     {
                         // Die
                         grid[j, i] = false;
                     } else if( count > 3 )
                     {
                         grid[j, i] = false;
                     }
                 } else
                 {
                     // cell is currently dead
                     if (count == 3)
                     {
                         grid[j, i] = true;
                     }
                 }
             }
         }

         // Add row
         for (int i = 0; i < rows; i++)
         {
             for (int j = 0; j < rows; j++)
             {
                 // Check if this cube is alive
                 if(grid[i,j] == true)
                 {
                     // get position of cube
                     // generate cube
                     Vector3 position = new Vector3(j, 0, i);
                     GameObject cube = prefabCube;

                     // Add to parent before setting global position
                     cube.transform.SetParent(gameObject.transform);
                     cube.transform.position = position;
                 } 
             }
         }

         // Move structure up
         gameObject.transform.position += new Vector3(0, 0.5f, 0);
     }

     
 }

}

after a while, the fps would be very low, I think there is too many draw calls. Is there any way to solve this problem? Thank you

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

  • Sort: 
avatar image
0

Answer by Eric5h5 · Jan 04, 2017 at 10:03 AM

Use the Mesh class to create objects rather than using separate cubes. Do a search for things like "minecraft unity" and you can get lots of info since this has been asked and answered many times.

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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Baked Light Maps 2 Answers

Strange static batching behaviour 0 Answers

Draw call increased on duplicated object. But not on others 0 Answers

Share material to reduce draw calls 3 Answers

Performance issues with grid based terrain 2 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