Skip to content

Commit b4c82b6

Browse files
committed
implement asset pools
1 parent e99eabc commit b4c82b6

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

Runtime/Core/ReactContext.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ public virtual ScriptSource CreateStaticScript(string path)
168168
return src;
169169
}
170170

171+
public bool TryGetAssetFromPool(string path, out object res)
172+
{
173+
if(string.IsNullOrEmpty(path)) {
174+
res = null;
175+
return false;
176+
}
177+
178+
var paths = path.Split("/", 2);
179+
Globals.TryGetValue(paths[0], out var poolObj);
180+
var pool = poolObj as IAssetPool;
181+
182+
if (pool == null)
183+
{
184+
res = null;
185+
return false;
186+
}
187+
188+
return pool.Assets.TryGetValue(paths[1], out res);
189+
}
190+
171191
public abstract void PlayAudio(AudioClip clip, float volume, float pitch);
172192

173193
public void Start(Action afterStart = null)

Runtime/Frameworks/UGUI/Internal/BorderAndBackground.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ private RectTransform EnsureBorderImage()
269269

270270
var border = Context.CreateNativeObject("[BorderImage]", typeof(RectTransform), typeof(WebBorderImage));
271271
borderImage = border.GetComponent<WebBorderImage>();
272+
borderImage.Context = Context;
272273

273274
borderImageRoot = border.transform as RectTransform;
274275

Runtime/Styling/AssetPool.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using ReactUnity.Helpers;
5+
6+
namespace ReactUnity.Styling
7+
{
8+
public interface IAssetPool
9+
{
10+
public SerializableDictionary Assets { get; }
11+
}
12+
13+
[CreateAssetMenu(fileName = "AssetPool", menuName = "React Unity/Asset Pool", order = 1)]
14+
public class AssetPool : ScriptableObject, IAssetPool
15+
{
16+
[SerializeField]
17+
private SerializableDictionary assets = new SerializableDictionary();
18+
public SerializableDictionary Assets => assets;
19+
}
20+
}

Runtime/Styling/AssetPool.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Types/AssetReference.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum AssetReferenceType
2424
Procedural = 7,
2525
Data = 8,
2626
Path = 9,
27+
Pool = 10,
2728
}
2829

2930
public class AssetReference<AssetType> : IDisposable where AssetType : class
@@ -95,6 +96,9 @@ public AssetReference(Url url)
9596
case UrlProtocol.Global:
9697
Type = AssetReferenceType.Global;
9798
break;
99+
case UrlProtocol.Pool:
100+
Type = AssetReferenceType.Pool;
101+
break;
98102
case UrlProtocol.None:
99103
default:
100104
Type = AssetReferenceType.None;
@@ -173,7 +177,10 @@ protected virtual T Get<T>(ReactContext context, AssetReferenceType realType, ob
173177
case AssetReferenceType.Resource:
174178
return ResourcesHelper.LoadResource<T>(realValue as string);
175179
case AssetReferenceType.Global:
176-
if (context.Globals.TryGetValue(realValue as string, out var res)) return res as T;
180+
if (context != null && context.Globals.TryGetValue(realValue as string, out var res)) return res as T;
181+
else return default;
182+
case AssetReferenceType.Pool:
183+
if (context != null && context.TryGetAssetFromPool(realValue as string, out var resp)) return resp as T;
177184
else return default;
178185
case AssetReferenceType.Object:
179186
return realValue as T;

Runtime/Types/ImageReference.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ protected override void Get(ReactContext context, AssetReferenceType realType, o
7878
switch (realType)
7979
{
8080
case AssetReferenceType.Resource:
81-
spriteRes = ResourcesHelper.LoadResource<Sprite>(realValue as string);
82-
break;
8381
case AssetReferenceType.Global:
84-
if (context.Globals.TryGetValue(realValue as string, out var res)) spriteRes = res as Sprite;
82+
case AssetReferenceType.Pool:
83+
spriteRes = Get<Sprite>(context, realType, realValue);
8584
break;
8685
}
8786

Runtime/Types/Url.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public Url(string fullUrl, UrlProtocol defaultProtocol = UrlProtocol.Contextual)
6464
{
6565
Protocol = UrlProtocol.Global;
6666
}
67+
else if (protocol == "pool" || protocol == "assetpool")
68+
{
69+
Protocol = UrlProtocol.Pool;
70+
}
6771
else
6872
{
6973
Protocol = defaultProtocol;
@@ -102,5 +106,6 @@ public enum UrlProtocol
102106
File = 4,
103107
Data = 5,
104108
Global = 6,
109+
Pool = 7,
105110
}
106111
}

0 commit comments

Comments
 (0)