就坏了你真正想要什么做,你会看到它是多么容易。就像我在评论部分提到Dictionary
一样,您仍然需要其他两个C#功能来完成确切的语法。
你希望能够做到这一点:
foo="box"+2[random int];
这是,与此相同:
int valueFromArray = arrayVariableName [index];
在下面的例子中,我们可以假定MyScript
是我们的剧本的名字。
。您需要可以保存名称和int数组名称的字典。
Dictionary<string, int[]> boxes
。你需要一种方法来"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;
}
。现在,你可以实现[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
转换操作符的帮助下返回这个(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);
}
}
}
您正在寻找'Dictionary'。 – Programmer
不是'Dictionary','Lookup' –
不是''Dictionary''或''Lookup'',而是一个带有''indexer''的自定义类 –