Skip to content

Commit

Permalink
#136 IPFSImage: Asynced image loading for icons and thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
willneedit committed May 30, 2024
1 parent b5394ce commit f4037ce
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 170 deletions.
140 changes: 21 additions & 119 deletions Assets/Arteranos/Editor/DevelopComponents/IconSelectorBar.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 4360149953330034815}
- component: {fileID: 6226774226023551763}
- component: {fileID: 8400043870960281088}
- component: {fileID: 278987140184397307}
- component: {fileID: 2291261512856647453}
- component: {fileID: 2370883619561989647}
m_Layer: 5
Expand All @@ -31,15 +31,14 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1686006096154621278}
m_Children: []
m_Father: {fileID: 4434522344508843081}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 42}
m_SizeDelta: {x: 0, y: 32}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6226774226023551763
CanvasRenderer:
Expand All @@ -49,7 +48,7 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1441106092429954160}
m_CullTransparentMesh: 1
--- !u!114 &8400043870960281088
--- !u!114 &278987140184397307
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand All @@ -58,27 +57,24 @@ MonoBehaviour:
m_GameObject: {fileID: 1441106092429954160}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Script: {fileID: 11500000, guid: 20b3b024288692a45b171f57ae9311b7, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 2100000, guid: ccc2f5fd25ceee045bb0d53a24e14ce2, type: 2}
m_Color: {r: 0.2509804, g: 0.2509804, b: 0.2509804, a: 0.1254902}
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
m_Texture: {fileID: 2800000, guid: 0d9f045a1d139854e894205778ec2450, type: 3}
m_UVRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
--- !u!114 &2291261512856647453
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -119,7 +115,7 @@ MonoBehaviour:
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 8400043870960281088}
m_TargetGraphic: {fileID: 278987140184397307}
m_OnClick:
m_PersistentCalls:
m_Calls: []
Expand All @@ -136,10 +132,10 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_IgnoreLayout: 0
m_MinWidth: 42
m_MinHeight: 42
m_PreferredWidth: 42
m_PreferredHeight: 42
m_MinWidth: 32
m_MinHeight: 32
m_PreferredWidth: 32
m_PreferredHeight: 32
m_FlexibleWidth: -1
m_FlexibleHeight: -1
m_LayoutPriority: 1
Expand Down Expand Up @@ -353,7 +349,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
txt_IconURL: {fileID: 7605249450523074662}
btn_Icon: {fileID: 2291261512856647453}
img_IconImage: {fileID: 2307096211255850571}
img_IconImage: {fileID: 278987140184397307}
--- !u!114 &7075834260643938011
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -372,7 +368,7 @@ MonoBehaviour:
m_Top: 0
m_Bottom: 0
m_ChildAlignment: 3
m_Spacing: 0
m_Spacing: 5
m_ChildForceExpandWidth: 0
m_ChildForceExpandHeight: 0
m_ChildControlWidth: 1
Expand Down Expand Up @@ -765,97 +761,3 @@ MonoBehaviour:
m_FlexibleWidth: 1
m_FlexibleHeight: -1
m_LayoutPriority: 1
--- !u!1 &8007098539033616312
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1686006096154621278}
- component: {fileID: 4503284250547000586}
- component: {fileID: 298641388979563736}
- component: {fileID: 2307096211255850571}
m_Layer: 5
m_Name: img_IconImage
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1686006096154621278
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8007098539033616312}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 4360149953330034815}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 32, y: 32}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &4503284250547000586
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8007098539033616312}
m_CullTransparentMesh: 1
--- !u!114 &298641388979563736
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8007098539033616312}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 306cc8c2b49d7114eaa3623786fc2126, type: 3}
m_Name:
m_EditorClassIdentifier:
m_IgnoreLayout: 0
m_MinWidth: 32
m_MinHeight: 32
m_PreferredWidth: 32
m_PreferredHeight: 32
m_FlexibleWidth: -1
m_FlexibleHeight: -1
m_LayoutPriority: 1
--- !u!114 &2307096211255850571
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8007098539033616312}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1344c3c82d62a2a41a3576d8abb8e3ea, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Texture: {fileID: 0}
m_UVRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
10 changes: 5 additions & 5 deletions Assets/Arteranos/Prefabs/UI/Panels/ObjectChooser.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 6595510000850821082}
- component: {fileID: 1629495479545284422}
- component: {fileID: 7015261720109464867}
- component: {fileID: 8755293866946712272}
- component: {fileID: 8064136766761328717}
- component: {fileID: 2232532268414904159}
m_Layer: 5
Expand Down Expand Up @@ -48,7 +48,7 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1024997721286812270}
m_CullTransparentMesh: 1
--- !u!114 &7015261720109464867
--- !u!114 &8755293866946712272
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
Expand All @@ -57,10 +57,10 @@ MonoBehaviour:
m_GameObject: {fileID: 1024997721286812270}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1344c3c82d62a2a41a3576d8abb8e3ea, type: 3}
m_Script: {fileID: 11500000, guid: 20b3b024288692a45b171f57ae9311b7, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 2100000, guid: ccc2f5fd25ceee045bb0d53a24e14ce2, type: 2}
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
Expand Down Expand Up @@ -115,7 +115,7 @@ MonoBehaviour:
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 7015261720109464867}
m_TargetGraphic: {fileID: 8755293866946712272}
m_OnClick:
m_PersistentCalls:
m_Calls: []
Expand Down
3 changes: 2 additions & 1 deletion Assets/Arteranos/Scripts/UI/Arteranos.UI.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:e992fb8119031314d9e6982eec5e7002",
"GUID:4c8cb9593dbbc3548a812336c8647bd3",
"GUID:12a71712e319a02479dd56e4d0f76c08"
"GUID:12a71712e319a02479dd56e4d0f76c08",
"GUID:e89de4e5a0e0c0b47a8506136f6d550d"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
88 changes: 88 additions & 0 deletions Assets/Arteranos/Scripts/UI/Components/IPFSImage.cs
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());
}
}
}
11 changes: 11 additions & 0 deletions Assets/Arteranos/Scripts/UI/Components/IPFSImage.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f4037ce

Please sign in to comment.