Skip to content

Instantly share code, notes, and snippets.

@rjproz
Created May 25, 2019 06:02
Show Gist options
  • Save rjproz/8172a1044ac1423550d314130d8e9aaa to your computer and use it in GitHub Desktop.
Save rjproz/8172a1044ac1423550d314130d8e9aaa to your computer and use it in GitHub Desktop.
HybCache - Full demo - 1.2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HowtoUseHybCache : MonoBehaviour {
public Transform mVisualQuad;
public List<Texture2D> textures = new List<Texture2D>();
void Start()
{
StartCoroutine(AnimateTextureQuad());
// Set config
HybCache.SetMaxCacheSize(20000000); // Setting the max cache size. 20 MB
// Set Expiry (Optional)
HybCache.EnableExpiry();
// or HybCache.DisableExpiry() to disable it
HybCache.SetExpiryAgeHours(10);
// or HybCache.SetExpiryAgeDays(1);
// or HybCache.SetExpiryAgeMinutes(1000);
// To Clear cache
// HybCache.Clear();
// Coroutine based implementation
StartCoroutine(LoadURl("http://hybriona.com/hybwebsite_3/images/Web_Bannermain.jpg"));
StartCoroutine(LoadURl("http://hybriona.com/services/api/extraservices/images/banner/puzzlehunt.png"));
StartCoroutine(LoadURl("http://hybriona.com/services/api/extraservices/images/banner/alienracer.png"));
//or you can use the below event based function
// LoadURLEventBased();
LoadURLEventBased("http://hybriona.com/services/api/extraservices/images/banner/melon.png");
LoadURLEventBased("http://hybriona.com/services/api/extraservices/images/banner/LBmutant.png");
//Load URL with standard HTTP Caching (via Headers). Currently only available as event based method
float timeStarted = 0;
string url_http_cache = "http://apis.hybriona.com/hybriona/unity/hugetext";
HybWWW.LoadUrlHC(url_http_cache,delegate(WWW www) {
if(string.IsNullOrEmpty(www.error))
{
Debug.Log("WWW ("+url_http_cache+") loaded in "+(Time.fixedTime - timeStarted)+" seconds.");
// Process the resource
Debug.Log(www.text);
}
else
{
Debug.LogError(www.error);
}
});
Invoke("DeleteSpecficCache",10);
}
IEnumerator LoadURl (string url) {
float timeStarted = Time.fixedTime;
WWW www = HybWWW.LoadUrl(url,"7216723");
yield return www;
if(www.error == null)
{
Debug.Log("WWW ("+url+") loaded in "+(Time.fixedTime - timeStarted)+" seconds.");
// Process the resource
Texture2D textureLoaded = www.textureNonReadable;
textures.Add(textureLoaded);
}
else
{
Debug.LogError(www.error);
}
}
void LoadURLEventBased(string url)
{
float timeStarted = Time.fixedTime;
HybWWW.LoadUrl(url,"7216723",delegate(WWW www) {
if(www.error == null)
{
Debug.Log("WWW ("+url+") loaded in "+(Time.fixedTime - timeStarted)+" seconds.");
// Process the resource
Texture2D textureLoaded = www.textureNonReadable;
textures.Add(textureLoaded);
}
else
{
Debug.LogError(www.error);
}
});
}
IEnumerator AnimateTextureQuad()
{
Transform mVisualTransform = mVisualQuad.transform;
Material mVisualMaterial = mVisualQuad.GetComponent<Renderer>().material;
int indexofTexture = 0;
while(true)
{
if(textures.Count > 0)
{
float height = 10;
float width = 10 * (float)textures[indexofTexture].width / (float)textures[indexofTexture].height;
float screenClipping = (Camera.main.aspect * 10) / width;
if(screenClipping < 1)
{
width = width * screenClipping; // To fit the quad under view
height = height * screenClipping;
}
mVisualMaterial.mainTexture = textures[indexofTexture];
mVisualTransform.localScale = new Vector3(width,height,1);
}
indexofTexture++;
if(indexofTexture >= textures.Count)
{
indexofTexture = 0;
}
yield return new WaitForSeconds(2);
}
}
public void DeleteSpecficCache()
{
HybCache.DeleteCache("http://hybriona.com/services/api/extraservices/images/banner/alienracer.png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment