Skip to content

Commit

Permalink
WebGL Support
Browse files Browse the repository at this point in the history
  • Loading branch information
rarafy committed Jun 15, 2022
1 parent 7695dbe commit c749074
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
64 changes: 52 additions & 12 deletions _Project/Assets/Unimgpicker/Samples/AddImage.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using Kakera;
using Kakera;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class AddImage : MonoBehaviour
// WebGLで使うときはIPointerDownHandlerを継承する必要がある点に注意
public class AddImage : MonoBehaviour, IPointerDownHandler
{
[SerializeField] private Unimgpicker imagePicker;
[SerializeField] private Image ssImage;
public Texture2D texture;
public Sprite texture2;

private void Awake()
{
imagePicker.Completed += path => StartCoroutine(LoadImage(path, ssImage));
}

private void Start()
{
if (!ssImage)
Expand All @@ -24,9 +22,15 @@ private void Start()
}
}

#if !UNITY_WEBGL
private void Awake()
{
imagePicker.Completed += path => StartCoroutine(LoadImage(path, ssImage));
}

public void OnPressShowPicker()
{
imagePicker.Show("Select Image", "unimgpicker", 512);//1024→512に変更
imagePicker.Show("Select Image", "unimgpicker", 512);//1024→512に変更
}

private IEnumerator LoadImage(string path, Image output)
Expand All @@ -36,22 +40,58 @@ private IEnumerator LoadImage(string path, Image output)
yield return www;

texture = www.texture;
// まずリサイズ
// まずリサイズ
int _CompressRate = TextureCompressionRate.TextureCompressionRatio(texture.width, texture.height);
TextureScale.Bilinear(texture, texture.width / _CompressRate, texture.height / _CompressRate);
// 次に圧縮(縦長・横長すぎると使えない場合があるようです。) -> https://forum.unity.com/threads/strange-error-message-miplevel-m_mipcount.441907/
// 次に圧縮(縦長・横長すぎると使えない場合があるようです。) -> https://forum.unity.com/threads/strange-error-message-miplevel-m_mipcount.441907/
//texture.Compress(false);
// Spriteに変換して使用する
// Spriteに変換して使用する
texture2 = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
output.overrideSprite = texture2;
}

// WebGLを使わない場合はこの関数(OnPointerDown)は不要
public void OnPointerDown(PointerEventData eventData) { }

#elif UNITY_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, .PNG, .jpg, .jpeg", false);
}

// Called from browser
public void OnFileUpload(string url)
{
StartCoroutine(OutputRoutine(url));
}


private IEnumerator OutputRoutine(string url)
{
var www = new WWW(url);
yield return www;

texture = www.texture;
// まずリサイズ
int _CompressRate = TextureCompressionRate.TextureCompressionRatio(texture.width, texture.height);
TextureScale.Bilinear(texture, texture.width / _CompressRate, texture.height / _CompressRate);
// 次に圧縮(縦長・横長すぎると使えない場合があるようです。) -> https://forum.unity.com/threads/strange-error-message-miplevel-m_mipcount.441907/
//texture.Compress(false);
// Spriteに変換して使用する
texture2 = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
ssImage.overrideSprite = texture2;
}
#endif
}


public static class TextureCompressionRate
{
/// <summary>
/// Textureが500x500に収まるようにリサイズします
/// Textureが500x500に収まるようにリサイズします
/// </summary>
public static int TextureCompressionRatio(int width, int height)
{
Expand Down
2 changes: 1 addition & 1 deletion _Project/Assets/Unimgpicker/Scripts/Unimgpicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Unimgpicker : MonoBehaviour
#elif UNITY_ANDROID
new PickerAndroid();
#elif UNITY_STANDALONE
new PickerStandalone();
new PickerStandalone();
#else
new PickerUnsupported();
#endif
Expand Down
Binary file added doc/unimgpicker_webgl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified unimgpicker-plus.unitypackage
Binary file not shown.

0 comments on commit c749074

Please sign in to comment.