title: Unity WebGl. Load textures from Java server
body: <p>I have two project, a Java+Spring server and a unity project written in C#.
  The unity project will be export into WebGl(it is not a game).
  Now here is my problem:
  I must load textures from the Java server.</p>
  
  <blockquote>
    <p>How can i solve this problem?</p>
  </blockquote>
  
  <p>My current solution, <strong><em>greatly simplified</em></strong> (not work in browser):
  Server:</p>
  
  <pre><code>@SpringBootApplication
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>@RestController
  public class MyController {
  
      @Autowired
      private Loader2 loader2;
  
      @RequestMapping("/skull2")
      public ByteObj skull2(@RequestParam(value = "name", defaultValue = "World") String name) {
  
          return loader2.getImages("Skull");
      }
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.tomcat.util.codec.binary.Base64;
  import org.springframework.stereotype.Controller;
  
  @Controller
  public class Loader2 {
  
      String mainPath = "Path to images folder"; // in the future i will load many textures;
  
      public List&lt;String&gt; getFileNameInPath(String foldername) {
  
          List&lt;String&gt; result = new ArrayList&lt;String&gt;();
  
          File folder = new File(mainPath + foldername);
          File[] listOfFiles = folder.listFiles();
  
          for (int i = 0; i &lt; listOfFiles.length; i++) {
              if (listOfFiles[i].isFile()) {
                  System.out.println("file " + listOfFiles[i].getName());
                  result.add(listOfFiles[i].getName());
              } else if (listOfFiles[i].isDirectory()) {
                  System.out.println("Directory " + listOfFiles[i].getName());
              }
          }
          return result;
      }
  
      public ByteObj getImages(String folder) {
          List&lt;String&gt; images = getFileNameInPath(folder);
  
          for (String string : images) {
              try {
                  File file = new File(mainPath + folder + "//" + string);
                  FileInputStream imageInFile = new FileInputStream(file);
                  byte imageData[] = new byte[(int) file.length()];
                  imageInFile.read(imageData);
  
                  // Converting Image byte array into Base64 String
                  String imageDataString = encodeImage(imageData);
  
                  imageInFile.close();
  
                  return new ByteObj(imageDataString);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          return null;
      }
  
      public  String encodeImage(byte[] imageByteArray) {
          return Base64.encodeBase64String(imageByteArray);
      }
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>import java.io.Serializable;
  public class ByteObj implements Serializable {
  
      public String obj;
  
      public ByteObj(String obj) {
          this.obj = obj;
      }
  
      public ByteObj() {
  
      }
  }
  </code></pre>
  
  <p>Client(load from server and decode)
  The Unity stage contains a 3D cube and a camera(with script) </p>
  
  <pre><code> //   C#
  
  public class ByteObj {
  
      public  string obj;
  
      public ByteObj(string obj) {
          this.obj = obj;
      }
  
      public ByteObj() {
  
      }
  
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  using System;
  using System.Text;
  
  
  public class MyTestClass: MonoBehaviour {
  
      public GameObject cube;
      public float speed = 10f;
  
      IEnumerator Start() {
          yield return StartCoroutine( UploadImages() );
  
      }
  
      void Update () {
          cube.transform.Rotate(Vector3.up, speed * Time.deltaTime);
      }
  
      IEnumerator UploadImages()
      {
          string aux = "";
          WWW www = new WWW("http://localhost:8080/skull2");
          yield return www;
  
          aux = www.text;
          if (www.text == null)
          {
              Debug.Log("Images not found");
          }
          else {
              ByteObj list = JsonUtility.FromJson&lt;ByteObj&gt;(aux);
              var myObject = list.obj;
              //instruction.text = list.obj;
              string s = myObject.Trim().Replace(" ", "+");
              if (s.Length % 4 &gt; 0)
                  s = s.PadRight(s.Length + 4 - s.Length % 4, '=');
              byte[] b64_bytes = System.Convert.FromBase64String(myObject);
  
              var decodedBytes = Convert.FromBase64String(myObject);
              var tex = new Texture2D(2, 2);
              tex.LoadImage(decodedBytes);
  
              cube.GetComponent&lt;Renderer&gt;().material.mainTexture = tex;
          }
      }
  }
  </code></pre>
  
  <p>It works as a Desktop app, but after export to WebGl I see only the cube(without textures)</p>
  
  <p>Thank you for your help</p>
  
  <p>PS. Sorry for my english</p>