您的位置:首页 > 移动开发 > Unity3D

unity 打开文件对话框功能及非Editor模式下全屏播放打开文件对话框后程序转后台问题的处理

2018-03-08 16:48 621 查看
(适用系统Windows)unity5.4及以前的版本没有自己的文件打开存储功能,如果需要操作本地文件,只能通过调用系统功能进行文件操作,以下是详细代码
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
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 LocalDialog
{
//链接指定系统函数       打开文件对话框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool GetOFN([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}

//链接指定系统函数        另存为对话框
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
public static bool GetSFN([In,Out] OpenFileName ofn)
{
return GetSaveFileName(ofn);
}
}
沟通的桥梁搭建好了,下面就是方法的使用了OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = @"视频文件|*.avi,*.AVI,*.mp4,*.MP4,*.mov,*.MOV,*.asf,*.ASF,*.wmv,*.WMV,*.mp3,*.MP3,*.wav,*.WAV";
openFileName.file = new string(new char[256]);
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = new string(new char[64]);
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.initialDir = defaultfilepath;
openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;

if (LocalDialog.GetOpenFileName(openFileName))
{
defaultfilepath = openFileName.file;
} 因为我做的是一个全屏的播放器功能,当选取本地的视频文件时,运行程序就会跳到后台,
4000
严重影响了用户体验,最后找了个折中的办法,当选取本地文件时跳出全屏改为窗口模式,播放或进行其它操作时再进入全屏模式,两句代码的事,Screen.SetResolution(1920, 1080, true)和Screen.SetResolution(1920, 1080, false)。虽然不能完美解决,至少能让用户体验提升一点,如果有大神有更好的方法请多多指教,感谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐