【Unity】PC读取本地图片文件

首先,配置一个打开文件日志类,代码如下:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileDlg
{
    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 fileOffset = 0;
    public short fileExtension = 0;
    public String defExt = null;
    public IntPtr custData = IntPtr.Zero;
    public IntPtr hook = IntPtr.Zero;
    public String templateName = null;
    public IntPtr reservedPtr = IntPtr.Zero;
    public int reservedInt = 0;
    public int flagsEx = 0;
}

public class OpenFileDialog
{
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileDlg ofd);
    public static bool GetOpenFileName1([In, Out] OpenFileDlg ofd)
    {
        return GetOpenFileName(ofd);
    }
}

然后,我这里是使用了一个静态管理器来管理加载图片

public class OpenFileMgr :MonoBehaviour
{
    private static OpenFileMgr instance;
    public static OpenFileMgr GetInstance()
    {
        if (instance==null)
        {
            GameObject obj = new GameObject("OpenFileMgr");
            
            instance = obj.AddComponent<OpenFileMgr>();
            return instance;
        }
        return instance;

    }

    #region 打开文件夹
    /// <summary>
    /// 打开资源管理器界面
    /// </summary>
    public void Openfile()
    {

        OpenFileDlg pth = new OpenFileDlg();
        pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);
       
        //加载图片文件
        pth.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
         
        pth.file = new string(new char[256]);
        pth.maxFile = pth.file.Length;
        pth.fileTitle = new string(new char[64]);
        pth.maxFileTitle = pth.fileTitle.Length;
        pth.initialDir = Application.streamingAssetsPath.Replace('/', '\\');  // default path
        pth.title = "选择项目json";
        pth.defExt = "JPG";//显示文件类型
        pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (OpenFileDialog.GetOpenFileName(pth))
        {
            string filepath = pth.file;//选择的文件路径;

            DirectoryInfo i = new DirectoryInfo(filepath);

            //上级目录
            string path = i.Parent.FullName;//返回文件的上级目录
            Debug.Log(path);//文件目录父目录
            Debug.Log(i.FullName);//文件路径全
            try
            {
            StartCoroutine("LoadIamge", i.FullName);
                
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
    #endregion

    //显示加载的图片
    public Image image;
    /// <summary>
    /// 加载本地图片
    /// </summary>
    /// <param name="path">本地文件路径</param>
    /// <returns></returns>
    IEnumerator LoadIamge(string path)
    {   //计算加载用时    
        double startTime = (double)Time.time;


        WWW www = new WWW("file:///" + path);
        yield return www;
        if (www != null && string.IsNullOrEmpty(www.error))
        {
            //获取Texture
            Texture2D texture = www.texture;
            //更多操作...
            //直接将选择图保存
            byte[] bytes = texture.EncodeToJPG();
            //测试地址
            //string filename = @"G:\wenshuxin\BackGround\6.jpg";
            System.IO.File.WriteAllBytes(path, bytes);
            //根据获取的Texture创建一个sprite
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            //将sprite显示在图片上
            //image.sprite = sprite;
            //图片设置为原始尺寸
            //image.SetNativeSize();
            image.sprite= sprite;

            //计算加载用时
            startTime = (double)Time.time - startTime;
            Debug.Log("WWW加载用时:" + startTime);
        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_44263579/article/details/123913469
今日推荐