Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to optimize

Discussion in 'Scripting' started by wyznawca, Jun 28, 2016.

  1. wyznawca

    wyznawca

    Joined:
    Dec 27, 2012
    Posts:
    27
    Hi, i have my script to find a way in my hexagonal world. But its really slow. How can i optimize it?

    In short, i am calling same method for all neighbours of my hex, then from end, adding hex, and return the way ("sciezka") back, then check all ways and get the shortest, add hex and return again.

    Code (CSharp):
    1. public List<GameObject> FindWay(List<GameObject> used) {
    2.         List<List<GameObject>> sciezka = new List<List<GameObject>>();
    3.         List<GameObject> usedT = new List<GameObject>();
    4.         used.Add(this.gameObject);
    5.         if (finish != true) {
    6.             for (int i = 0; i < neighbours.Length; i++) {
    7.                 if (neighbours[i] != null)
    8.                     Debug.LogError(used.Count);
    9.                 if (neighbours[i] != null && !used.Contains(neighbours[i])) {
    10.                     sciezka.Add(neighbours[i].GetComponent<Hex>().FindWay(new List<GameObject>(used)));
    11.  
    12.                 }
    13.             }
    14.         }
    15.  
    16.         int index = 0;
    17.      
    18.         for (int i = 0; i < sciezka.Count; i++) {
    19.             if (sciezka[i].Count < sciezka[index].Count && sciezka[i][0].GetComponent<Hex>().finish == true)
    20.                 index = i;
    21.         }
    22.         if(sciezka.Count == 0)
    23.             sciezka.Add(new List<GameObject>());
    24.         sciezka[index].Add(this.gameObject);
    25.         return sciezka[index];
    26.     }
     
    Last edited: Jun 28, 2016
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    A*

    Similar to what you are doing I guess. You would see gains using a 3rd party asset like http://arongranberg.com/astar/ but I guess you want to do it yourself. The only thing I can think of is that for your own way, spread the work over a few frames. Player won't notice...

    Your method probably hurts memory performance with garbage collector though, adding and removing things on the fly.
     
    wyznawca likes this.
  3. wyznawca

    wyznawca

    Joined:
    Dec 27, 2012
    Posts:
    27
    Ya, i know this, but wanted to try something made by myselfe. Guess i am still not good enough. Thanks anyway
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    wyznawca likes this.
  5. wyznawca

    wyznawca

    Joined:
    Dec 27, 2012
    Posts:
    27
    Thanks :D