当前位置:   article > 正文

Unity 加载图片_unity在电脑端加载图片

unity在电脑端加载图片

Unity 加载图片

1.服务器加载图片

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.Networking;
using System.IO;

public class UploadImage : MonoBehaviour
{
    public Image img;

    public void UpLoad()
    {
        StartCoroutine(UpLoadTexture());
    }

    IEnumerator UpLoadTexture()
    {
        yield return new WaitForSeconds(0.5f);
        byte[] bytes = File.ReadAllBytes(Application.dataPath + "/StreamingAssets/" + "photo.png");
        WWWForm form = new WWWForm();
        //string id = "Photo_" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss");
        //form.AddField("id", id);//文件名这个参数是给服务器处理的,服务器不同,参数数量和类型也将不同
        form.AddBinaryData("file", bytes, "photo.png");
        using (UnityWebRequest www = UnityWebRequest.Post("XXX", form))
        {
            yield return www.SendWebRequest();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log("上传失败:" + www.error);
            }
            else
            {
                string text = www.downloadHandler.text;
                Debug.Log("服务器返回值" + text);//正确打印服务器返回值
                Debug.Log("上传成功!");
                StartCoroutine(LoadTexture(text, img));
            }
        }
    }
    

    Texture2D texture2D;
    IEnumerator LoadTexture(string url, Image image)
    {
        print(url);
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url))
        {
            yield return uwr.SendWebRequest();

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log("图片加载失败" + uwr.error);
            }
            else
            {
                // Get downloaded asset bundle
                texture2D = DownloadHandlerTexture.GetContent(uwr);
                print("图片获取成功!");
                Sprite temp = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
                image.sprite = temp;
            }
        }
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

2.加载文件夹路径中的图片资源

/// <summary>
    /// StreamingAssets加载图片资源
    /// </summary>
    /// <param name="path">文件夹路径</param>
    /// <returns></returns>
    public Texture2D GetTexture(string path)
    {
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes, 0, (int)fileStream.Length);
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;
        int width = 300;
        int height = 372;
        Texture2D texture = new Texture2D(width, height);
        texture.LoadImage(bytes);
        //Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        //image.sprite = sprite;
        return texture;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

将Texture2d转换为Sprite

private Sprite TextureToSprite(Texture2D tex)
    {
        Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
        return sprite;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/112595
推荐阅读
相关标签
  

闽ICP备14008679号