0
\$\begingroup\$

Here's my code:

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

void GetTextureFromServer()
{
    string dealer_img = "";

    for (int i = 0; i < PlayInfo.Instance.gametablelist.Count; i++)
    {
        dealer_img += PlayInfo.Instance.gametablelist[i].dlrimage;
        dealer_img += ",";
    }
    Debug.Log("HERE ARE THE LINKS : " + dealer_img);

    string[] links = dealer_img.Split(',');

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

        new BestHTTP.HTTPRequest(new System.Uri("*************************amazonaws.com/resources/" + "dealer/pic/" + links[j]),
        (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
        =>
        {
            for (int k = 0; k < uitex.Length; k++) { 
                var tex = new Texture2D(20, 20);
                tex.LoadImage(res.Data);
                uitex[k].GetComponent<UITexture>().mainTexture = tex;
            }
        }).Send();
    }
}

The problem with this code is that it's only getting the last index of my links. What could be the problem on my iteration?

Here's an approach that works that shows what the expected behavior is:

 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(',');

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

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

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

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

It's working perfectly, but now the problem is that it is not very clean code.

\$\endgroup\$
2
  • \$\begingroup\$ What's the expected behaviour? Currently you're iterating over all links, and once a download finishes, you're assigning the loaded image to all the UITextures, so obviously just one the content in link becomes visible. Are you for an example try to assign the image in link at index j to the texture at index j? Something else? \$\endgroup\$ – Tyyppi_77 May 7 '18 at 10:21
  • \$\begingroup\$ edited it sir.. \$\endgroup\$ – TheGinxx009 May 7 '18 at 10:37
1
\$\begingroup\$

Since you want to assign a single link to a single texture, just capture the index into a lambda, kinda like you were doing.

for (int i = 0; i < newLinks.Length; 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();}
}

So we take the link at the index, and assign the downloaded image into the texture at the same index. The additional index variable is required, because C# captures by reference. See here.

\$\endgroup\$
2
  • \$\begingroup\$ its strange sir . It's getting all the textures but the problem is that it has 1 error Array index is out of range. StackTrace on line 307 which is uitex[index].GetComponent<UITexture>().mainTexture = tex; \$\endgroup\$ – TheGinxx009 May 7 '18 at 10:56
  • \$\begingroup\$ Most likely you have a different number of textures and links. \$\endgroup\$ – Tyyppi_77 May 7 '18 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.