只需分解您真正想做的事情,您就会发现它是多么容易。就像我在评论部分提到的Dictionary 一样,您仍然需要另外两个 C# 功能来完成确切的语法。
你希望能够做到这一点:
foo="box"+2[random int];
与此相同:
int valueFromArray = arrayVariableName [index];
在下面的示例中,我们可以假设 MyScript 是我们脚本的名称。
1。你需要一个可以保存名字和int数组的字典。
Dictionary<string, int[]> boxes
2。您需要一种将 "box"+2 或 arrayVariableName 转换为当前脚本实例的方法。这可以通过 C# 中的隐式转换运算符功能来完成。您可以将 "box"+2 或 arrayVariableName 传递到这个隐式 MyScript 中,然后将其存储到全局变量中以供下一步使用。
//The global variable that holds the arrayVariableName to access
static string targetBox = null;
//Implicit conversion operators (box array name to this script(MyScript) instance)
public static implicit operator MyScript(string box)
{
return setTargetAndGetInstance(box);
}
public static MyScript setTargetAndGetInstance(string box)
{
if (instance.boxes.ContainsKey(box))
{
//Set the box requested. This will be needed in the Indexer overloading above
targetBox = box;
return instance;
}
else
return null;
}
3。现在,您可以实现 [index] 语法。这可以通过索引器重载功能来实现。
//Indexer overloading (index to int (value in array))
public int this[int index]
{
get
{
//Get value based on value set in the implicit operators
return accessBox(targetBox, index);
}
}
现在,当你这样做时,"box"+2,它将在 implicit 转换运算符的帮助下返回 this(MyScript) 的实例。然后它将允许您使用索引器重载功能执行 [random int]。
下面的代码是您问题中的语法示例以及执行此操作的其他类似方法。 包括类似的,因为它们看起来比您要求的更好:
用一个简单的功能:
int test1 = accessBox("box" + 2, UnityEngine.Random.Range(0, 3));
Debug.Log(test1);
使用arrayVariableName [index] 语法:
你正在寻找这个,但 MyScript 的转换看起来很糟糕
int test2 = ((MyScript)("box" + 2))[UnityEngine.Random.Range(0, 3)];
Debug.Log(test2);
使用[arrayVariableName][index] 语法:
int test3 = this["box" + 2][UnityEngine.Random.Range(0, 3)];
Debug.Log(test3);
使用[arrayVariableName, index] 语法
int test4 = this["box" + 2, UnityEngine.Random.Range(0, 3)];
Debug.Log(test4);
完整的功能示例:
using System.Collections.Generic;
using UnityEngine;
public class MyScript : MonoBehaviour
{
public int[] box1 = { 1, 0, 2 };
public int[] box2 = { 3, 1, 0 };
public int[] box3 = { 2, 3, 1 };
public Dictionary<string, int[]> boxes = new Dictionary<string, int[]>();
private static MyScript instance;
void Awake()
{
instance = this;
//Add to Dictionary
addBox();
int test1 = accessBox("box" + 2, UnityEngine.Random.Range(0, 3));
Debug.Log(test1);
int test2 = ((MyScript)("box" + 2))[UnityEngine.Random.Range(0, 3)];
Debug.Log(test2);
int test3 = this["box" + 2][UnityEngine.Random.Range(0, 3)];
Debug.Log(test3);
int test4 = this["box" + 2, UnityEngine.Random.Range(0, 3)];
Debug.Log(test4);
}
void addBox()
{
boxes.Add("box1", box1);
boxes.Add("box2", box2);
boxes.Add("box3", box3);
}
public int accessBox(string box, int index)
{
//Return the array from the Dictionary
int[] tempVar;
if (boxes.TryGetValue(box, out tempVar))
{
//Return the spicified index
return tempVar[index];
}
else
{
//ERROR - return -1
return -1;
}
}
//Indexer overloading (index to int (value in array))
public int this[int index]
{
get
{
//Get value based on value set in the implicit operators
return accessBox(targetBox, index);
}
}
static string targetBox = null;
//Implicit conversion operators (box array name to this script(MyScript) instance)
public static implicit operator MyScript(string box)
{
return setTargetAndGetInstance(box);
}
public static MyScript setTargetAndGetInstance(string box)
{
if (instance.boxes.ContainsKey(box))
{
//Set the box requested. This will be needed in the Indexer overloading above
targetBox = box;
return instance;
}
else
return null;
}
//Indexer overloading (box array name to this script(MyScript) instance)
public MyScript this[string box]
{
get
{
return setTargetAndGetInstance(box);
}
}
//Indexer overloading (box array name to int)
public int this[string box, int index]
{
get
{
setTargetAndGetInstance(box);
return accessBox(box, index);
}
}
}