赞
踩
本文参考自: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()); } }
代码已附上,听说IO的方式要比www快一点也不知道是不是真的,各位自己去测试吧。如有不对的地方欢迎指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。