当前位置:   article > 正文

Unity笔记之本地加载图片_unity 读取本地图片

unity 读取本地图片

本文参考自:https://www.jianshu.com/p/78372e4f1143
在原有的基础上稍微做了一些改动,本文仅作为个人笔记。

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

public class LoadPictrueManager : MonoBehaviour
{
    public Material ma;
    public Image iss;
    int nums;

    string url_Begin = @"C:\Users\zhang\Desktop\";
    string url_End = @".jpg";
    int number = 100;

    string url1;
    private GameObject[] objs;
    private List<Material[]> materialss = new List<Material[]>();

    private Texture2D img = null;
    void Start()
    {
        objs = new GameObject[transform.childCount];
        for (int i = 0; i < objs.Length; i++)
        {
            objs[i] = transform.GetChild(i).gameObject;
        }

        for (int i = 0; i < objs.Length; i++)
        {
            materialss.Add(objs[i].GetComponent<MeshRenderer>().materials);
        }

        StartCoroutine(IOCeShi());
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            nums++;
            StartCoroutine(IOCeShi());
        }
    }
    /// <summary>
    /// www的方式
    /// </summary>
    /// <returns></returns>
    IEnumerator CeShi()
    {
        yield return new WaitForSeconds(0.5f);
        WWW www = new WWW(url1);
        yield return www;
        img = www.texture;
        Sprite sprite = Sprite.Create(img, new Rect(0, 0, img.width, img.height), new Vector2(0.5f, 0.5f));
        nums++;
        iss.sprite = sprite;//图片 用于UI
        ma.mainTexture = img;//材质球 用于plane等 ma是定义的一个材质球
    }
    /// <summary>
    /// 读取并赋予材质(IO流的方式)
    /// </summary>
    /// <returns></returns>
    IEnumerator IOCeShi()
    {
        yield return new WaitForSeconds(0.5f);
        for (int i = 0; i < objs.Length; i++, number++)
        {
            url1 = url_Begin + number.ToString() + url_End;
            print(url1);
            FileStream fileStream = new FileStream(url1, FileMode.Open, FileAccess.Read);
            byte[] by = new byte[fileStream.Length];
            fileStream.Read(by, 0, (int)fileStream.Length);
            fileStream.Close();//关闭流
            fileStream.Dispose();//释放流
            fileStream = null;
            img = new Texture2D(1080, 1920);
            img.LoadImage(by);
            objs[i].GetComponent<MeshRenderer>().material.mainTexture = img;
        }
        StopCoroutine(IOCeShi());
    }
}


  • 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
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85

代码已附上,听说IO的方式要比www快一点也不知道是不是真的,各位自己去测试吧。如有不对的地方欢迎指正!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/112583
推荐阅读
相关标签
  

闽ICP备14008679号