Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于多维数组(C#UNITY)的值实例化预置_C#_Unity3d - Fatal编程技术网

基于多维数组(C#UNITY)的值实例化预置

基于多维数组(C#UNITY)的值实例化预置,c#,unity3d,C#,Unity3d,大家好,我只是想问一下,是否有可能根据多维数组的值实例化prefab,例如,我有这个数据 102011 00 21 10 00 00 00 00 00 00 00 00 00 00 00 00 这是我的代码 string road1 = ""; for (int y = 0; y < bsb.ArrBigRoad.GetLength(0); y++) { for (int x = 0; x < bsb.ArrBigRoad.GetL

大家好,我只是想问一下,是否有可能根据多维数组的值实例化prefab,例如,我有这个数据

102011

00 21 10

00 00 00

00 00 00

00 00 00

00 00 00

这是我的代码

string road1 = "";
    for (int y = 0; y < bsb.ArrBigRoad.GetLength(0); y++)
    {
        for (int x = 0; x < bsb.ArrBigRoad.GetLength(1); x++)
        {
            road1 += string.Format("{0:D2}", bsb.ArrBigRoad[y, x] / 100);
            road1 += ".";
        }
        road1 += "\n";
    }
Debug.Log(road1);

我建议您采用两种不同的方式:

1.创建一个开关/枚举,使用您可以实例化的可能预设,例如:

//Possible Prefabs, link them on editor
public GameObject car;
public GameObject road;
public GameObject truck;

//Used to instantiate the new object
public GameObject newObject;

switch(road1){
    case 'car':
        newObject = Instantiate(car);
        break;
    case 'road':
        newObject = Instantiate(road);
        break;
    case 'truck':
        newObject = Instantiate(truck);
        break;
    default:
        print("error: prefab not exists");
        break;
}
2.使用资源文件夹

//Used to instantiate the new object
public GameObject newObject;

newObject = (GameObject)Instantiate(Resources.Load(road1));

请记住,对于案例2,您需要创建一个名为Resources的文件夹,并在该文件夹上找到预制件。

谢谢Lotan,我所做的就是这样

[SerializeField] public UILabel info_scores_bigroad;

info_scores_bigroad.text = road1;
SetScore.cs

public void Set( int score )
{
    int who = score / 1000;

    if (who == 1)
    {
        NGUITools.SetActive(obj_player, true );
        NGUITools.SetActive(obj_banker, false);
    }
    else if( who == 2)
    {
        NGUITools.SetActive(obj_player, false);
        NGUITools.SetActive(obj_banker, true);
    }
    else
    {
        NGUITools.SetActive(obj_player, false);
        NGUITools.SetActive(obj_banker, false);

        NGUITools.SetActive(lbl_tie_no.gameObject, false);
        NGUITools.SetActive(spr_playerPair.gameObject, false);
        NGUITools.SetActive(spr_bankerPair.gameObject, false);
        return;
    }
}
public IEnumerator ShowScoreBoard_BigRoad(int[,]  arrBigRoad)
{
    NGUITools.DestroyChildren(pos_bigroad);

    for (int y = 0; y < arrBigRoad.GetLength(0); y++)
    {
        for (int x = 0; x < arrBigRoad.GetLength(1); x++)
        {
            int score = arrBigRoad[y, x];

            GameObject o = Instantiate(prefab_bigroad) as GameObject;
            o.transform.SetParent(pos_bigroad);
            o.transform.localScale = Vector3.one;

            o.transform.localPosition = new Vector3(x * SX_, y* SY_, 0);
            NGUITools.SetActive(o, true);

            // 1011, 2000, 3000, 

            bsbBigRoad s = o.GetComponent<bsbBigRoad>();
            s.Set(score);
        }
    }

    yield break;
}
然后在我的2d阵列上画出这样的图案

[SerializeField] public UILabel info_scores_bigroad;

info_scores_bigroad.text = road1;
Game.cs

public void Set( int score )
{
    int who = score / 1000;

    if (who == 1)
    {
        NGUITools.SetActive(obj_player, true );
        NGUITools.SetActive(obj_banker, false);
    }
    else if( who == 2)
    {
        NGUITools.SetActive(obj_player, false);
        NGUITools.SetActive(obj_banker, true);
    }
    else
    {
        NGUITools.SetActive(obj_player, false);
        NGUITools.SetActive(obj_banker, false);

        NGUITools.SetActive(lbl_tie_no.gameObject, false);
        NGUITools.SetActive(spr_playerPair.gameObject, false);
        NGUITools.SetActive(spr_bankerPair.gameObject, false);
        return;
    }
}
public IEnumerator ShowScoreBoard_BigRoad(int[,]  arrBigRoad)
{
    NGUITools.DestroyChildren(pos_bigroad);

    for (int y = 0; y < arrBigRoad.GetLength(0); y++)
    {
        for (int x = 0; x < arrBigRoad.GetLength(1); x++)
        {
            int score = arrBigRoad[y, x];

            GameObject o = Instantiate(prefab_bigroad) as GameObject;
            o.transform.SetParent(pos_bigroad);
            o.transform.localScale = Vector3.one;

            o.transform.localPosition = new Vector3(x * SX_, y* SY_, 0);
            NGUITools.SetActive(o, true);

            // 1011, 2000, 3000, 

            bsbBigRoad s = o.GetComponent<bsbBigRoad>();
            s.Set(score);
        }
    }

    yield break;
}
public IEnumerator展示记分牌_BigRoad(int[,]arrBigRoad)
{
NGUITools.儿童(pos_bigroad);
for(int y=0;y

不过还是要谢谢你

所以你的问题是“我可以用它的名字来实例化一个预置吗”?或者我错过了什么?因为你正在用多维数组构建一个字符串,我知道你想这样做:实例化(road1),不是吗?@Lotan是的,这就是我想要的。@Ginxxx如果有效,请标记答案,以便其他人看到它被解决了。如果没有,告诉我你得到了什么:)是的,我会:)别担心。