上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

70 lines
2.2 KiB

using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using SFB;
using UnityEngine.Events;
using UniRx;
using System;
using UnityEngine.Networking;
[RequireComponent(typeof(Button))]
public class AnnotationOpenImage : MonoBehaviour, IPointerDownHandler
{
public GameObject Output;
public UnityAction<Texture2D> OnLoadTextureFinished;
#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", ".png, .jpg,.bmp", false);
}
// Called from browser
public void OnFileUpload(string url) {
StartCoroutine(OutputRoutine(url));
}
#else
public void OnPointerDown(PointerEventData eventData) { }
// Start is called before the first frame update
void Start()
{
GetComponent<Button>().OnClickAsObservable()
.Subscribe(_ => OnClick());
}
private void OnClick()
{
var extensions = new[] {
new ExtensionFilter("Image Files", "png", "jpg", "jpeg" ),
};
var paths = StandaloneFileBrowser.OpenFilePanel("Title", "", extensions, false);
if (paths.Length > 0)
{
StartCoroutine(OutputRoutine(new Uri(paths[0]).AbsoluteUri));
}
}
#endif
private IEnumerator OutputRoutine(string url)
{
UnityWebRequest uwr = new UnityWebRequest(url);
DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
uwr.downloadHandler = downloadTexture;
yield return uwr.SendWebRequest();
if (!(uwr.isNetworkError || uwr.isHttpError))
{
var loader = downloadTexture.texture;
Output.GetComponent<Renderer>().material.SetTexture("_MainTex", loader);
Output.transform.localScale = new Vector3(20, 1, (float)loader.height * ((float)20 / (float)loader.width));
OnLoadTextureFinished?.Invoke(loader);
}
}
}