Skip to content

Commit

Permalink
Merge pull request #143 from fairygui/dev_atlas_ref_count
Browse files Browse the repository at this point in the history
Add Atlas reference manage mechanism
  • Loading branch information
xiaoguzhu authored Sep 3, 2020
2 parents 757b7b4 + 157812d commit 56a6d36
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
20 changes: 15 additions & 5 deletions Assets/Scripts/Core/NGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public NTexture texture
{
if (_texture != value)
{
if (value != null)
value.AddRef();
if (_texture != null)
_texture.ReleaseRef();

_texture = value;
if (_customMatarial != 0 && _material != null)
_material.mainTexture = _texture != null ? _texture.nativeTexture : null;
Expand Down Expand Up @@ -213,11 +218,10 @@ public string shader
public void SetShaderAndTexture(string shader, NTexture texture)
{
_shader = shader;
_texture = texture;
if (_customMatarial != 0 && _material != null)
_material.mainTexture = _texture != null ? _texture.nativeTexture : null;
_meshDirty = true;
UpdateManager();
if (_texture != texture)
this.texture = texture;
else
UpdateManager();
}

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

if (_texture != null)
{
_texture.ReleaseRef();
_texture = null;
}

_manager = null;
_material = null;
meshRenderer = null;
Expand Down
45 changes: 41 additions & 4 deletions Assets/Scripts/Core/NTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public enum DestroyMethod
/// </summary>
public class NTexture
{
public static Action<Texture> CustomDestroyMethod;
/// <summary>
/// This event will trigger when a texture is destroying if its destroyMethod is Custom
/// </summary>
public static event Action<Texture> CustomDestroyMethod;

/// <summary>
///
Expand Down Expand Up @@ -50,9 +53,14 @@ public class NTexture
public DestroyMethod destroyMethod;

/// <summary>
///
/// This event will trigger when texture reloaded and size changed.
/// </summary>
public event Action<NTexture> onSizeChanged;

/// <summary>
/// This event will trigger when ref count is zero.
/// </summary>
public event Action onSizeChanged;
public event Action<NTexture> onRelease;

Texture _nativeTexture;
Texture _alphaTexture;
Expand Down Expand Up @@ -407,7 +415,7 @@ public void Reload(Texture nativeTexture, Texture alphaTexture)
RefreshMaterials();

if (onSizeChanged != null && lastSize != _originalSize)
onSizeChanged();
onSizeChanged(this);
}

void DestroyTexture()
Expand Down Expand Up @@ -467,6 +475,34 @@ void DestroyMaterials()
}
}

public void AddRef()
{
if (_root == null) //disposed
return;

if (_root != this && refCount == 0)
_root.AddRef();

refCount++;
}

public void ReleaseRef()
{
if (_root == null) //disposed
return;

refCount--;

if (refCount == 0)
{
if (_root != this)
_root.ReleaseRef();

if (onRelease != null)
onRelease(this);
}
}

/// <summary>
///
/// </summary>
Expand All @@ -479,6 +515,7 @@ public void Dispose()
Unload(true);
_root = null;
onSizeChanged = null;
onRelease = null;
}
}
}
8 changes: 4 additions & 4 deletions Assets/Scripts/UI/GLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class GLoader : GObject, IAnimationGear, IColorGear
bool _shrinkOnly;
bool _updatingLayout;
PackageItem _contentItem;
Action _reloadDelegate;
Action<NTexture> _reloadDelegate;

MovieClip _content;
GObject _errorSign;
Expand Down Expand Up @@ -471,10 +471,10 @@ protected void onExternalLoadFailed()
SetErrorState();
}

void OnExternalReload()
void OnExternalReload(NTexture texture)
{
sourceWidth = _content.texture.width;
sourceHeight = _content.texture.height;
sourceWidth = texture.width;
sourceHeight = texture.height;
UpdateLayout();
}

Expand Down
14 changes: 13 additions & 1 deletion Assets/Scripts/UI/UIPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace FairyGUI
{
/// <summary>
/// A UI Package contains a description file and some texture,sound assets.
/// A UI Package contains a description file and some texture, sound assets.
/// </summary>
public class UIPackage
{
Expand All @@ -21,6 +21,11 @@ public class UIPackage
/// </summary>
public static bool unloadBundleByFGUI = true;

/// <summary>
/// The event is triggered when all reference to this package item dropped.
/// </summary>
public static event Action<PackageItem> onReleaseResource;

/// <summary>
/// Package id. It is generated by the Editor.
/// </summary>
Expand Down Expand Up @@ -1319,7 +1324,14 @@ void LoadAtlas(PackageItem item)
}

if (item.texture == null)
{
item.texture = new NTexture(tex, alphaTex, (float)tex.width / item.width, (float)tex.height / item.height);
item.texture.onRelease += (NTexture t) =>
{
if (onReleaseResource != null)
onReleaseResource(item);
};
}
else
item.texture.Reload(tex, alphaTex);
item.texture.destroyMethod = dm;
Expand Down

0 comments on commit 56a6d36

Please sign in to comment.