• 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 pdunton · Jan 12, 2016 at 05:52 AM · javascriptoptimizationtexture2darray of gameobjects

Ant Colony Frame Rate Optimization

Hi. I have a script that runs an algorithm that simulates the behavior of an ant or many ants. The when the ant isnt standing on a quad that is called a "square" it creates one using Instantiate and adds it to the array of squares. As the ant moves very quickly, it creates lots and lots of squares. Up to 28,059 squares can occur and as I add in more ants or let one ant run for a while, the game becomes very slow due to the large number of squares in the scene. I am looking for a new way to do this so that the frame rate is increased and I an add more ants with ease. The only two requirements for a square is that it needs to be read as either black or brown and needs to be able to be changed between the two colors. I was thinking about using one texture and then editing the pixels on it. But I read that Texture2D.Apply() is not very fast. What would you guys recommend for obtaining the fastest read, edit, and storage of all these squares? Here is the code:

 #pragma strict
 var moveRate : float;
 var queenRate : float;
 var antLifeTime : float;
 var step : int;
 var queenAnt : GameObject;
 var builderAnt : GameObject;
 var black : GameObject;
 var brown : GameObject;
 var blackMat : Material;
 var brownMat : Material;
 var squareParent : GameObject;
 var squares : GameObject[];
 var ants : GameObject[];
 var newAnt : GameObject;
 var rot : Quaternion;
 rot.eulerAngles = new Vector3(0, 0, 0);
 
 function Start () {
     
     InvokeRepeating("Move", 0.1f, moveRate);
     InvokeRepeating("Fall", 0.1f, 0.15);
     InvokeRepeating("QueenLayAnt", 0.1f, queenRate);
 }
 
 function Update () {
      squares = GameObject.FindGameObjectsWithTag("Square");
      ants = GameObject.FindGameObjectsWithTag("Ant");
      
      if(Input.GetMouseButtonDown(0)){
          var position : Vector2 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
              position = new Vector2(Mathf.Floor(position.x), Mathf.Floor(position.y));
              position = new Vector2(position.x + 0.5, position.y + 0.5);
          if(Input.GetKey(KeyCode.LeftShift)){
              newAnt = Instantiate(queenAnt, new Vector3(position.x, position.y, -1.5), rot);
              newAnt.transform.name = "Queen Ant";
          } else {
              if(Input.GetKey(KeyCode.LeftControl)){
                  newAnt = Instantiate(builderAnt, position, rot);
                  newAnt.transform.name = "Burried Ant";
                  Destroy(newAnt, antLifeTime);
              } else {
                  newAnt = Instantiate(builderAnt, position, rot);
                  newAnt.transform.name = "Builder Ant";
                  Destroy(newAnt, antLifeTime);
              }
         }
     }
 }
 
 function Fall(){
     for(var i : int = 0; i < ants.length; i++){
         if(ants[i].transform.position.y > 62.5){
             ants[i].transform.position.y -= 1;
         }
     }
 }
 
 function QueenLayAnt(){
     for(var i : int = 0; i < ants.length; i++){
         if(ants[i].transform.name == "Queen Ant"){
             newAnt = Instantiate(builderAnt, new Vector3(ants[i].transform.position.x, ants[i].transform.position.y, 0), rot);
             newAnt.transform.name = "Builder Ant";
             Destroy(newAnt, antLifeTime);
         }
     }
 }
 
 function Move(){
     step += 1;
     for(var i : int = 0; i < ants.length; i++){
         var currentSquare : GameObject;
         for(var o : int = 0; o < squares.length; o++){
             if(squares[o].transform.position == ants[i].transform.position){
                 currentSquare = squares[o];
             }
         }
         if(currentSquare == null && ants[i].transform.position.y < 63.5){
             if(ants[i].transform.name == "Builder Ant"){
                 currentSquare = Instantiate(brown, ants[i].transform.position, rot);
                 currentSquare.transform.parent = squareParent.transform;    
             }
         } // 199x141
         if(currentSquare !== null){
             if(ants[i].transform.name == "Builder Ant" || ants[i].transform.name == "Burried Ant"){
                 if(currentSquare.transform.name == "Brown" || currentSquare.transform.name == "Brown(Clone)"){
                     if(ants[i].transform.rotation.eulerAngles.z == 0){
                         if(ants[i].transform.position.x < 99.5){
                                 ants[i].transform.position.x += 1;
                                 ants[i].transform.rotation.eulerAngles.z = 270;
                             } else {
                                 ants[i].transform.position.x -= Random.Range(0, 3);
                                 ants[i].transform.rotation.eulerAngles.z = 270;
                             }
                     } else {
                         if(ants[i].transform.rotation.eulerAngles.z == 270){
                             if(ants[i].transform.position.y > -78.5){
                                 ants[i].transform.position.y -= 1;
                                 ants[i].transform.rotation.eulerAngles.z = 180;
                             } else {
                                 ants[i].transform.position.y += Random.Range(0, 3);
                                 ants[i].transform.rotation.eulerAngles.z = 180;
                             }
                         } else {
                             if(ants[i].transform.rotation.eulerAngles.z == 90){
                                 if(ants[i].transform.position.y < 62.5){
                                     ants[i].transform.position.y += 1;
                                     ants[i].transform.rotation.eulerAngles.z = 0;
                                 } else {
                                     ants[i].transform.position.y -= Random.Range(0, 3);
                                     ants[i].transform.rotation.eulerAngles.z = 0;
                                 }
                             } else {
                                 if(ants[i].transform.position.x > -99.5){
                                     ants[i].transform.position.x -= 1;
                                     ants[i].transform.rotation.eulerAngles.z = 90;
                                 } else {
                                     ants[i].transform.position.x += Random.Range(0, 3);
                                     ants[i].transform.rotation.eulerAngles.z = 90;
                                 }
                             }
                         }        
                     }
                     currentSquare.transform.name = "Black";
                     currentSquare.GetComponent(Renderer).material = blackMat;
                     ants[i].transform.name = "Builder Ant";
                     
                 } else {
                     if(currentSquare.transform.name == "Black" || currentSquare.transform.name == "Black(Clone)"){
                         if(ants[i].transform.rotation.eulerAngles.z == 0){
                             if(ants[i].transform.position.x > -99.5){
                                     ants[i].transform.position.x -= 1;
                                     ants[i].transform.rotation.eulerAngles.z = 90;
                                 } else {
                                     ants[i].transform.position.x += Random.Range(0, 3);
                                     ants[i].transform.rotation.eulerAngles.z = 90;
                                 }
                         } else {
                             if(ants[i].transform.rotation.eulerAngles.z == 270){
                                 if(ants[i].transform.position.y < 62.5){
                                     ants[i].transform.position.y += 1;
                                     ants[i].transform.rotation.eulerAngles.z = 0;
                                 } else {
                                     ants[i].transform.position.y -= Random.Range(0, 3);
                                     ants[i].transform.rotation.eulerAngles.z = 0;
                                 }
                             } else {
                                 if(ants[i].transform.rotation.eulerAngles.z == 90){
                                     if(ants[i].transform.position.y > -78.5){
                                         ants[i].transform.position.y -= 1;
                                         ants[i].transform.rotation.eulerAngles.z = 180;
                                     } else {
                                         ants[i].transform.position.y += Random.Range(0, 3);
                                         ants[i].transform.rotation.eulerAngles.z = 180;
                                     }
                                 } else {
                                     if(ants[i].transform.position.x < 99.5){
                                         ants[i].transform.position.x += 1;
                                         ants[i].transform.rotation.eulerAngles.z = 270;
                                     } else {
                                         ants[i].transform.position.x -= Random.Range(0, 3);
                                         ants[i].transform.rotation.eulerAngles.z = 270;
                                     }
                                 }
                             }        
                         }
                         currentSquare.transform.name = "Brown";
                         currentSquare.GetComponent(Renderer).material = brownMat;
                         ants[i].transform.name = "Builder Ant";
                     }
                 }
             }
         }
     }
 }

alt text

alt text

untitled.png (10.9 kB)
untitled.png (73.4 kB)
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Hows to assign an objects children to its LOD component? 0 Answers

How to change rect size? 2 Answers

Making a GetComponent array 1 Answer

Working around Texture2D.Apply() performance issue 0 Answers

Is it better to check before you change variable 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