CanvasSampleOpenFileTextMultiple.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Text;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.EventSystems;
  8. using SFB;
  9. [RequireComponent(typeof(Button))]
  10. public class CanvasSampleOpenFileTextMultiple : MonoBehaviour, IPointerDownHandler {
  11. public Text output;
  12. #if UNITY_WEBGL && !UNITY_EDITOR
  13. //
  14. // WebGL
  15. //
  16. [DllImport("__Internal")]
  17. private static extern void UploadFile(string gameObjectName, string methodName, string filter, bool multiple);
  18. public void OnPointerDown(PointerEventData eventData) {
  19. UploadFile(gameObject.name, "OnFileUpload", ".txt", true);
  20. }
  21. // Called from browser
  22. public void OnFileUpload(string urls) {
  23. StartCoroutine(OutputRoutine(urls.Split(',')));
  24. }
  25. #else
  26. //
  27. // Standalone platforms & editor
  28. //
  29. public void OnPointerDown(PointerEventData eventData) { }
  30. void Start() {
  31. var button = GetComponent<Button>();
  32. button.onClick.AddListener(OnClick);
  33. }
  34. private void OnClick() {
  35. // var paths = StandaloneFileBrowser.OpenFilePanel("Title", "", "txt", true);
  36. var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", "", true);
  37. if (paths.Length > 0) {
  38. var urlArr = new List<string>(paths.Length);
  39. for (int i = 0; i < paths.Length; i++) {
  40. urlArr.Add(new System.Uri(paths[i]).AbsoluteUri);
  41. }
  42. StartCoroutine(OutputRoutine(urlArr.ToArray()));
  43. }
  44. }
  45. #endif
  46. private IEnumerator OutputRoutine(string[] urlArr) {
  47. var outputText = "";
  48. for (int i = 0; i < urlArr.Length; i++) {
  49. var loader = new WWW(urlArr[i]);
  50. yield return loader;
  51. outputText += loader.text;
  52. }
  53. output.text = outputText;
  54. }
  55. }