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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM