diff --git a/Davinci/Assets/Davinci/Examples/1- Simple/SimpleExample.cs b/Davinci/Assets/Davinci/Examples/1- Simple/SimpleExample.cs index 5f73767..1091da1 100644 --- a/Davinci/Assets/Davinci/Examples/1- Simple/SimpleExample.cs +++ b/Davinci/Assets/Davinci/Examples/1- Simple/SimpleExample.cs @@ -1,5 +1,6 @@ using UnityEngine; using UnityEngine.UI; +using DavinciCore; public class SimpleExample : MonoBehaviour { diff --git a/Davinci/Assets/Davinci/Examples/2- Placeholders/PlaceholdersExample.cs b/Davinci/Assets/Davinci/Examples/2- Placeholders/PlaceholdersExample.cs index 23f11a8..6ea2047 100644 --- a/Davinci/Assets/Davinci/Examples/2- Placeholders/PlaceholdersExample.cs +++ b/Davinci/Assets/Davinci/Examples/2- Placeholders/PlaceholdersExample.cs @@ -1,5 +1,6 @@ using UnityEngine; using UnityEngine.UI; +using DavinciCore; public class PlaceholdersExample : MonoBehaviour { diff --git a/Davinci/Assets/Davinci/Examples/3- Fading/FadingExample.cs b/Davinci/Assets/Davinci/Examples/3- Fading/FadingExample.cs index 44516f2..bccc6ec 100644 --- a/Davinci/Assets/Davinci/Examples/3- Fading/FadingExample.cs +++ b/Davinci/Assets/Davinci/Examples/3- Fading/FadingExample.cs @@ -1,5 +1,6 @@ using UnityEngine; using UnityEngine.UI; +using DavinciCore; public class FadingExample : MonoBehaviour { diff --git a/Davinci/Assets/Davinci/Examples/4- Callbacks/CallbacksExample.cs b/Davinci/Assets/Davinci/Examples/4- Callbacks/CallbacksExample.cs index c8f67ab..70e3e82 100644 --- a/Davinci/Assets/Davinci/Examples/4- Callbacks/CallbacksExample.cs +++ b/Davinci/Assets/Davinci/Examples/4- Callbacks/CallbacksExample.cs @@ -1,5 +1,6 @@ using UnityEngine; using UnityEngine.UI; +using DavinciCore; public class CallbacksExample : MonoBehaviour { diff --git a/Davinci/Assets/Davinci/Examples/5- Load as Texture/TextureExamlpe.cs b/Davinci/Assets/Davinci/Examples/5- Load as Texture/TextureExamlpe.cs index 986f609..7e58b10 100644 --- a/Davinci/Assets/Davinci/Examples/5- Load as Texture/TextureExamlpe.cs +++ b/Davinci/Assets/Davinci/Examples/5- Load as Texture/TextureExamlpe.cs @@ -1,5 +1,6 @@ using UnityEngine; using UnityEngine.UI; +using DavinciCore; public class TextureExamlpe : MonoBehaviour { diff --git a/Davinci/Assets/Davinci/Scripts/Davinci.cs b/Davinci/Assets/Davinci/Scripts/Davinci.cs index 3932a00..7f53c3e 100644 --- a/Davinci/Assets/Davinci/Scripts/Davinci.cs +++ b/Davinci/Assets/Davinci/Scripts/Davinci.cs @@ -18,642 +18,648 @@ /// Licensed under the MIT License. /// https://github.com/shamsdev/davinci /// -public class Davinci : MonoBehaviour -{ - private static bool ENABLE_GLOBAL_LOGS = true; +/// - private bool enableLog = false; - private float fadeTime = 1; - private bool cached = true; +namespace DavinciCore +{ - private enum RendererType + public class Davinci : MonoBehaviour { - none, - uiImage, - renderer, - sprite - } + private static bool ENABLE_GLOBAL_LOGS = true; - private RendererType rendererType = RendererType.none; - private GameObject targetObj; - private string url = null; + private bool enableLog = false; + private float fadeTime = 1; + private bool cached = true; - private Texture2D loadingPlaceholder, errorPlaceholder; + private enum RendererType + { + none, + uiImage, + renderer, + sprite + } - private UnityAction onStartAction, - onDownloadedAction, - OnLoadedAction, - onEndAction; + private RendererType rendererType = RendererType.none; + private GameObject targetObj; + private string url = null; - private UnityAction onDownloadProgressChange; - private UnityAction onErrorAction; + private Texture2D loadingPlaceholder, errorPlaceholder; - private static Dictionary underProcessDavincies - = new Dictionary(); + private UnityAction onStartAction, + onDownloadedAction, + OnLoadedAction, + onEndAction; - private string uniqueHash; - private int progress; + private UnityAction onDownloadProgressChange; + private UnityAction onErrorAction; - private bool success = false; + private static Dictionary underProcessDavincies + = new Dictionary(); - static string filePath = Application.persistentDataPath + "/" + - "davinci" + "/"; + private string uniqueHash; + private int progress; + private bool success = false; - /// - /// Get instance of davinci class - /// - public static Davinci get() - { - return new GameObject("Davinci").AddComponent(); - } + static string filePath = Application.persistentDataPath + "/" + + "davinci" + "/"; - /// - /// Set image url for download. - /// - /// Image Url - /// - public Davinci load(string url) - { - if (enableLog) - Debug.Log("[Davinci] Url set : " + url); - this.url = url; - return this; - } - - /// - /// Set fading animation time. - /// - /// Fade animation time. Set 0 for disable fading. - /// - public Davinci setFadeTime(float fadeTime) - { - if (enableLog) - Debug.Log("[Davinci] Fading time set : " + fadeTime); - - this.fadeTime = fadeTime; - return this; - } + /// + /// Get instance of davinci class + /// + public static Davinci get() + { + return new GameObject("Davinci").AddComponent(); + } - /// - /// Set target Image component. - /// - /// target Unity UI image component - /// - public Davinci into(Image image) - { - if (enableLog) - Debug.Log("[Davinci] Target as UIImage set : " + image); + /// + /// Set image url for download. + /// + /// Image Url + /// + public Davinci load(string url) + { + if (enableLog) + Debug.Log("[Davinci] Url set : " + url); - rendererType = RendererType.uiImage; - this.targetObj = image.gameObject; - return this; - } + this.url = url; + return this; + } - /// - /// Set target Renderer component. - /// - /// target renderer component - /// - public Davinci into(Renderer renderer) - { - if (enableLog) - Debug.Log("[Davinci] Target as Renderer set : " + renderer); + /// + /// Set fading animation time. + /// + /// Fade animation time. Set 0 for disable fading. + /// + public Davinci setFadeTime(float fadeTime) + { + if (enableLog) + Debug.Log("[Davinci] Fading time set : " + fadeTime); - rendererType = RendererType.renderer; - this.targetObj = renderer.gameObject; - return this; - } + this.fadeTime = fadeTime; + return this; + } - public Davinci into(SpriteRenderer spriteRenderer) - { - if (enableLog) - Debug.Log("[Davinci] Target as SpriteRenderer set : " + spriteRenderer); + /// + /// Set target Image component. + /// + /// target Unity UI image component + /// + public Davinci into(Image image) + { + if (enableLog) + Debug.Log("[Davinci] Target as UIImage set : " + image); - rendererType = RendererType.sprite; - this.targetObj = spriteRenderer.gameObject; - return this; - } + rendererType = RendererType.uiImage; + this.targetObj = image.gameObject; + return this; + } - #region Actions - public Davinci withStartAction(UnityAction action) - { - this.onStartAction = action; + /// + /// Set target Renderer component. + /// + /// target renderer component + /// + public Davinci into(Renderer renderer) + { + if (enableLog) + Debug.Log("[Davinci] Target as Renderer set : " + renderer); - if (enableLog) - Debug.Log("[Davinci] On start action set : " + action); + rendererType = RendererType.renderer; + this.targetObj = renderer.gameObject; + return this; + } - return this; - } + public Davinci into(SpriteRenderer spriteRenderer) + { + if (enableLog) + Debug.Log("[Davinci] Target as SpriteRenderer set : " + spriteRenderer); - public Davinci withDownloadedAction(UnityAction action) - { - this.onDownloadedAction = action; + rendererType = RendererType.sprite; + this.targetObj = spriteRenderer.gameObject; + return this; + } - if (enableLog) - Debug.Log("[Davinci] On downloaded action set : " + action); + #region Actions + public Davinci withStartAction(UnityAction action) + { + this.onStartAction = action; - return this; - } + if (enableLog) + Debug.Log("[Davinci] On start action set : " + action); - public Davinci withDownloadProgressChangedAction(UnityAction action) - { - this.onDownloadProgressChange = action; + return this; + } - if (enableLog) - Debug.Log("[Davinci] On download progress changed action set : " + action); + public Davinci withDownloadedAction(UnityAction action) + { + this.onDownloadedAction = action; - return this; - } + if (enableLog) + Debug.Log("[Davinci] On downloaded action set : " + action); - public Davinci withLoadedAction(UnityAction action) - { - this.OnLoadedAction = action; + return this; + } - if (enableLog) - Debug.Log("[Davinci] On loaded action set : " + action); + public Davinci withDownloadProgressChangedAction(UnityAction action) + { + this.onDownloadProgressChange = action; - return this; - } + if (enableLog) + Debug.Log("[Davinci] On download progress changed action set : " + action); - public Davinci withErrorAction(UnityAction action) - { - this.onErrorAction = action; + return this; + } - if (enableLog) - Debug.Log("[Davinci] On error action set : " + action); + public Davinci withLoadedAction(UnityAction action) + { + this.OnLoadedAction = action; - return this; - } + if (enableLog) + Debug.Log("[Davinci] On loaded action set : " + action); - public Davinci withEndAction(UnityAction action) - { - this.onEndAction = action; + return this; + } - if (enableLog) - Debug.Log("[Davinci] On end action set : " + action); + public Davinci withErrorAction(UnityAction action) + { + this.onErrorAction = action; - return this; - } - #endregion - - /// - /// Show or hide logs in console. - /// - /// 'true' for show logs in console. - /// - public Davinci setEnableLog(bool enableLog) - { - this.enableLog = enableLog; + if (enableLog) + Debug.Log("[Davinci] On error action set : " + action); - if (enableLog) - Debug.Log("[Davinci] Logging enabled : " + enableLog); + return this; + } - return this; - } + public Davinci withEndAction(UnityAction action) + { + this.onEndAction = action; - /// - /// Set the sprite of image when davinci is downloading and loading image - /// - /// loading texture - /// - public Davinci setLoadingPlaceholder(Texture2D loadingPlaceholder) - { - this.loadingPlaceholder = loadingPlaceholder; + if (enableLog) + Debug.Log("[Davinci] On end action set : " + action); - if (enableLog) - Debug.Log("[Davinci] Loading placeholder has been set."); + return this; + } + #endregion + + /// + /// Show or hide logs in console. + /// + /// 'true' for show logs in console. + /// + public Davinci setEnableLog(bool enableLog) + { + this.enableLog = enableLog; - return this; - } + if (enableLog) + Debug.Log("[Davinci] Logging enabled : " + enableLog); - /// - /// Set image sprite when some error occurred during downloading or loading image - /// - /// error texture - /// - public Davinci setErrorPlaceholder(Texture2D errorPlaceholder) - { - this.errorPlaceholder = errorPlaceholder; + return this; + } - if (enableLog) - Debug.Log("[Davinci] Error placeholder has been set."); + /// + /// Set the sprite of image when davinci is downloading and loading image + /// + /// loading texture + /// + public Davinci setLoadingPlaceholder(Texture2D loadingPlaceholder) + { + this.loadingPlaceholder = loadingPlaceholder; - return this; - } + if (enableLog) + Debug.Log("[Davinci] Loading placeholder has been set."); - /// - /// Enable cache - /// - /// - public Davinci setCached(bool cached) - { - this.cached = cached; + return this; + } - if (enableLog) - Debug.Log("[Davinci] Cache enabled : " + cached); + /// + /// Set image sprite when some error occurred during downloading or loading image + /// + /// error texture + /// + public Davinci setErrorPlaceholder(Texture2D errorPlaceholder) + { + this.errorPlaceholder = errorPlaceholder; - return this; - } + if (enableLog) + Debug.Log("[Davinci] Error placeholder has been set."); - /// - /// Start davinci process. - /// - public void start() - { - if (url == null) - { - error("Url has not been set. Use 'load' funtion to set image url."); - return; + return this; } - try - { - Uri uri = new Uri(url); - this.url = uri.AbsoluteUri; - } - catch (Exception ex) + /// + /// Enable cache + /// + /// + public Davinci setCached(bool cached) { - error("Url is not correct."); - return; + this.cached = cached; + + if (enableLog) + Debug.Log("[Davinci] Cache enabled : " + cached); + + return this; } - if (rendererType == RendererType.none || targetObj == null) + /// + /// Start davinci process. + /// + public void start() { - error("Target has not been set. Use 'into' function to set target component."); - return; - } + if (url == null) + { + error("Url has not been set. Use 'load' funtion to set image url."); + return; + } - if (enableLog) - Debug.Log("[Davinci] Start Working."); + try + { + Uri uri = new Uri(url); + this.url = uri.AbsoluteUri; + } + catch (Exception ex) + { + error("Url is not correct."); + return; + } - if (loadingPlaceholder != null) - SetLoadingImage(); + if (rendererType == RendererType.none || targetObj == null) + { + error("Target has not been set. Use 'into' function to set target component."); + return; + } - if (onStartAction != null) - onStartAction.Invoke(); + if (enableLog) + Debug.Log("[Davinci] Start Working."); - if (!Directory.Exists(filePath)) - { - Directory.CreateDirectory(filePath); - } + if (loadingPlaceholder != null) + SetLoadingImage(); - uniqueHash = CreateMD5(url); + if (onStartAction != null) + onStartAction.Invoke(); - if (underProcessDavincies.ContainsKey(uniqueHash)) - { - Davinci sameProcess = underProcessDavincies[uniqueHash]; - sameProcess.onDownloadedAction += () => + if (!Directory.Exists(filePath)) { - if (onDownloadedAction != null) - onDownloadedAction.Invoke(); + Directory.CreateDirectory(filePath); + } - loadSpriteToImage(); - }; - } - else - { - if (File.Exists(filePath + uniqueHash)) - { - if (onDownloadedAction != null) - onDownloadedAction.Invoke(); + uniqueHash = CreateMD5(url); - loadSpriteToImage(); + if (underProcessDavincies.ContainsKey(uniqueHash)) + { + Davinci sameProcess = underProcessDavincies[uniqueHash]; + sameProcess.onDownloadedAction += () => + { + if (onDownloadedAction != null) + onDownloadedAction.Invoke(); + + loadSpriteToImage(); + }; } else { - underProcessDavincies.Add(uniqueHash, this); - StopAllCoroutines(); - StartCoroutine("Downloader"); + if (File.Exists(filePath + uniqueHash)) + { + if (onDownloadedAction != null) + onDownloadedAction.Invoke(); + + loadSpriteToImage(); + } + else + { + underProcessDavincies.Add(uniqueHash, this); + StopAllCoroutines(); + StartCoroutine("Downloader"); + } } } - } - private IEnumerator Downloader() - { - if (enableLog) - Debug.Log("[Davinci] Download started."); + private IEnumerator Downloader() + { + if (enableLog) + Debug.Log("[Davinci] Download started."); #if UNITY_2018_3_OR_NEWER - UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); - yield return www.SendWebRequest(); + UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); + yield return www.SendWebRequest(); #else var www = new WWW(url); #endif - while (!www.isDone) - { - if (www.error != null) + while (!www.isDone) { - error("Error while downloading the image : " + www.error); - yield break; - } + if (www.error != null) + { + error("Error while downloading the image : " + www.error); + yield break; + } #if UNITY_2018_3_OR_NEWER - progress = Mathf.FloorToInt(www.downloadProgress * 100); + progress = Mathf.FloorToInt(www.downloadProgress * 100); #else progress = Mathf.FloorToInt(www.progress * 100); #endif - if (onDownloadProgressChange != null) - onDownloadProgressChange.Invoke(progress); + if (onDownloadProgressChange != null) + onDownloadProgressChange.Invoke(progress); - if (enableLog) - Debug.Log("[Davinci] Downloading progress : " + progress + "%"); + if (enableLog) + Debug.Log("[Davinci] Downloading progress : " + progress + "%"); - yield return null; - } + yield return null; + } #if UNITY_2018_3_OR_NEWER - if (www.error == null) - File.WriteAllBytes(filePath + uniqueHash, www.downloadHandler.data); + if (www.error == null) + File.WriteAllBytes(filePath + uniqueHash, www.downloadHandler.data); #else if (www.error == null) File.WriteAllBytes(filePath + uniqueHash, www.bytes); #endif - www.Dispose(); - www = null; + www.Dispose(); + www = null; - if (onDownloadedAction != null) - onDownloadedAction.Invoke(); + if (onDownloadedAction != null) + onDownloadedAction.Invoke(); - loadSpriteToImage(); + loadSpriteToImage(); - underProcessDavincies.Remove(uniqueHash); - } + underProcessDavincies.Remove(uniqueHash); + } - private void loadSpriteToImage() - { - progress = 100; - if (onDownloadProgressChange != null) - onDownloadProgressChange.Invoke(progress); + private void loadSpriteToImage() + { + progress = 100; + if (onDownloadProgressChange != null) + onDownloadProgressChange.Invoke(progress); - if (enableLog) - Debug.Log("[Davinci] Downloading progress : " + progress + "%"); + if (enableLog) + Debug.Log("[Davinci] Downloading progress : " + progress + "%"); - if (!File.Exists(filePath + uniqueHash)) - { - error("Loading image file has been failed."); - return; - } + if (!File.Exists(filePath + uniqueHash)) + { + error("Loading image file has been failed."); + return; + } - StopAllCoroutines(); - StartCoroutine(ImageLoader()); - } + StopAllCoroutines(); + StartCoroutine(ImageLoader()); + } - private void SetLoadingImage() - { - switch (rendererType) + private void SetLoadingImage() { - case RendererType.renderer: - Renderer renderer = targetObj.GetComponent(); - renderer.material.mainTexture = loadingPlaceholder; - break; - - case RendererType.uiImage: - Image image = targetObj.GetComponent(); - Sprite sprite = Sprite.Create(loadingPlaceholder, - new Rect(0, 0, loadingPlaceholder.width, loadingPlaceholder.height), - new Vector2(0.5f, 0.5f)); - image.sprite = sprite; - - break; - - case RendererType.sprite: - SpriteRenderer spriteRenderer = targetObj.GetComponent(); - Sprite spriteImage = Sprite.Create(loadingPlaceholder, - new Rect(0, 0, loadingPlaceholder.width, loadingPlaceholder.height), - new Vector2(0.5f, 0.5f)); - - spriteRenderer.sprite = spriteImage; - break; - } + switch (rendererType) + { + case RendererType.renderer: + Renderer renderer = targetObj.GetComponent(); + renderer.material.mainTexture = loadingPlaceholder; + break; - } + case RendererType.uiImage: + Image image = targetObj.GetComponent(); + Sprite sprite = Sprite.Create(loadingPlaceholder, + new Rect(0, 0, loadingPlaceholder.width, loadingPlaceholder.height), + new Vector2(0.5f, 0.5f)); + image.sprite = sprite; - private IEnumerator ImageLoader(Texture2D texture = null) - { - if (enableLog) - Debug.Log("[Davinci] Start loading image."); + break; + + case RendererType.sprite: + SpriteRenderer spriteRenderer = targetObj.GetComponent(); + Sprite spriteImage = Sprite.Create(loadingPlaceholder, + new Rect(0, 0, loadingPlaceholder.width, loadingPlaceholder.height), + new Vector2(0.5f, 0.5f)); + + spriteRenderer.sprite = spriteImage; + break; + } - if (texture == null) - { - byte[] fileData; - fileData = File.ReadAllBytes(filePath + uniqueHash); - texture = new Texture2D(2, 2); - //ImageConversion.LoadImage(texture, fileData); - texture.LoadImage(fileData); //..this will auto-resize the texture dimensions. } - Color color; + private IEnumerator ImageLoader(Texture2D texture = null) + { + if (enableLog) + Debug.Log("[Davinci] Start loading image."); - if (targetObj != null) - switch (rendererType) + if (texture == null) { - case RendererType.renderer: - Renderer renderer = targetObj.GetComponent(); + byte[] fileData; + fileData = File.ReadAllBytes(filePath + uniqueHash); + texture = new Texture2D(2, 2); + //ImageConversion.LoadImage(texture, fileData); + texture.LoadImage(fileData); //..this will auto-resize the texture dimensions. + } - if (renderer == null || renderer.material == null) - break; + Color color; - renderer.material.mainTexture = texture; - float maxAlpha; + if (targetObj != null) + switch (rendererType) + { + case RendererType.renderer: + Renderer renderer = targetObj.GetComponent(); - if (fadeTime > 0 && renderer.material.HasProperty("_Color")) - { - color = renderer.material.color; - maxAlpha = color.a; + if (renderer == null || renderer.material == null) + break; - color.a = 0; + renderer.material.mainTexture = texture; + float maxAlpha; - renderer.material.color = color; - float time = Time.time; - while (color.a < maxAlpha) + if (fadeTime > 0 && renderer.material.HasProperty("_Color")) { - color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime); + color = renderer.material.color; + maxAlpha = color.a; - if (renderer != null) - renderer.material.color = color; + color.a = 0; - yield return null; - } - } + renderer.material.color = color; + float time = Time.time; + while (color.a < maxAlpha) + { + color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime); - break; + if (renderer != null) + renderer.material.color = color; - case RendererType.uiImage: - Image image = targetObj.GetComponent(); + yield return null; + } + } - if (image == null) break; - Sprite sprite = Sprite.Create(texture, - new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); + case RendererType.uiImage: + Image image = targetObj.GetComponent(); - image.sprite = sprite; - color = image.color; - maxAlpha = color.a; + if (image == null) + break; - if (fadeTime > 0) - { - color.a = 0; - image.color = color; + Sprite sprite = Sprite.Create(texture, + new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); - float time = Time.time; - while (color.a < maxAlpha) - { - color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime); + image.sprite = sprite; + color = image.color; + maxAlpha = color.a; - if (image != null) - image.color = color; - yield return null; + if (fadeTime > 0) + { + color.a = 0; + image.color = color; + + float time = Time.time; + while (color.a < maxAlpha) + { + color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime); + + if (image != null) + image.color = color; + yield return null; + } } - } - break; - - case RendererType.sprite: - SpriteRenderer spriteRenderer = targetObj.GetComponent(); - - if (spriteRenderer == null) break; - Sprite spriteImage = Sprite.Create(texture, - new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); + case RendererType.sprite: + SpriteRenderer spriteRenderer = targetObj.GetComponent(); - spriteRenderer.sprite = spriteImage; - color = spriteRenderer.color; - maxAlpha = color.a; + if (spriteRenderer == null) + break; - if (fadeTime > 0) - { - color.a = 0; - spriteRenderer.color = color; + Sprite spriteImage = Sprite.Create(texture, + new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); - float time = Time.time; - while (color.a < maxAlpha) - { - color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime); + spriteRenderer.sprite = spriteImage; + color = spriteRenderer.color; + maxAlpha = color.a; - if (spriteRenderer != null) - spriteRenderer.color = color; - yield return null; + if (fadeTime > 0) + { + color.a = 0; + spriteRenderer.color = color; + + float time = Time.time; + while (color.a < maxAlpha) + { + color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime); + + if (spriteRenderer != null) + spriteRenderer.color = color; + yield return null; + } } - } - break; - } + break; + } - if (OnLoadedAction != null) - OnLoadedAction.Invoke(); + if (OnLoadedAction != null) + OnLoadedAction.Invoke(); - if (enableLog) - Debug.Log("[Davinci] Image has been loaded."); + if (enableLog) + Debug.Log("[Davinci] Image has been loaded."); - success = true; - finish(); - } + success = true; + finish(); + } - public static string CreateMD5(string input) - { - // Use input string to calculate MD5 hash - using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) + public static string CreateMD5(string input) { - byte[] inputBytes = Encoding.ASCII.GetBytes(input); - byte[] hashBytes = md5.ComputeHash(inputBytes); - - // Convert the byte array to hexadecimal string - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < hashBytes.Length; i++) + // Use input string to calculate MD5 hash + using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create()) { - sb.Append(hashBytes[i].ToString("X2")); + byte[] inputBytes = Encoding.ASCII.GetBytes(input); + byte[] hashBytes = md5.ComputeHash(inputBytes); + + // Convert the byte array to hexadecimal string + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < hashBytes.Length; i++) + { + sb.Append(hashBytes[i].ToString("X2")); + } + return sb.ToString(); } - return sb.ToString(); } - } - - private void error(string message) - { - success = false; - if (enableLog) - Debug.LogError("[Davinci] Error : " + message); + private void error(string message) + { + success = false; - if (onErrorAction != null) - onErrorAction.Invoke(message); + if (enableLog) + Debug.LogError("[Davinci] Error : " + message); - if (errorPlaceholder != null) - StartCoroutine(ImageLoader(errorPlaceholder)); - else finish(); - } + if (onErrorAction != null) + onErrorAction.Invoke(message); - private void finish() - { - if (enableLog) - Debug.Log("[Davinci] Operation has been finished."); + if (errorPlaceholder != null) + StartCoroutine(ImageLoader(errorPlaceholder)); + else finish(); + } - if (!cached) + private void finish() { - try + if (enableLog) + Debug.Log("[Davinci] Operation has been finished."); + + if (!cached) { - File.Delete(filePath + uniqueHash); + try + { + File.Delete(filePath + uniqueHash); + } + catch (Exception ex) + { + if (enableLog) + Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}"); + } } - catch (Exception ex) - { - if (enableLog) - Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}"); - } - } - if (onEndAction != null) - onEndAction.Invoke(); + if (onEndAction != null) + onEndAction.Invoke(); - Invoke("destroyer", 0.5f); - } + Invoke("destroyer", 0.5f); + } - private void destroyer() - { - Destroy(gameObject); - } + private void destroyer() + { + Destroy(gameObject); + } - /// - /// Clear a certain cached file with its url - /// - /// Cached file url. - /// - public static void ClearCache(string url) - { - try + /// + /// Clear a certain cached file with its url + /// + /// Cached file url. + /// + public static void ClearCache(string url) { - File.Delete(filePath + CreateMD5(url)); + try + { + File.Delete(filePath + CreateMD5(url)); - if (ENABLE_GLOBAL_LOGS) - Debug.Log($"[Davinci] Cached file has been cleared: {url}"); - } - catch (Exception ex) - { - if (ENABLE_GLOBAL_LOGS) - Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}"); + if (ENABLE_GLOBAL_LOGS) + Debug.Log($"[Davinci] Cached file has been cleared: {url}"); + } + catch (Exception ex) + { + if (ENABLE_GLOBAL_LOGS) + Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}"); + } } - } - /// - /// Clear all davinci cached files - /// - /// - public static void ClearAllCachedFiles() - { - try + /// + /// Clear all davinci cached files + /// + /// + public static void ClearAllCachedFiles() { - Directory.Delete(filePath, true); + try + { + Directory.Delete(filePath, true); - if (ENABLE_GLOBAL_LOGS) - Debug.Log("[Davinci] All Davinci cached files has been cleared."); - } - catch (Exception ex) - { - if (ENABLE_GLOBAL_LOGS) - Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}"); + if (ENABLE_GLOBAL_LOGS) + Debug.Log("[Davinci] All Davinci cached files has been cleared."); + } + catch (Exception ex) + { + if (ENABLE_GLOBAL_LOGS) + Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}"); + } } } -} \ No newline at end of file +} diff --git a/Davinci/Assets/Davinci/Scripts/DavinciCore.asmdef b/Davinci/Assets/Davinci/Scripts/DavinciCore.asmdef new file mode 100644 index 0000000..5919f41 --- /dev/null +++ b/Davinci/Assets/Davinci/Scripts/DavinciCore.asmdef @@ -0,0 +1,3 @@ +{ + "name": "DavinciCore" +} diff --git a/Davinci/Assets/Davinci/Scripts/DavinciCore.asmdef.meta b/Davinci/Assets/Davinci/Scripts/DavinciCore.asmdef.meta new file mode 100644 index 0000000..ad88381 --- /dev/null +++ b/Davinci/Assets/Davinci/Scripts/DavinciCore.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ac6bf373a71824ea28d04d3dccc615f7 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Davinci/Assets/Davinci/package.json b/Davinci/Assets/Davinci/package.json new file mode 100644 index 0000000..6640ad3 --- /dev/null +++ b/Davinci/Assets/Davinci/package.json @@ -0,0 +1,9 @@ +{ + "name": "com.shamsdev.davinci", + "displayName": "Davinci", + "version": "1.2.0", + "unity": "2018.3", + "description": "A powerful, easy-to-use image downloading and caching library for Unity in Run-Time!", + "license": "MIT" + +} \ No newline at end of file diff --git a/Davinci/Assets/Davinci/package.json.meta b/Davinci/Assets/Davinci/package.json.meta new file mode 100644 index 0000000..447298c --- /dev/null +++ b/Davinci/Assets/Davinci/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 57d9a47005792481c8cb9a6f93d95aeb +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: