123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class Textureload : MonoBehaviour {
- public string url = "https://bchy.oss-cn-qingdao.aliyuncs.com/Work/5_2_0.png";
- public GameObject go;
- string path;
- void Start()
- {
- PhotoButtonClick();
- }
- /// <summary>
- /// 拍照按钮的功能实现
- /// </summary>
- private void PhotoButtonClick()
- {
- string fName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";//用时间作为图片的名称,保证唯一性
- PlayerPrefs.SetString("photoName", fName);//将名字存储,为了方便获取
- StartCoroutine(UploadPNG("5_2_0.png", "Work"));
- }
- /// <summary>
- /// 下载图片
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- private IEnumerator UploadPNG(string fileName,string dic)
- {
- WWW www = new WWW(url);
- yield return www;
- byte[] bytes = www.texture.EncodeToPNG();
- path = PathForFile(fileName, dic);//移动平台的判断
-
- print("文件" + path);
-
- SaveNativeFile(bytes, path);//保存图片到本地
- }
- /// <summary>
- /// 判断平台
- /// </summary>
- /// <param name="filename"></param>
- /// <returns></returns>
- public string PathForFile(string filename,string dic)
- {
- if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- string path = Application.persistentDataPath.Substring(0, Application.persistentDataPath.Length - 5);
- path = path.Substring(0, path.LastIndexOf('/'));
- path = Path.Combine(path, "Documents");
- path = Path.Combine(path, dic);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- return Path.Combine(path, filename);
- }
- else if (Application.platform == RuntimePlatform.Android)
- {
- string path = Application.persistentDataPath;
- path = path.Substring(0, path.LastIndexOf('/'));
- path = Path.Combine(path, dic);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- return Path.Combine(path, filename);
- }
- else
- {
- string path = Application.dataPath;
- path = path.Substring(0, path.LastIndexOf('/'));
- path = Path.Combine(path, dic);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- return Path.Combine(path, filename);
- }
- }
- /// <summary>
- /// 在本地保存文件
- /// </summary>
- /// <param name="bytes"></param>
- /// <param name="path"></param>
- public void SaveNativeFile(byte[] bytes, string path)
- {
- FileStream fs = new FileStream(path, FileMode.Create);
- fs.Write(bytes, 0, bytes.Length);
- fs.Flush();
- fs.Close();
- }
- void OnGUI()
- {
- if (GUI.Button(new Rect(200, 200, 120, 30), "Click"))
- {
- ShowNativeTexture();
- }
- }
- //上述是保存在移动平台的代码,下面则是读取到移动平台的图片
- /// <summary>
- /// 显示图片
- /// </summary>
- public void ShowNativeTexture()
- {
- go.GetComponent<MeshRenderer>().material.mainTexture = GetNativeFile(path);
- }
- /// <summary>
- /// 获取到本地的图片
- /// </summary>
- /// <param name="path"></param>
- public Texture2D GetNativeFile(string path)
- {
- try
- {
- var pathName = path;
- var bytes = ReadFile(pathName);
- int width = Screen.width;
- int height = Screen.height;
- var texture = new Texture2D(width, height);
- texture.LoadImage(bytes);
- return texture;
- }
- catch (Exception c)
- {
- }
- return null;
- }
- public byte[] ReadFile(string filePath)
- {
- var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- fs.Seek(0, SeekOrigin.Begin);
- var binary = new byte[fs.Length];
- fs.Read(binary, 0, binary.Length);
- fs.Close();
- return binary;
- }
- }
- //Global.SetPhotoTexture("Work" ,idex, "https://bchy.oss-cn-qingdao.aliyuncs.com", (myt) => {
- // _cartoonObj.transform.Find("Icon").GetComponent<UITexture>().mainTexture = myt;
- // _cartoonObj.transform.Find("PlayerDesc_Text").GetComponent<UILabel>().text = this.LoadWorkImageDesc(go.name);
- // _cartoonObj.transform.Find("PlayerName_Text").GetComponent<UILabel>().text = Global.MainPlayer.GetPlayerName() + this.LoadWorkImageTitle(go.name);
- // this.transform.Find("CartoonPanel/Remove").gameObject.SetActive(true);
- // this.transform.Find("CartoonPanel/SavaPhoto").gameObject.SetActive(true);
- // this.transform.Find("CartoonPanel/Lottry").gameObject.SetActive(true);
- // if (_cartoonObj != null)
- // _cartoonObj.gameObject.SetActive(true);
- //});
|