2

我有两个项目,一个 Java+Spring 服务器和一个用 C# 编写的统一项目。统一项目将导出到 WebGl(它不是游戏)。现在这是我的问题:我必须从 Java 服务器加载纹理。

我怎么解决这个问题?

我当前的解决方案,大大简化(不适用于浏览器): 服务器:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

-

@RestController
public class MyController {

    @Autowired
    private Loader2 loader2;

    @RequestMapping("/skull2")
    public ByteObj skull2(@RequestParam(value = "name", defaultValue = "World") String name) {

        return loader2.getImages("Skull");
    }
}

-

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<String> getFileNameInPath(String foldername) {

        List<String> result = new ArrayList<String>();

        File folder = new File(mainPath + foldername);
        File[] listOfFiles = folder.listFiles();

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

-

import java.io.Serializable;
public class ByteObj implements Serializable {

    public String obj;

    public ByteObj(String obj) {
        this.obj = obj;
    }

    public ByteObj() {

    }
}

客户端(从服务器加载并解码) Unity 阶段包含一个 3D 立方体和一个摄像头(带有脚本)

 //   C#

public class ByteObj {

    public  string obj;

    public ByteObj(string obj) {
        this.obj = obj;
    }

    public ByteObj() {

    }

}

-

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<ByteObj>(aux);
            var myObject = list.obj;
            //instruction.text = list.obj;
            string s = myObject.Trim().Replace(" ", "+");
            if (s.Length % 4 > 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<Renderer>().material.mainTexture = tex;
        }
    }
}

它作为桌面应用程序工作,但在导出到 WebGl 后,我只看到立方体(没有纹理)

谢谢您的帮助

PS。对不起我的英语不好

4

0 回答 0