【问题标题】:Texture Array getting the last index only ( C# UNITY) [duplicate]纹理数组仅获取最后一个索引(C#UNITY)[重复]
【发布时间】:2018-05-07 06:27:34
【问题描述】:

我正在创建一个纹理数组,所以我正在这样做

[SerializeField] GameObject[] uitex = new GameObject[4];

void GetTextureFromServer()
{
    string dealer_image = "";
    var tex = new Texture2D(20, 20);

    for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    {
        dealer_image += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
        dealer_image += ",";

    }
    string[] NewLinks = dealer_image.Split(',');


    for(int j = 0; j < NewLinks.Length - 1; j++)
    {
        Debug.Log("HERE ARE THE LINKS : " + NewLinks[j]);

        new BestHTTP.HTTPRequest(new System.Uri("***********.amazonaws.com/resources" + "/dealer/pic/" + NewLinks[j]),
            (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res) =>
            {
                tex.LoadImage(res.Data);
            }).Send();
    }

    for (int i = 0; i < uitex.Length; i++)
    {
        uitex[i].GetComponent<UITexture>().mainTexture = tex;
    }
}

到目前为止我尝试的是这个

uitex[j].GetComponent<UITexture>().mainTexture = tex;

但它给了我一个数组超出范围,我不知道为什么。

这段代码的问题是它总是得到我拥有的最后一个索引,所以所有 4 个游戏对象的所有纹理都相同,这不是我想要的。有人可以帮我解决我的问题。谢谢。

【问题讨论】:

  • 我认为您混淆了数组大小。 new GameObject[4] 是 5 个元素。数组从 0 开始。而且 uitex[j].GetComponent&lt;UITexture&gt;().mainTexture = tex; 绝对不可能,因为 j 不在范围内。
  • 您之前(几个小时前)问过这个问题,我将其标记为重复。您将其删除并再次询问,然后提供了重复的答案....为什么?

标签: c# android unity3d


【解决方案1】:

在第二个 for 循环中,将纹理加载到 tex 变量中。但是,您不会对它做任何事情,因此当 for 循环结束时,tex 变量会保存您在第三个 for 循环中设置的最后加载的纹理。我不确定你的 tzPlayInfo.Instance.bc_gametablelist 类型是什么,但我建议重写:

for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
{
    dealer_image += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;

    Debug.Log("HERE ARE THE LINKS : " + dealer_image);

    new BestHTTP.HTTPRequest(new System.Uri("***********.amazonaws.com/resources" + "/dealer/pic/" + dealer_image),
        (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res) =>
        {
            tex.LoadImage(res.Data);
            uitex[i].GetComponent<UITexture>().mainTexture = tex;
        }).Send();
}

这可能有错误,因为我无法真正测试它,但我希望你能明白。

【讨论】:

  • 仍然把这个数组从索引中弄出来了,先生
【解决方案2】:
for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    {
        dealer_img += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
        dealer_img += ",";
    }
    string[] newLinks = dealer_img.Split(',');

    for (int i = 0; i < newLinks.Length - 1; i++)
    {
        var index = i;  // We need to make a local copy because C# captures variables by reference to lambdas.
        new BestHTTP.HTTPRequest(new System.Uri("***************.amazonaws.com/resources/"
            + "dealer/pic/" + newLinks[index]),
            (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
            =>
            {
                var tex = new Texture2D(20, 20);
                tex.LoadImage(res.Data);
                uitex[index].GetComponent<UITexture>().mainTexture = tex;
            }).Send();
    }

想出了这个

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 2016-02-16
    • 1970-01-01
    • 2014-05-09
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    相关资源
    最近更新 更多