UNITY3D中調(diào)用winform文件選擇框選擇路徑和文件名方法
2023/3/31??????點(diǎn)擊:
UNITY3D中設(shè)置文件選擇框的范例:
using OpenWinForm = System.Windows.Forms;
在unity3d中,使用FileDialog應(yīng)該把System.Windows.Forms.dll拷貝到unity工程的plugins目錄,
并且把Player Setting中Other Settings下的api compatibility Level改為.NET2.0。要不無法編譯通過。
//比如unity3d要讓用戶選擇某個(gè)音樂文件播放;
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);//設(shè)置Texture2D
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);//獲取Pixels
tex.Apply();//應(yīng)用改變
byte[] bytes = tex.EncodeToPNG();//轉(zhuǎn)換為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();
}
}
不忙啊, 調(diào)用winform還有個(gè)坑, 得注冊組件才不會出現(xiàn) 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()函數(shù)里調(diào)用下 OopsWindowsThreadStart(), 到此較好呈現(xiàn)winform對話框。
- 上一篇:UNITY3D中調(diào)用winform發(fā)布出現(xiàn)問題補(bǔ)充 2023/4/2
- 下一篇:unity3d版本不兼容,出現(xiàn)的Visual C# Comp 2023/2/26
