Search Unity

Texture Array getting the last index only ( C# UNITY)

Discussion in 'Scripting' started by Ginxx009, May 7, 2018.

  1. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Here's my code

    Code (CSharp):
    1.  [SerializeField] GameObject[] uitex = new GameObject[3];
    2. void GetTextureFromServer()
    3. {
    4.      string dealer_img = "";
    5.      for (int i = 0; i < PlayInfo.Instance.gametablelist.Count; i++)
    6.      {
    7.          dealer_img += PlayInfo.Instance.gametablelist[i].dlrimage;
    8.          dealer_img += ",";
    9.      }
    10.      Debug.Log("HERE ARE THE LINKS : " + dealer_img);
    11.      string[] links = dealer_img.Split(',');
    12.      for (int j = 0; j < links.Length - 1; j++)
    13.      {
    14.          Debug.Log("HERE ARE THE NEW LINKS : " + links[j]);
    15.          new BestHTTP.HTTPRequest(new System.Uri("*************************amazonaws.com/resources/" + "dealer/pic/" + links[j]),
    16.          (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    17.          =>
    18.          {
    19.              for (int k = 0; k < uitex.Length; k++) {
    20.                  var tex = new Texture2D(20, 20);
    21.                  tex.LoadImage(res.Data);
    22.                  uitex[k].GetComponent<UITexture>().mainTexture = tex;
    23.              }
    24.          }).Send();
    25.      }
    26. }
    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 . Can someone please help me.
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    First, why using dealer_img and split and not adding the links to an array directly at line 7?
    I am not sure, from my view it should provide you all links except the last one according to "j < links.Length - 1".
    Please explain the problem in more detail, what happens exactly? Is Line 14 called only once?
    If not then I assume that something with your Network request is wrong, I have no much understanding of it but is it wise to call multiple Send() in a loop, don't you have to wait for the resut first? Just a guess.
     
  3. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I tried another way so i'm doing it like this right now .

    Code (CSharp):
    1. for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    2.         {
    3.             dealer_img += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
    4.  
    5.             new BestHTTP.HTTPRequest(new System.Uri("*****************.amazonaws.com/resources/" + "dealer/pic/" + dealer_img),
    6.             (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    7.             =>
    8.             {
    9.                 var tex = new Texture2D(20, 20);
    10.                 tex.LoadImage(res.Data);
    11.                 uitex[i].GetComponent<UITexture>().mainTexture = tex;
    12.             }).Send();
    13.         }
    But the problem is that it gives me an Array index is out of range. StackTrace in this line
    uitex[i].GetComponent<UITexture>().mainTexture = tex


    I tried debugging my i value and it has 4
     
  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    Right, sounds like boxing. Try that using a temporary variable instead of i.
    The reason is that the delegate is called after the for loop has finished and at that time i has become 4. You basically call the variable, not it's value once the HTTP answered.
    By storing the value in another variable we can fix this.
    Code (CSharp):
    1. for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    2.         {
    3.             dealer_img += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
    4. var n=i;
    5.             new BestHTTP.HTTPRequest(new System.Uri("https://samgyeopsalgood.s3.ap-southeast-1.amazonaws.com/resources/" + "dealer/pic/" + dealer_img),
    6.             (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    7.             =>
    8.             {
    9.                 var tex = new Texture2D(20, 20);
    10.                 tex.LoadImage(res.Data);
    11.                 uitex[n].GetComponent<UITexture>().mainTexture = tex;
    12.             }).Send();
    13.         }
     
  5. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    sir could you please edit it and put ***** on the system.uri please thanks
     
  6. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Now it can only download 1 image sir and can display successfully. the other 3 is i cannot get them .
     
  7. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
  8. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    This works for me but the problem this code is not optimize sir .


    Code (CSharp):
    1. for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    2.         {
    3.             dealer_img += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
    4.             dealer_img += ",";
    5.         }
    6.         string[] newLinks = dealer_img.Split(',');
    7.  
    8.         new BestHTTP.HTTPRequest(new System.Uri("***************************.amazonaws.com/resources/"
    9.             + "dealer/pic/" + newLinks[0]),
    10.             (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    11.             =>
    12.             {
    13.                 var tex = new Texture2D(20, 20);
    14.                 tex.LoadImage(res.Data);
    15.                 uitex[0].GetComponent<UITexture>().mainTexture = tex;
    16.             }).Send();
    17.  
    18.         new BestHTTP.HTTPRequest(new System.Uri("***************************.amazonaws.com/resources/"
    19.             + "dealer/pic/" + newLinks[1]),
    20.             (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    21.             =>
    22.             {
    23.                 var tex = new Texture2D(20, 20);
    24.                 tex.LoadImage(res.Data);
    25.                 uitex[1].GetComponent<UITexture>().mainTexture = tex;
    26.             }).Send();
    27.  
    28.         new BestHTTP.HTTPRequest(new System.Uri("***************************.amazonaws.com/resources/"
    29.             + "dealer/pic/" + newLinks[2]),
    30.             (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    31.             =>
    32.             {
    33.                 var tex = new Texture2D(20, 20);
    34.                 tex.LoadImage(res.Data);
    35.                 uitex[2].GetComponent<UITexture>().mainTexture = tex;
    36.             }).Send();
    37.  
    38.         new BestHTTP.HTTPRequest(new System.Uri("***************************.amazonaws.com/resources/"
    39.             + "dealer/pic/" + newLinks[3]),
    40.             (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
    41.             =>
    42.             {
    43.                 var tex = new Texture2D(20, 20);
    44.                 tex.LoadImage(res.Data);
    45.                 uitex[3].GetComponent<UITexture>().mainTexture = tex;
    46.             }).Send();