Last active
February 25, 2023 13:20
-
-
Save mtytheone/b2873718f210f1921da015e377f217a5 to your computer and use it in GitHub Desktop.
画像ダウンロードサンプルコード(ブログ用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UdonSharp; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using VRC.SDKBase; | |
using VRC.Udon; | |
using VRC.SDK3.Image; | |
using VRC.SDK3.Components; | |
public class ImageDownload : UdonSharpBehaviour | |
{ | |
// ダウンロードしたテクスチャの反映先 | |
[SerializeField] private RawImage _target; | |
// URLを入力するInputField | |
[SerializeField] private VRCUrlInputField _urlInputfield; | |
// ロードバー | |
[SerializeField] private Slider _loadingBar; | |
// 画像をダウンロードしてくれるクラス | |
private VRCImageDownloader _imageDownloader; | |
// ダウンロード実行タスク | |
private IVRCImageDownload _task; | |
private void Start() | |
{ | |
// クラス作成 | |
_imageDownloader = new VRCImageDownloader(); | |
} | |
private void Update() | |
{ | |
// ロードバーを動かす | |
UpdateLoadingInfo(); | |
} | |
private void OnDestroy() | |
{ | |
// ちゃんと破棄する | |
_imageDownloader.Dispose(); | |
} | |
// インタラクトしたら、ダウンロード開始 | |
public override void Interact() | |
{ | |
base.Interact(); | |
StartDownLoad(); | |
} | |
// ダウンロード成功時 | |
public override void OnImageLoadSuccess(IVRCImageDownload result) | |
{ | |
base.OnImageLoadSuccess(result); | |
// ロード状況を可視化するために必要だった情報は全てリセット | |
ResetLoadingInfo(); | |
// テクスチャ反映 | |
ApplyTexture(result.Result); | |
} | |
// ダウンロード失敗時 | |
public override void OnImageLoadError(IVRCImageDownload result) | |
{ | |
base.OnImageLoadError(result); | |
// ロード状況を可視化するために必要だった情報は全てリセット | |
ResetLoadingInfo(); | |
} | |
// ダウンロードを開始 | |
private void StartDownLoad() | |
{ | |
// ダウンロードするテクスチャの設定 | |
TextureInfo info = GetTextureInfo(); | |
// 自身にダウンロード後、イベントを発生させる | |
UdonBehaviour targetUdon = this.GetComponent<UdonBehaviour>(); | |
// ダウンロード実行 | |
_task = _imageDownloader.DownloadImage(_urlInputfield.GetUrl(), null, targetUdon, info); | |
} | |
// ダウンロードしたテクスチャを割り当て | |
private void ApplyTexture(Texture2D texture) | |
{ | |
if (_target == null) return; | |
// テクスチャ反映 | |
_target.texture = texture; | |
// サイズ変更 | |
_target.rectTransform.sizeDelta = new Vector2(_target.texture.width, _target.texture.height); | |
} | |
// テクスチャの設定情報を取得 | |
private TextureInfo GetTextureInfo() | |
{ | |
// Mipmapを指定 | |
TextureInfo info = new TextureInfo(); | |
//info.MaterialProperty = "_MainTex"; | |
info.GenerateMipMaps = true; | |
return info; | |
} | |
// ロードの可視状況を更新 | |
private void UpdateLoadingInfo() | |
{ | |
// ダウンロード実行中以外は、反映処理をさせない | |
if (_task == null || _loadingBar == null) return; | |
_loadingBar.value = _task.Progress; | |
} | |
// ロード状況をリセット | |
private void ResetLoadingInfo() | |
{ | |
if (_loadingBar == null) return; | |
_task = null; | |
_loadingBar.value = 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment