-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#136 IPFSImage: Asynced image loading for icons and thumbnails
- Loading branch information
1 parent
b5394ce
commit f4037ce
Showing
8 changed files
with
143 additions
and
170 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright (c) 2023, willneedit | ||
* | ||
* Licensed by the Mozilla Public License 2.0, | ||
* residing in the LICENSE.md file in the project's root directory. | ||
*/ | ||
|
||
|
||
using System.Collections; | ||
using UnityEngine.UI; | ||
|
||
using Arteranos.Core; | ||
using Arteranos.Services; | ||
using UnityEngine; | ||
using Ipfs.Unity; | ||
using System.Threading; | ||
|
||
namespace Arteranos.UI | ||
{ | ||
[AddComponentMenu("UI/IPFS Image", 13)] | ||
public class IPFSImage : RawImage | ||
{ | ||
public string Path | ||
{ | ||
get => m_Path; | ||
set | ||
{ | ||
m_Path = value; | ||
RestartLoader(); | ||
} | ||
} | ||
|
||
public byte[] ImageData | ||
{ | ||
get => m_ImageData; | ||
set | ||
{ | ||
m_Path = null; | ||
m_ImageData = value; | ||
RestartLoader(); | ||
} | ||
} | ||
|
||
private string m_Path = null; | ||
private byte[] m_ImageData = null; | ||
|
||
|
||
protected override void OnEnable() | ||
{ | ||
base.OnEnable(); | ||
|
||
RestartLoader(); | ||
} | ||
|
||
private void RestartLoader() | ||
{ | ||
IEnumerator LoaderCoroutine() | ||
{ | ||
yield return null; | ||
|
||
if(m_Path != null) | ||
{ | ||
using CancellationTokenSource cts = new(4000); | ||
|
||
yield return Asyncs.Async2Coroutine( | ||
IPFSService.ReadBinary(m_Path, cancel: cts.Token), | ||
_data => m_ImageData = _data); | ||
} | ||
|
||
bool result = false; | ||
Texture2D tex = new(2, 2); | ||
|
||
if (m_ImageData != null) | ||
{ | ||
yield return Asyncs.Async2Coroutine( | ||
AsyncImageLoader.LoadImageAsync(tex, m_ImageData), | ||
_r => result = _r); | ||
} | ||
|
||
texture = result ? tex : BP.I.Unknown_Icon; | ||
} | ||
|
||
StopAllCoroutines(); | ||
|
||
StartCoroutine(LoaderCoroutine()); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.