Unity保存本地图⽚到unity⾥⾯,并使⽤unity打开本地⽂件代码如下
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
///<summary>
///数据接收类
///</summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileOpenDialog{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
克己public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOfft = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;什么是root
public String templateName = null;
public IntPtr rervedPtr = IntPtr.Zero;
public int rervedInt = 0;
public int flagsEx = 0;
}
//系统调⽤函数
public class DialogShow
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out]FileOpenDialog dialog); //这个⽅法名称必须为GetOpenFileName
}
public class OpenFileByWin32 : MonoBehaviour {
//打开⽂件
public void OpenFile()
{
FileOpenDialog dialog = new FileOpenDialog();
dialog.structSize = Marshal.SizeOf(dialog);
// dialog.filter = "exe files\0*.exe\0All Files\0*.*\0\0";
dialog.filter = "图⽚⽂件(*.png*.jpg)\0*.png;*.jpg";
dialog.file = new string(new char[256]);
dialog.maxFile = dialog.file.Length;
dialog.fileTitle = new string(new char[64]);
dialog.maxFileTitle = dialog.fileTitle.Length;
dialog.initialDir = UnityEngine.Application.dataPath; //默认路径
dialog.title = "Open File Dialog";
dialog.defExt = "png";//显⽰⽂件的类型士明
//注意⼀下项⽬不⼀定要全选但是0x00000008项不要缺少
dialog.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (DialogShow.GetOpenFileName(dialog))
{
if (dialog.file!=null)
{
Structure_RenZhi.instacnce.Chuandong(dialog.file);//dialog.file本地图⽚的地址
}
el
{
return;
}
}
}
}
下⾯是把获取到图⽚地址的⽂件流转换成Image
public Image image;
public string url;
//把路径图⽚转换成 Image
public void Chuandong(string path)
{
image.sprite = ChangeToSprite(ByteToTex2d(byteToImage(path)));
url = path;
print(url);
}
//根据图⽚路径返回图⽚的字节流byte[]
public static byte[] byteToImage(string path)
羊年
{
FileStream files = new FileStream(path, FileMode.Open);
byte[] imgByte = new byte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Clo();
return imgByte;
}
//根据字节流转换成图⽚
public static Texture2D ByteToTex2d(byte[] bytes)
{
int w = 500;
int h = 500;
Texture2D tex = new Texture2D(w, h);
tex.LoadImage(bytes);
return tex;
}
//转换为Image
private Sprite ChangeToSprite(Texture2D tex)
{
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
return sprite;
}
张湘然后在把获取到的图⽚路径转换成⽂件流在保存成图⽚
// 开启协程就可以保存成图⽚了
IEnumerator DownSprite()
{
yield return new WaitForSeconds(0);
Texture2D tex = ByteToTex2d(byteToImage(url));
//保存本地
Byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.streamingAstsPath + "/DataerTion/"+ +".png", bytes); // 是保存图⽚的名字# if UNITY_EDITOR张艺谋活着
AstDataba.Refresh(); //刷新Editor 不刷新显⽰不出来
#endif
}
下⾯是使⽤保存的图⽚显⽰在ui上⾯怎么使⽤具体看需要可以拉⼏个UI(如下图)试⼀下看看效果already的用法
下⾯上代码
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
///<summary>
///切换图⽚显⽰, 读取unity本地图⽚⽂件显⽰在ui
///</summary>
public class Cut_Photo : MonoBehaviour
{
public Dictionary<int, Sprite> spriteDic = new Dictionary<int, Sprite>();广式蜜汁叉烧
public Button button_zuo;
public Button button_you;
public Image image;
private int aa=0;
// Start is called before the first frame update
void Start()
{
GetTexture();
// image = ansform.GetComponent<Image>();
image.sprite =spriteDic[aa];
Debug.Log(spriteDic.Count);
Click.AddListener(delegate {
if (aa !=0)
{
aa--;
}
el
{
aa = (spriteDic.Count - 1);
}
image.sprite = spriteDic[aa];
});
Click.AddListener(delegate {
if (aa != (spriteDic.Count-1))
{
aa++;
}
el
{
aa = 0;
}
image.sprite = spriteDic[aa];
});
}
//获取⽂件⾥⾯的图⽚
public void GetTexture()
{
DirectoryInfo dir = new DirectoryInfo(Application.streamingAstsPath + "/DataerTion");
FileInfo[] filess = dir.GetFiles("*.png");//获取所有⽂件的信息
int i = 0;
foreach (FileInfo file in filess)
{
FileStream fs = new FileStream(Application.streamingAstsPath + "/DataerTion/" + file.Name, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Clo();
Texture2D tex = new Texture2D(2, 2);
tex.LoadImage(buffer);
tex.Apply();
spriteDic.Add(i, ChangeToSprite(tex));
i++;
}
}
//转换为Image
private Sprite ChangeToSprite(Texture2D tex)
{
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
return sprite;
}
// Update is called once per frame
void Update()
{
}
}
//指定⼀张图⽚显⽰在UI上⾯
public void GetTexTure(string spriptname,Image sprit )
{
string picpath = Application.streamingAstsPath + "/DataerTion/" + spriptname.ToString() + ".png";
if (File.Exists(picpath))
{
byte[] bt = File.ReadAllBytes(picpath);
Texture2D t2d = new Texture2D(1920, 1080);
t2d.LoadImage(bt);
Sprite sprite = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), );
sprit.sprite = sprite;
}
}
//指定多张图⽚的
public void GetTexTure1(string spriptname)
{
for (int i = 0; i < 10; i++)
{
string picpath = Application.streamingAstsPath + "/DataerTion/" + spriptname.ToString()+i+ ".png";
if (File.Exists(picpath))
{
byte[] bt = File.ReadAllBytes(picpath);
Texture2D t2d = new Texture2D(1920, 1080);
t2d.LoadImage(bt);
Sprite sprite = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), );
//sprit.sprite = sprite;
spriteDic.Add(i, sprite);
}
}
}
本次就这么多,谢谢⼤家,有需要会继续补充