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