C# 获取游戏对象在数组中的位置
我正在使用此脚本对数组字母数字进行排序:C# 获取游戏对象在数组中的位置,c#,arrays,unity3d,C#,Arrays,Unity3d,我正在使用此脚本对数组字母数字进行排序: public class AlphanumComparatorFast : IComparer<string> { public int Compare(string x, string y) { string s1 = x as string; if (s1 == null) { return 0; } string s2
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;
}
}
这是检查器中的输出:
我的问题是:有没有办法找出数组中当前对象的数目?我把这个脚本添加到所有的立方体中,那么有没有一种方法,如果我在脚本中加入一些代码,它会返回游戏对象在数组列表中的位置?例如,如果我调试Cube5,它将返回:
我在数组中的位置是4
我的目标是获得每个多维数据集的下一个多维数据集的下一个多维数据集:)。
所以Cube1应该给我Cube3,Cube3应该给我Cube5等等。。我想我可以使用答案,然后执行
variable+2
。我想要这个,因为每个立方体都必须获得下一个立方体的下一个立方体的一些信息。你在数组上循环并比较GameObject实例。如果匹配,则返回当前循环索引。如果没有匹配项,则返回负数,因为数组不能有负数
public GameObject[] allCubes;
int findIndex(GameObject target)
{
for (int i = 0; i < allCubes.Length; i++)
{
//If we find the index return the current index
if (allCubes[i] == target)
{
return i;
}
}
//Nothing found. Return a negative number
return -1;
}
编辑:
我建议您稍微更改一下当前的设计,并将该代码放在一个空的游戏对象上,以便执行一次
AlphanumComparatorFast
脚本。然后,您可以从其他脚本访问该数组结果。如何做到这一点,请参阅帖子。我这么说是因为它会加快你的比赛速度。这绝对完美!谢谢NP修改了我的答案,以展示如何改进现有的内容。从编辑往下读。如果你的问题解决了,别忘了回答。谢谢你的提示,我以前已经做过了。实际上,我只是把它放在一个单独的脚本中,但我没有把它附加到任何东西上,但它仍然有效。有可能吗?没有。您必须将其附加到游戏对象。一个空的游戏对象就可以了。查看我答案中的链接了解如何访问它。在Unity中,如果你想从另一个脚本访问脚本,你必须将该脚本附加到游戏对象。请参阅以了解有关Unity的更多信息。听起来您希望维护一个链接列表,这将加快几个数量级,具体取决于您的最终实现以及在创建多维数据集时是否可以通过引用链接这些多维数据集。您可能只需在FindGameObjects
返回的多维数据集上循环,并在那里设置引用。
public GameObject[] allCubes;
int findIndex(GameObject target)
{
for (int i = 0; i < allCubes.Length; i++)
{
//If we find the index return the current index
if (allCubes[i] == target)
{
return i;
}
}
//Nothing found. Return a negative number
return -1;
}
int index = findIndex(gameObject);