2017-07-24 36 views
0

我做了一個腳本,使所有多維數據集的當前場景的數組:排序遊戲對象數組的名字

public GameObject[] allCubes; 

void Awake() 
{ 
    allCubes = GameObject.FindGameObjectsWithTag("cube"); 
} 

的問題是該數組看起來像這樣的檢查:

https://i.gyazo.com/69f2f844183fe6e592e61c1517267da1.png

我已經嘗試這樣做:

public GameObject[] allCubes; 

void Awake() 
{ 
    allCubes = GameObject.FindGameObjectsWithTag("cube"); 
    Array.Sort (allCubes); 
} 

然而,T他給我一個錯誤:

InvalidOperationException: No IComparable or IComparable<UnityEngine.GameObject> interface found. 
System.Array.compare[GameObject] (UnityEngine.GameObject value1, UnityEngine.GameObject value2, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1756) 
System.Array.qsort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 low0, Int32 high0, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1722) 
System.Array.Sort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 index, Int32 length, IComparer`1 comparer) (at 

我該怎麼辦?

+1

那麼你如何期待它對'GameObject'進行排序呢? – DavidG

+0

或者,相關的問題 - *排序他們的目的*是什麼? – Adrian

+0

@DavidG我期望他們排序像Cube1 - Cube2 - Cube2 ....但我有一種感覺,我錯了.. –

回答

0

從你提供的截圖,該字符串也包含int值。你需要一個字母數字排序。有AlphanumComparatorFastdotnetperls作爲我使用和推薦。請參閱下面的可用於此的稍微修改版本。

public GameObject[] allCubes; 

void Awake() 
{ 
    allCubes = GameObject.FindGameObjectsWithTag("cube"); 
    allCubes = allCubes.OrderBy(obj => obj.name, new AlphanumComparatorFast()).ToArray(); 
} 

這裏是AlphanumComparatorFast類的修改版本:

public class AlphanumComparatorFast : IComparer<string> 
{ 
    public int Compare(string x, string y) 
    { 
     string s1 = x as string; 
     if (s1 == null) 
     { 
      return 0; 
     } 
     string s2 = y as string; 
     if (s2 == null) 
     { 
      return 0; 
     } 

     int len1 = s1.Length; 
     int len2 = s2.Length; 
     int marker1 = 0; 
     int marker2 = 0; 

     // Walk through two the strings with two markers. 
     while (marker1 < len1 && marker2 < len2) 
     { 
      char ch1 = s1[marker1]; 
      char ch2 = s2[marker2]; 

      // Some buffers we can build up characters in for each chunk. 
      char[] space1 = new char[len1]; 
      int loc1 = 0; 
      char[] space2 = new char[len2]; 
      int loc2 = 0; 

      // Walk through all following characters that are digits or 
      // characters in BOTH strings starting at the appropriate marker. 
      // Collect char arrays. 
      do 
      { 
       space1[loc1++] = ch1; 
       marker1++; 

       if (marker1 < len1) 
       { 
        ch1 = s1[marker1]; 
       } 
       else 
       { 
        break; 
       } 
      } while (char.IsDigit(ch1) == char.IsDigit(space1[0])); 

      do 
      { 
       space2[loc2++] = ch2; 
       marker2++; 

       if (marker2 < len2) 
       { 
        ch2 = s2[marker2]; 
       } 
       else 
       { 
        break; 
       } 
      } while (char.IsDigit(ch2) == char.IsDigit(space2[0])); 

      // If we have collected numbers, compare them numerically. 
      // Otherwise, if we have strings, compare them alphabetically. 
      string str1 = new string(space1); 
      string str2 = new string(space2); 

      int result; 

      if (char.IsDigit(space1[0]) && char.IsDigit(space2[0])) 
      { 
       int thisNumericChunk = int.Parse(str1); 
       int thatNumericChunk = int.Parse(str2); 
       result = thisNumericChunk.CompareTo(thatNumericChunk); 
      } 
      else 
      { 
       result = str1.CompareTo(str2); 
      } 

      if (result != 0) 
      { 
       return result; 
      } 
     } 
     return len1 - len2; 
    } 
} 

不要忘了與using System.Linq;


導入System.Linq;對字符串進行排序沒有你只需簡單地使用數字:

public GameObject[] allCubes; 

void Awake() 
{ 
    allCubes = GameObject.FindGameObjectsWithTag("cube").OrderBy(go => go.name).ToArray(); 
} 
+0

哇,這是一些複雜的東西,但非常感謝,它完美的作品! –

+0

不客氣! – Programmer

+0

小問題:有沒有辦法找出數組中的當前對象是什麼數字?我添加了這個腳本到所有的立方體,那麼有沒有辦法,如果我在腳本中放置一些代碼,它會返回數組列表中的那個gameobject的位置?例如,如果我調試Cube5,它將返回:「我在陣列中的位置是4「(正如你可以在這張圖中看到的:https://i.gyazo.com/0dd2c9fdf8e07f9f971cd074695fc061.png) –

0

你不說你要使用的立方體

這個代碼進行排序使用Array.Sort通過名稱來比較至極參數:

class GameObjectComparerByName : IComparer<GameObject> { 
int IComparer<GameObject>.Compare(GameObject x, GameObject y) 
{ 
    return string.Compare(x.name, y.name); 
} 

而且在任何地方使用:

Array.Sort(allCubes,new GameObjectComparer()); 

說明:

Array.Sort方法wor ks它必須實現IComparable<UnityEngine.GameObject>接口 C#編譯器不知道如何比較作爲GameObject實例的兩個立方體對象並拋出異常。

例子:

class Testy { 
    private int bla;   
    public int Bla 
    { 
     get { return bla; } 
     set { bla = value; } 
    } 
    public Testy(int bla) 
    { 
     this.bla = bla; 
    } 
} 

編譯器這麼想的知道如何解釋x>yx<=y等等 當x,y是示例類暴躁的兩個對象,但實現的IComparer界面

class TestyComparer : IComparer<Testy> { 


    int IComparer<Testy>.Compare(Testy x, Testy y) 
    { 
     if (x.Bla == y.Bla) 
     { 
      return 0; 
     } 
     else if (x.Bla > y.Bla) 
     { 
      return 1; 
     } 
     else // (x.Bla < y.Bla) 
     { 
      return -1; 
     } 
     //all lines works equals than: 
     //return x.Bla < y.Bla 
    } 
} 

,現在你可以使用 Array.Sort(testyArray, new TestyComparer());

在你的情況下實現

class GameObjectComparer : IComparer<GameObject> { 
    int IComparer<GameObject>.Compare(GameObject x, GameObject y) 
    { 
     //Compare the two cubes here 
     //By the parameter you want to use to sor them (volume, proximity etc..) 
     /* 
      return <=-1;//Less 
      return 0;//Equals 
      return >=1;//Greather 
     */   
    } 

,做

Array.Sort(allCubes,new GameObjectComparer()); 

如果你使用.NET 4.5或添加額外的課程,你可以使用比較程序接口拉姆達看到這個帖子Using lambda expression in place of IComparer argument

+0

感謝您的回覆。我真的不明白我在''在你的case implements'下面的代碼是怎麼處理的,我嘗試了一些東西,但是得到了錯誤 –

+0

我添加了一個和@Programmer做同樣種類但是使用Array的例子。排序 –