Skip to content

Commit 56a6d36

Browse files
authored
Merge pull request #143 from fairygui/dev_atlas_ref_count
Add Atlas reference manage mechanism
2 parents 757b7b4 + 157812d commit 56a6d36

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

Assets/Scripts/Core/NGraphics.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ public NTexture texture
183183
{
184184
if (_texture != value)
185185
{
186+
if (value != null)
187+
value.AddRef();
188+
if (_texture != null)
189+
_texture.ReleaseRef();
190+
186191
_texture = value;
187192
if (_customMatarial != 0 && _material != null)
188193
_material.mainTexture = _texture != null ? _texture.nativeTexture : null;
@@ -213,11 +218,10 @@ public string shader
213218
public void SetShaderAndTexture(string shader, NTexture texture)
214219
{
215220
_shader = shader;
216-
_texture = texture;
217-
if (_customMatarial != 0 && _material != null)
218-
_material.mainTexture = _texture != null ? _texture.nativeTexture : null;
219-
_meshDirty = true;
220-
UpdateManager();
221+
if (_texture != texture)
222+
this.texture = texture;
223+
else
224+
UpdateManager();
221225
}
222226

223227
/// <summary>
@@ -508,6 +512,12 @@ public void Dispose()
508512
if ((_customMatarial & 128) != 0 && _material != null)
509513
Object.DestroyImmediate(_material);
510514

515+
if (_texture != null)
516+
{
517+
_texture.ReleaseRef();
518+
_texture = null;
519+
}
520+
511521
_manager = null;
512522
_material = null;
513523
meshRenderer = null;

Assets/Scripts/Core/NTexture.cs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public enum DestroyMethod
2222
/// </summary>
2323
public class NTexture
2424
{
25-
public static Action<Texture> CustomDestroyMethod;
25+
/// <summary>
26+
/// This event will trigger when a texture is destroying if its destroyMethod is Custom
27+
/// </summary>
28+
public static event Action<Texture> CustomDestroyMethod;
2629

2730
/// <summary>
2831
///
@@ -50,9 +53,14 @@ public class NTexture
5053
public DestroyMethod destroyMethod;
5154

5255
/// <summary>
53-
///
56+
/// This event will trigger when texture reloaded and size changed.
57+
/// </summary>
58+
public event Action<NTexture> onSizeChanged;
59+
60+
/// <summary>
61+
/// This event will trigger when ref count is zero.
5462
/// </summary>
55-
public event Action onSizeChanged;
63+
public event Action<NTexture> onRelease;
5664

5765
Texture _nativeTexture;
5866
Texture _alphaTexture;
@@ -407,7 +415,7 @@ public void Reload(Texture nativeTexture, Texture alphaTexture)
407415
RefreshMaterials();
408416

409417
if (onSizeChanged != null && lastSize != _originalSize)
410-
onSizeChanged();
418+
onSizeChanged(this);
411419
}
412420

413421
void DestroyTexture()
@@ -467,6 +475,34 @@ void DestroyMaterials()
467475
}
468476
}
469477

478+
public void AddRef()
479+
{
480+
if (_root == null) //disposed
481+
return;
482+
483+
if (_root != this && refCount == 0)
484+
_root.AddRef();
485+
486+
refCount++;
487+
}
488+
489+
public void ReleaseRef()
490+
{
491+
if (_root == null) //disposed
492+
return;
493+
494+
refCount--;
495+
496+
if (refCount == 0)
497+
{
498+
if (_root != this)
499+
_root.ReleaseRef();
500+
501+
if (onRelease != null)
502+
onRelease(this);
503+
}
504+
}
505+
470506
/// <summary>
471507
///
472508
/// </summary>
@@ -479,6 +515,7 @@ public void Dispose()
479515
Unload(true);
480516
_root = null;
481517
onSizeChanged = null;
518+
onRelease = null;
482519
}
483520
}
484521
}

Assets/Scripts/UI/GLoader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class GLoader : GObject, IAnimationGear, IColorGear
2323
bool _shrinkOnly;
2424
bool _updatingLayout;
2525
PackageItem _contentItem;
26-
Action _reloadDelegate;
26+
Action<NTexture> _reloadDelegate;
2727

2828
MovieClip _content;
2929
GObject _errorSign;
@@ -471,10 +471,10 @@ protected void onExternalLoadFailed()
471471
SetErrorState();
472472
}
473473

474-
void OnExternalReload()
474+
void OnExternalReload(NTexture texture)
475475
{
476-
sourceWidth = _content.texture.width;
477-
sourceHeight = _content.texture.height;
476+
sourceWidth = texture.width;
477+
sourceHeight = texture.height;
478478
UpdateLayout();
479479
}
480480

Assets/Scripts/UI/UIPackage.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace FairyGUI
1111
{
1212
/// <summary>
13-
/// A UI Package contains a description file and some texture,sound assets.
13+
/// A UI Package contains a description file and some texture, sound assets.
1414
/// </summary>
1515
public class UIPackage
1616
{
@@ -21,6 +21,11 @@ public class UIPackage
2121
/// </summary>
2222
public static bool unloadBundleByFGUI = true;
2323

24+
/// <summary>
25+
/// The event is triggered when all reference to this package item dropped.
26+
/// </summary>
27+
public static event Action<PackageItem> onReleaseResource;
28+
2429
/// <summary>
2530
/// Package id. It is generated by the Editor.
2631
/// </summary>
@@ -1319,7 +1324,14 @@ void LoadAtlas(PackageItem item)
13191324
}
13201325

13211326
if (item.texture == null)
1327+
{
13221328
item.texture = new NTexture(tex, alphaTex, (float)tex.width / item.width, (float)tex.height / item.height);
1329+
item.texture.onRelease += (NTexture t) =>
1330+
{
1331+
if (onReleaseResource != null)
1332+
onReleaseResource(item);
1333+
};
1334+
}
13231335
else
13241336
item.texture.Reload(tex, alphaTex);
13251337
item.texture.destroyMethod = dm;

0 commit comments

Comments
 (0)