UNITY3D中调用winform文件选择框选择路径和文件名方法
2023/3/31 点击:
UNITY3D中设置文件选择框的范例:
using OpenWinForm = System.Windows.Forms;
在unity3d中,使用FileDialog应该把System.Windows.Forms.dll拷贝到unity工程的plugins目录,
并且把Player Setting中Other Settings下的api compatibility Level改为.NET2.0。要不无法编译通过。
//比如unity3d要让用户选择某个音乐文件播放;
private void SelectMusic(){
OpenWinForm.OpenFileDialog op = new OpenWinForm.OpenFileDialog();
op.Title = "音乐";
op.Filter = "音频文件(*.wiseglove;*.ogg)|*.wiseglove;*.ogg";
if (op.ShowDialog() == OpenWinForm.DialogResult.OK || op.ShowDialog() == OpenWinForm.DialogResult.Yes)
{
string selectName = op.FileName;
customBgmPath.text = selectName;
string path = customBgmPath.text;
WWW www = new WWW("file://"+path);
if(www.audioClip){
AudioClip clip = www.audioClip;
AudioPlayCtr.instance.ChangeBgMusic(clip);
}
}
else {
return;
}
}
//自定义文件保存文件夹;
private void SaveCutScreenPath()
{
OpenWinForm.FolderBrowserDialog fb = new OpenWinForm.FolderBrowserDialog();
fb.ShowNewFolderButton = true;
fb.RootFolder = Environment.SpecialFolder.MyDocuments;
fb.SelectedPath = "C:";
fb.Description = "请选择截图保存目录";
fb.RootFolder = Environment.SpecialFolder.MyDocuments;
if (fb.ShowDialog() == OpenWinForm.DialogResult.OK || fb.ShowDialog() == OpenWinForm.DialogResult.Yes)
{
string selectName = fb.SelectedPath;
customCutScreenPath.text = selectName;
}
else {
return;
}
}
//比如unity3d截图后,弹出对话框用户选择保存路径;
public IEnumerator CutScreent() {
int width = Screen.width;
int height = Screen.height;
yield return new WaitForEndOfFrame();
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);//设置Texture2D
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);//获取Pixels
tex.Apply();//应用改变
byte[] bytes = tex.EncodeToPNG();//转换为byte[]
Destroy(tex);
OpenWinForm.SaveFileDialog saveFileDialog = new OpenWinForm.SaveFileDialog();
saveFileDialog.Filter = "图像文件(*.png)|*.png";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == OpenWinForm.DialogResult.OK)
{
string fName = saveFileDialog.FileName;
Stream flstr = new FileStream(fName, FileMode.Create);//文件操作
BinaryWriter sw = new BinaryWriter(flstr, System.Text.Encoding.Unicode);
sw.Write(bytes);
sw.Close();
flstr.Close();
}
}
不忙啊, 调用winform还有个坑, 得注册组件才不会出现 oop, could not register... 等等文字
/* 注册winform对话框*/
#region winform
private System.Threading.Thread CloseOops;
private bool ThreadRunning = true;
private void OopsWindowsThreadStart()
{
CloseOops = new System.Threading.Thread(ClearOopsWindows) { IsBackground = true };
ThreadRunning = true;
CloseOops.Start();
}
private void ClearOopsWindows()
{
while (ThreadRunning)
{
FindAndCloseWindow();
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public static void FindAndCloseWindow()
{
IntPtr lHwnd = FindWindow(null, "Oops");
if (lHwnd != IntPtr.Zero)
{
SendMessage(lHwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
#endregion
记得在脚本程序的 start()函数里调用下 OopsWindowsThreadStart(), 到此较好呈现winform对话框。
- 上一篇:UNITY3D中调用winform发布出现问题补充 2023/4/2
- 下一篇:unity3d版本不兼容,出现的Visual C# Comp 2023/2/26
