Unity輸出PC端(Windows) 拖拽檔案到app中

來源:互聯網
上載者:User

標籤:unit   ugui   分享   The   pos   something   foreach   動態   檔案路徑   

需求:給策劃們寫一個PC端(Window)的Excel導表工具。本來用OpenFile開啟FileExplorerDialog後讓他們自己選擇想要添加的Excel檔案就行了,結果有個需求是希望能拖拽Excel檔案直接添加進來,覺得這樣做更方便。

案頭應用用winform和wpf來寫的話,這個功能應該還挺好做,用Unity來做就比較蛋疼了。。。查了一下多是調用win32的api。

概括一下,就是需要拖過檔案管理工具選檔案,和通過滑鼠拖拽的方式選中檔案。

這個導表工具大致關鍵點是:

1、常規方式匯入Excel檔案。Unity中用UGUI拼好介面,做一個Item模板(可做為prefab預製體),點按鈕開啟FileExplorerDialog,多選Excel檔案後按確定,動態產生Item加入到ScrollView的Content下。要求ScrollView下的Content添加Content Size Fitter和Grid Layout Group組件。這種匯入方式建議用Crosstale的FileBrowser外掛程式(這裡)。

2、Window平台下用拖拽檔案到app視窗中的方式添加檔案。建議使用這個工具(這裡),主要是C#調用win32的api。FileDragAndDrop.cs檔案中有用法範例。稍微加一句MessageBox輸出拖拽進app的檔案路徑:

void OnFiles(List<string> pathList, POINT aPos){    // do something with the dropped file names. aPos will contain the     // mouse position within the window where the files has been dropped.    Debug.Log("Dropped "+pathList.Count+" files at: " + aPos + "\n"+        pathList.Aggregate((a, b) => a + "\n" + b));    StringBuilder sb = new StringBuilder();    sb.Append("拖拽檔案進來了\n\n");    foreach (var path in pathList)    {        sb.Append(path);        sb.Append("了\n\n");    }    MessageBox.Show(sb.ToString());}

測試一下,運行後拖檔案進來,效果如下:

 

Unity輸出PC端(Windows) 拖拽檔案到app中

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.