• 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();
            }
        }

  • 相关阅读:
    图解集合5:不正确地使用HashMap引发死循环及元素丢失
    图解集合4:HashMap
    图解集合3:CopyOnWriteArrayList
    图解集合2:LinkedList
    SharePoint PowerShell 修改母版页
    SharePoint PowerShell 启动工作流
    SharePoint REST 服务获取讨论版问题
    SharePoint 前端开发常用的对象之_spPageContextInfo
    SharePoint 读取内容的插件之SharepointPlus
    SharePoint 配置PowerShell任务计划
  • 原文地址:https://www.cnblogs.com/2Yous/p/5079991.html
Copyright © 2020-2023  润新知