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 = "音频文件(*.wav;*.ogg)|*.wav;*.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();
}
}