Created
February 1, 2017 11:23
-
-
Save rjproz/799509b1efc5afca5bafa7494c4b35e0 to your computer and use it in GitHub Desktop.
HybCache example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| public class HowtoUseHybCache : MonoBehaviour { | |
| public Transform mVisualQuad; | |
| public List textures = new List(); | |
| 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/website/images/banner_logo.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"); | |
| } | |
| 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().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); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment