就壞了你真正想要什麼做,你會看到它是多麼容易。就像我在評論部分提到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''的自定義類 –