zoukankan      html  css  js  c++  java
  • C# 文件选择对话框,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 = "音频文件(*.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();
            }
        }

  • 相关阅读:
    [Leetcode] Regular Expression Matching
    [Leetcode] Edit Distance
    计算机科学论文写作3-表、图、例子和其他类似的元素的使用
    计算机科学论文写作2-搜寻、阅读和引用文献
    灰度图与彩图的双边滤波
    opencv6.1-imgproc图像处理模块之平滑与形态学操作
    opencv5-objdetect之级联分类器
    opencv4-highgui之视频的输入和输出以及滚动条
    计算机科学论文写作1-引言
    lecture11-hopfiled网络与玻尔兹曼机
  • 原文地址:https://www.cnblogs.com/2Yous/p/5079991.html
Copyright © 2011-2022 走看看