簡體   English   中英

如何在數組C#中添加精靈?

[英]How to add sprites in array C#?

我想在我的數組中的代碼中訪問游戲對象(精靈) ,但是我不知道該怎么做,因為我是C#的新手。

這是我的測試代碼,但沒有任何值:

void Start()
{

    Sprite[] countries = Resources.LoadAll<Sprite>("MAPS");
    sr = GetComponent<SpriteRenderer>();
    names = new string[countries.Length];

    for (int i = 0; i < names.Length; i++)
    {
        names[i] = countries[i].name;
        //0 = red
        //1 = green
        if (UnityEngine.Random.Range(0, 2) == 0)
        {
            this.GetComponent<SpriteRenderer>().color = Color.red;
        }
        else
        {
            this.GetComponent<SpriteRenderer>().color = Color.green;
        }
    }
}

而不使用this關鍵字:

if (UnityEngine.Random.Range(0, 2) == 0)
{
    this.GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
    this.GetComponent<SpriteRenderer>().color = Color.green;
}

嘗試使用國家[i]:

if (UnityEngine.Random.Range(0, 2) == 0)
{
    countries[i].GetComponent<SpriteRenderer>().color = Color.red;
}
else
{
    countries[i].GetComponent<SpriteRenderer>().color = Color.green;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM