Skip to content

Commit

Permalink
Texture loading completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Auros committed Aug 23, 2022
1 parent 4a4a313 commit 86a05c3
Show file tree
Hide file tree
Showing 7 changed files with 308 additions and 14 deletions.
6 changes: 6 additions & 0 deletions SmartImage/Assets/Development/TestLoading.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

namespace SmartImage.Develop
{
Expand All @@ -11,12 +12,17 @@ public class TestLoading : MonoBehaviour
[SerializeField]
private string _source = string.Empty;

[SerializeField]
private Image _image;

private async UniTaskVoid Start()
{
await UniTask.SwitchToThreadPool();
var smartTexture = await _smartImageManager.LoadAsync(_source);
await UniTask.SwitchToMainThread();
print(smartTexture);

_image.sprite = Sprite.Create(smartTexture.Active, new Rect(0, 0, smartTexture.Active.width, smartTexture.Active.height), Vector2.zero, 100);
}
}
}
261 changes: 261 additions & 0 deletions SmartImage/Assets/Scenes/Development.unity
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,107 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &413598971
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 413598975}
- component: {fileID: 413598974}
- component: {fileID: 413598973}
- component: {fileID: 413598972}
m_Layer: 5
m_Name: Canvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &413598972
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 413598971}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dc42784cf147c0c48a680349fa168899, type: 3}
m_Name:
m_EditorClassIdentifier:
m_IgnoreReversedGraphics: 1
m_BlockingObjects: 0
m_BlockingMask:
serializedVersion: 2
m_Bits: 4294967295
--- !u!114 &413598973
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 413598971}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 0
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &413598974
Canvas:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 413598971}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 0
m_Camera: {fileID: 0}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_AdditionalShaderChannelsFlag: 0
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
--- !u!224 &413598975
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 413598971}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 1660173792}
m_Father: {fileID: 0}
m_RootOrder: 4
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: 0}
m_Pivot: {x: 0, y: 0}
--- !u!1 &1003123730
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -169,6 +270,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
_smartImageManager: {fileID: 2079983290}
_source: C:\Users\Auros\Desktop\bob.gif
_image: {fileID: 1660173793}
--- !u!1 &1267384749
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -347,6 +449,150 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1480463090
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1480463093}
- component: {fileID: 1480463092}
- component: {fileID: 1480463091}
m_Layer: 0
m_Name: EventSystem
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1480463091
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1480463090}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3}
m_Name:
m_EditorClassIdentifier:
m_SendPointerHoverToParent: 1
m_HorizontalAxis: Horizontal
m_VerticalAxis: Vertical
m_SubmitButton: Submit
m_CancelButton: Cancel
m_InputActionsPerSecond: 10
m_RepeatDelay: 0.5
m_ForceModuleActive: 0
--- !u!114 &1480463092
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1480463090}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 76c392e42b5098c458856cdf6ecaaaa1, type: 3}
m_Name:
m_EditorClassIdentifier:
m_FirstSelected: {fileID: 0}
m_sendNavigationEvents: 1
m_DragThreshold: 10
--- !u!4 &1480463093
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1480463090}
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: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1660173791
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1660173792}
- component: {fileID: 1660173794}
- component: {fileID: 1660173793}
m_Layer: 5
m_Name: Image
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1660173792
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1660173791}
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: 413598975}
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: -684.5, y: -335}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1660173793
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1660173791}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, 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_Sprite: {fileID: 0}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &1660173794
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1660173791}
m_CullTransparentMesh: 1
--- !u!1 &2079983289
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -357,6 +603,7 @@ GameObject:
m_Component:
- component: {fileID: 2079983291}
- component: {fileID: 2079983290}
- component: {fileID: 2079983293}
- component: {fileID: 2079983292}
m_Layer: 0
m_Name: Smart Image Manager
Expand All @@ -377,7 +624,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b6fed1c9e27d41149ab0e9776b42998c, type: 3}
m_Name:
m_EditorClassIdentifier:
_incrementalLimit: 5
_sources:
- {fileID: 2079983293}
- {fileID: 2079983292}
--- !u!4 &2079983291
Transform:
Expand Down Expand Up @@ -406,3 +655,15 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 46d89563878fd4a44b51d36032971826, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &2079983293
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2079983289}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9857f8b1ae67470e906ca38a95bf28ba, type: 3}
m_Name:
m_EditorClassIdentifier:
12 changes: 9 additions & 3 deletions SmartImage/Assets/SmartImage/Runtime/SmartImageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SmartImageManager : MonoBehaviour
private readonly Stopwatch _stopwatch = new();
private readonly Dictionary<int, SmartTexture> _textures = new();
private readonly List<SmartTextureConstruction> _texturesToBuild = new();

[PublicAPI]
public UniTask<SmartTexture?> LoadAsync(string source) => LoadAsync(source, null, default);

Expand Down Expand Up @@ -137,14 +137,18 @@ private void Update()
item.Cursor++;
var frame = item.Frames[i];

if (frame.TryBuild)
if (!frame.TryBuild)
continue;

triedToBuildAnything = true;
frame.TryBuild = false;

Texture2D tex = new(frame.Width, frame.Height);
tex.SetPixels32(frame.Pixels);

for (int c = 0; c < frame.Size; c++)
item.Copier[c] = frame.Pixels[c];

tex.SetPixels32(item.Copier);
tex.wrapMode = TextureWrapMode.Clamp;
tex.Apply();

Expand Down Expand Up @@ -194,6 +198,7 @@ private class SmartTextureConstruction
public ImageLoadingOptions Options { get; }
public List<ImageFrameConstructionInfo> Frames { get; }

public Color32[] Copier { get; }
public int Cursor { get; set; }

public SmartTextureConstruction(SmartTexture texture, List<ImageFrameConstructionInfo> frames, ImageLoadingOptions options, CancellationToken token)
Expand All @@ -203,6 +208,7 @@ public SmartTextureConstruction(SmartTexture texture, List<ImageFrameConstructio
Options = options;
Texture = texture;

Copier = new Color32[frames[0].Width * frames[0].Height];
texture.Frames = new SmartFrame<Texture2D>[frames.Count];
}

Expand Down
Loading

0 comments on commit 86a05c3

Please sign in to comment.