简体   繁体   English

从 Web 服务器 C# Unity 接收图像

[英]Receive image from web server C# Unity

I have had web server that stores the images.我有存储图像的网络服务器。 In Unity, I can receive one and create gameobject to change it's material.在 Unity 中,我可以接收一个并创建游戏对象来更改它的材质。 However, I want to receive max no.但是,我想收到最大数量。 four images.四个图像。 After 1 mins, I want to receive max no. 1 分钟后,我想收到最大数量。 four images again.再来四张图。 Besides, if there is two images in server, I just want to create two new gameobject and change them material.此外,如果服务器中有两个图像,我只想创建两个新的游戏对象并更改它们的材质。 If there is three, just create three.如果有三个,只需创建三个。 How can I do that, Any one can help me?我该怎么做,有人可以帮助我吗? Here is my code in Unity:这是我在 Unity 中的代码:

void Start () {
    StartCoroutine (LoadImage ());
}

IEnumerator LoadImage(){

    filename = "image" + k.ToString () + ".png";
    url = "https://wwwfoodparadisehk.000webhostapp.com/" + filename;
    WWW www = new WWW (url);
    yield return www;

    if (www.error != null) {
        Debug.Log (www.error);
    } else {


        Debug.Log (k);


        path = "Assets/MyMaterial" + k.ToString () + ".mat";

        k = k + 1;

        material = new Material (Shader.Find ("Sprites/Default"));
        AssetDatabase.CreateAsset (material, path);

        Debug.Log (AssetDatabase.GetAssetPath (material));

        material.mainTexture = www.texture;
        GameObject newPaperInstance = Instantiate (newpaper) as GameObject;
        newPaperInstance.transform.Find ("Plane001").gameObject.GetComponent<Renderer> ().material = material;


    }



}

I'd first ask my server for a list of the items I can get.我会首先向我的服务器询问我可以获得的物品清单。 For this, you can simply make a text file or make your own PHP file to create a list that you separate by a character like the pipe (|):为此,您可以简单地制作一个文本文件或制作您自己的 PHP 文件来创建一个列表,您可以使用管道 (|) 之类的字符将其分隔:

MyMaterial1|MyMaterial2|MyMaterial3

Then you can ask the file from your server the same way you'd get the images and create a string[] array object from the result.然后,您可以像获取图像并从结果中创建 string[] 数组对象一样,从您的服务器询问文件。 You can use Split('|') in order to create this array from your result string.您可以使用 Split('|') 从结果字符串创建此数组。

When you're done, you can foreach over the items within the array.完成后,您可以对数组中的项目进行 foreach。

IEnumerator LoadImages()
{
  string filename = "imagelist.txt";
  string url = "https://wwwfoodparadisehk.000webhostapp.com/" + filename;
  WWW www = new WWW (url);
  yield return www;

  if (www.error != null) 
  {
    Debug.Log (www.error);
  } 
  else 
  {
    string[] images = www.text.Split ('|');
    foreach (var image in images) 
    {
      LoadImage (image);
    }
  }
}

Last but not least, you'll have to create a second function that loads the texture from the string you supply:最后但并非最不重要的是,您必须创建第二个函数,从您提供的字符串加载纹理:

IEnumerator LoadImage(string image)
{
  string url = "https://wwwfoodparadisehk.000webhostapp.com/" + image;
  WWW www = new WWW (url);
  yield return www;

  if (www.error != null) 
  {
    Debug.Log (www.error);
  } 
  else 
  {
    // turn your image into a texture with www.texture and apply it to your objects.
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM