Skip to content
This repository has been archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
Created TextureCache and implemented ImageDisplayData equality
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Wood committed Jun 11, 2019
1 parent e1e858a commit 8858e3c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/UI/DisplayData/ImageDisplayData.cs
Expand Up @@ -53,5 +53,28 @@ public void SetImageTexture(bool original, Texture2D value)
thumbnailTexture = value;
}
}

// ---------[ EQUALITY OVERRIDES ]---------
public override bool Equals(object obj)
{
if(!(obj is ImageDisplayData))
{
return false;
}

var other = (ImageDisplayData)obj;
bool isEqual = (this.ownerId == other.ownerId
&& this.mediaType == other.mediaType
&& this.imageId == other.imageId);
return isEqual;
}

public override int GetHashCode()
{
int idFactor = (string.IsNullOrEmpty(this.imageId)
? 1
: this.imageId.GetHashCode());
return (this.ownerId << 2) ^ idFactor;
}
}
}
48 changes: 48 additions & 0 deletions src/UI/TextureCache.cs
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using UnityEngine;

namespace ModIO.UI
{
/// <summary>Manages caching of the textures required by the UI.</summary>
public class TextureCache : MonoBehaviour
{
// ---------[ SINGLETON ]---------
/// <summary>The singleton instance of the TextureCache.</summary>
private static TextureCache _instance;
/// <summary>The singleton instance of the TextureCache.</summary>
public static TextureCache instance
{
get
{
if(TextureCache._instance == null)
{
TextureCache._instance = UIUtilities.FindComponentInScene<TextureCache>(true);

if(TextureCache._instance == null)
{
GameObject instanceGO = new GameObject("mod.io UI Texture Cache");
TextureCache._instance = instanceGO.AddComponent<TextureCache>();
}
}

return TextureCache._instance;
}
}

// ---------[ FIELDS ]---------
/// <summary>Should the cache be cleared on disable</summary>
public bool clearCacheOnDisable = true;

/// <summary>The texture cache holding all versions of cached images.</summary>
public Dictionary<ImageDisplayData, Texture2D[]> cache = new Dictionary<ImageDisplayData, Texture2D[]>();

// ---------[ INITIALIZATION ]---------
protected virtual void OnDisable()
{
if(this.clearCacheOnDisable)
{
this.cache.Clear();
}
}
}
}
11 changes: 11 additions & 0 deletions src/UI/TextureCache.cs.meta

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

0 comments on commit 8858e3c

Please sign in to comment.