AR视频direction文件相关调整

This commit is contained in:
lishuo committed 2022-05-25 18:40:47 +08:00
1 parent d876c20def
commit 17c2a44e40
62 files changed
+120845 -42066

No files matched your search

+601
View File
@@ -0,0 +1,601 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// Davinci - A powerful, esay-to-use image downloading and caching library for Unity in Run-Time
/// v 1.2
/// Developed by ShamsDEV.com
/// copyright (c) ShamsDEV.com All Rights Reserved.
/// Licensed under the MIT License.
/// https://github.com/shamsdev/davinci
/// </summary>
public class Davinci : MonoBehaviour
{
private static bool ENABLE_GLOBAL_LOGS = true;
private bool enableLog = false;
private float fadeTime = 1;
private bool cached = true;
private enum RendererType
{
none,
uiImage,
renderer
}
private RendererType rendererType = RendererType.none;
private GameObject targetObj;
private string url = null;
private Texture2D loadingPlaceholder, errorPlaceholder;
private UnityAction onStartAction,
onDownloadedAction,
OnLoadedAction,
onEndAction;
private UnityAction<int> onDownloadProgressChange;
private UnityAction<string> onErrorAction;
private static Dictionary<string, Davinci> underProcessDavincies
= new Dictionary<string, Davinci>();
private string uniqueHash;
private int progress;
private bool success = false;
static string filePath = Application.persistentDataPath + "/" +
"davinci" + "/";
/// <summary>
/// Get instance of davinci class
/// </summary>
public static Davinci get()
{
return new GameObject("Davinci").AddComponent<Davinci>();
}
/// <summary>
/// Set image url for download.
/// </summary>
/// <param name="url">Image Url</param>
/// <returns></returns>
public Davinci load(string url)
{
if (enableLog)
Debug.Log("[Davinci] Url set : " + url);
this.url = url;
return this;
}
/// <summary>
/// Set fading animation time.
/// </summary>
/// <param name="fadeTime">Fade animation time. Set 0 for disable fading.</param>
/// <returns></returns>
public Davinci setFadeTime(float fadeTime)
{
if (enableLog)
Debug.Log("[Davinci] Fading time set : " + fadeTime);
this.fadeTime = fadeTime;
return this;
}
/// <summary>
/// Set target Image component.
/// </summary>
/// <param name="image">target Unity UI image component</param>
/// <returns></returns>
public Davinci into(MaskableGraphic image)
{
if (enableLog)
Debug.Log("[Davinci] Target as UIImage set : " + image);
rendererType = RendererType.uiImage;
this.targetObj = image.gameObject;
return this;
}
/// <summary>
/// Set target Renderer component.
/// </summary>
/// <param name="renderer">target renderer component</param>
/// <returns></returns>
public Davinci into(Renderer renderer)
{
if (enableLog)
Debug.Log("[Davinci] Target as Renderer set : " + renderer);
rendererType = RendererType.renderer;
this.targetObj = renderer.gameObject;
return this;
}
#region Actions
public Davinci withStartAction(UnityAction action)
{
this.onStartAction = action;
if (enableLog)
Debug.Log("[Davinci] On start action set : " + action);
return this;
}
public Davinci withDownloadedAction(UnityAction action)
{
this.onDownloadedAction = action;
if (enableLog)
Debug.Log("[Davinci] On downloaded action set : " + action);
return this;
}
public Davinci withDownloadProgressChangedAction(UnityAction<int> action)
{
this.onDownloadProgressChange = action;
if (enableLog)
Debug.Log("[Davinci] On download progress changed action set : " + action);
return this;
}
public Davinci withLoadedAction(UnityAction action)
{
this.OnLoadedAction = action;
if (enableLog)
Debug.Log("[Davinci] On loaded action set : " + action);
return this;
}
public Davinci withErrorAction(UnityAction<string> action)
{
this.onErrorAction = action;
if (enableLog)
Debug.Log("[Davinci] On error action set : " + action);
return this;
}
public Davinci withEndAction(UnityAction action)
{
this.onEndAction = action;
if (enableLog)
Debug.Log("[Davinci] On end action set : " + action);
return this;
}
#endregion
/// <summary>
/// Show or hide logs in console.
/// </summary>
/// <param name="enable">'true' for show logs in console.</param>
/// <returns></returns>
public Davinci setEnableLog(bool enableLog)
{
this.enableLog = enableLog;
if (enableLog)
Debug.Log("[Davinci] Logging enabled : " + enableLog);
return this;
}
/// <summary>
/// Set the sprite of image when davinci is downloading and loading image
/// </summary>
/// <param name="loadingPlaceholder">loading texture</param>
/// <returns></returns>
public Davinci setLoadingPlaceholder(Texture2D loadingPlaceholder)
{
this.loadingPlaceholder = loadingPlaceholder;
if (enableLog)
Debug.Log("[Davinci] Loading placeholder has been set.");
return this;
}
/// <summary>
/// Set image sprite when some error occurred during downloading or loading image
/// </summary>
/// <param name="errorPlaceholder">error texture</param>
/// <returns></returns>
public Davinci setErrorPlaceholder(Texture2D errorPlaceholder)
{
this.errorPlaceholder = errorPlaceholder;
if (enableLog)
Debug.Log("[Davinci] Error placeholder has been set.");
return this;
}
/// <summary>
/// Enable cache
/// </summary>
/// <returns></returns>
public Davinci setCached(bool cached)
{
this.cached = cached;
if (enableLog)
Debug.Log("[Davinci] Cache enabled : " + cached);
return this;
}
/// <summary>
/// Start davinci process.
/// </summary>
public void start()
{
if (url == null)
{
error("Url has not been set. Use 'load' funtion to set image url.");
return;
}
try
{
Uri uri = new Uri(url);
this.url = uri.AbsoluteUri;
}
catch (Exception ex)
{
error("Url is not correct.");
return;
}
if (rendererType == RendererType.none || targetObj == null)
{
error("Target has not been set. Use 'into' function to set target component.");
return;
}
if (enableLog)
Debug.Log("[Davinci] Start Working.");
if (loadingPlaceholder != null)
SetLoadingImage();
if (onStartAction != null)
onStartAction.Invoke();
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
uniqueHash = CreateMD5(url);
if (underProcessDavincies.ContainsKey(uniqueHash))
{
Davinci sameProcess = underProcessDavincies[uniqueHash];
sameProcess.onDownloadedAction += () =>
{
if (onDownloadedAction != null)
onDownloadedAction.Invoke();
loadSpriteToImage();
};
}
else
{
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.");
var www = new WWW(url);
while (!www.isDone)
{
if (www.error != null)
{
error("Error while downloading the image : " + www.error);
yield break;
}
progress = Mathf.FloorToInt(www.progress * 100);
if (onDownloadProgressChange != null)
onDownloadProgressChange.Invoke(progress);
if (enableLog)
Debug.Log("[Davinci] Downloading progress : " + progress + "%");
yield return null;
}
if (www.error == null)
File.WriteAllBytes(filePath + uniqueHash, www.bytes);
www.Dispose();
www = null;
if (onDownloadedAction != null)
onDownloadedAction.Invoke();
loadSpriteToImage();
underProcessDavincies.Remove(uniqueHash);
}
private void loadSpriteToImage()
{
progress = 100;
if (onDownloadProgressChange != null)
onDownloadProgressChange.Invoke(progress);
if (enableLog)
Debug.Log("[Davinci] Downloading progress : " + progress + "%");
if (!File.Exists(filePath + uniqueHash))
{
error("Loading image file has been failed.");
return;
}
StopAllCoroutines();
StartCoroutine(ImageLoader());
}
private void SetLoadingImage()
{
switch (rendererType)
{
case RendererType.renderer:
Renderer renderer = targetObj.GetComponent<Renderer>();
renderer.material.mainTexture = loadingPlaceholder;
break;
case RendererType.uiImage:
Image image = targetObj.GetComponent<Image>();
Sprite sprite = Sprite.Create(loadingPlaceholder,
new Rect(0, 0, loadingPlaceholder.width, loadingPlaceholder.height),
new Vector2(0.5f, 0.5f));
image.sprite = sprite;
break;
}
}
private IEnumerator ImageLoader(Texture2D texture = null)
{
if (enableLog)
Debug.Log("[Davinci] Start loading image.");
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;
if (targetObj != null)
switch (rendererType)
{
case RendererType.renderer:
Renderer renderer = targetObj.GetComponent<Renderer>();
if (renderer == null || renderer.material == null)
break;
renderer.material.mainTexture = texture;
float maxAlpha;
if (fadeTime > 0 && renderer.material.HasProperty("_Color"))
{
color = renderer.material.color;
maxAlpha = color.a;
color.a = 0;
renderer.material.color = color;
float time = Time.time;
while (color.a < maxAlpha)
{
color.a = Mathf.Lerp(0, maxAlpha, (Time.time - time) / fadeTime);
if (renderer != null)
renderer.material.color = color;
yield return null;
}
}
break;
case RendererType.uiImage:
MaskableGraphic image = null;
if (targetObj.GetComponent<RawImage>())
{
image = targetObj.GetComponent<RawImage>();
targetObj.GetComponent<RawImage>().texture = texture;
}
else if (targetObj.GetComponent<Image>())
{
image = targetObj.GetComponent<Image>();
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
targetObj.GetComponent<Image>().sprite = sprite;
}
if (image == null)
break;
color = image.color;
maxAlpha = color.a;
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;
}
if (OnLoadedAction != null)
OnLoadedAction.Invoke();
if (enableLog)
Debug.Log("[Davinci] Image has been loaded.");
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())
{
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();
}
}
private void error(string message)
{
success = false;
if (enableLog)
Debug.LogError("[Davinci] Error : " + message);
if (onErrorAction != null)
onErrorAction.Invoke(message);
if (errorPlaceholder != null)
StartCoroutine(ImageLoader(errorPlaceholder));
else finish();
}
private void finish()
{
if (enableLog)
Debug.Log("[Davinci] Operation has been finished.");
if (!cached)
{
try
{
File.Delete(filePath + uniqueHash);
}
catch (Exception ex)
{
if (enableLog)
Debug.LogError($"[Davinci] Error while removing cached file: {ex.Message}");
}
}
if (onEndAction != null)
onEndAction.Invoke();
Invoke("destroyer", 0.5f);
}
private void destroyer()
{
Destroy(gameObject);
}
/// <summary>
/// Clear a certain cached file with its url
/// </summary>
/// <param name="url">Cached file url.</param>
/// <returns></returns>
public static void ClearCache(string 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}");
}
}
/// <summary>
/// Clear all davinci cached files
/// </summary>
/// <returns></returns>
public static void ClearAllCachedFiles()
{
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}");
}
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b9b77a5c7e324f6499d232d9e031ae96
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+145
View File
@@ -0,0 +1,145 @@
using System;
using System.Security.Cryptography;
using System.Text;
public class EncrypHelper
{
/// <summary>
///静态无参构造
/// </summary>
static EncrypHelper()
{
//默认的密钥
SecretKey = "";
}
/// <summary>
/// 使用SHA256加密字符串
/// </summary>
/// <param name="Source"></param>
/// <returns></returns>
public static string EncrypToSHA(string Source)
{
SHA256Managed sha256 = new SHA256Managed();
byte[] s = UTF8Encoding.UTF8.GetBytes(Source);
byte[] t = sha256.ComputeHash(s);
return Convert.ToBase64String(t);
}
/// <summary>
/// 缺省的密钥
/// </summary>
public static readonly string SecretKey;
/// <summary>
/// 使用缺省密钥字符串加密string
/// </summary>
/// <param name="original">明文</param>
/// <returns>密文</returns>
public static string Encrypt(string original)
{
return Encrypt(original, SecretKey);
}
/// <summary>
/// 使用缺省密钥字符串解密string
/// </summary>
/// <param name="original">密文</param>
/// <returns>明文</returns>
public static string Decrypt(string original)
{
try
{
return Decrypt(original, SecretKey, System.Text.Encoding.Default);
}
catch (Exception)
{
}
return string.Empty;
}
/// <summary>
/// 使用给定密钥字符串加密string
/// </summary>
/// <param name="original">原始文字</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static string Encrypt(string original, string key)
{
byte[] buff = System.Text.Encoding.Default.GetBytes(original);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return Convert.ToBase64String(Encrypt(buff, kb));
}
/// <summary>
/// 使用给定密钥字符串解密string
/// </summary>
/// <param name="original">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string Decrypt(string original, string key)
{
return Decrypt(original, key, System.Text.Encoding.Default);
}
/// <summary>
/// 使用给定密钥字符串解密string,返回指定编码方式明文
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>明文</returns>
public static string Decrypt(string encrypted, string key, Encoding encoding)
{
byte[] buff = Convert.FromBase64String(encrypted);
byte[] kb = System.Text.Encoding.Default.GetBytes(key);
return encoding.GetString(Decrypt(buff, kb));
}
/// <summary>
/// 生成MD5摘要
/// </summary>
/// <param name="original">数据源</param>
/// <returns>摘要</returns>
public static byte[] MakeMd5(byte[] original)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
byte[] keyhash = hashmd5.ComputeHash(original);
hashmd5 = null;
return keyhash;
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] Encrypt(byte[] original, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMd5(key);
des.Mode = CipherMode.ECB;
return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
}
/// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] Decrypt(byte[] encrypted, byte[] key)
{
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Key = MakeMd5(key);
des.Mode = CipherMode.ECB;
return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a9ee2af5e9deb0c47b98031e6d4ec657
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+11 -9
View File
@@ -359,10 +359,13 @@ namespace Assets.Scripts
}
public static Dictionary<string, Texture> propTextureCache = new Dictionary<string, Texture>();
public delegate Coroutine StartCoroutine(IEnumerator routine);
public static void DisplayImage(MaskableGraphic img, string url, bool cache = false,Dictionary<string,Texture> caches = null)
public static void DisplayImage(MaskableGraphic img, string url, bool cache = true,Dictionary<string,Texture> caches = null)
{
if (string.IsNullOrEmpty(url))
return;
Davinci.get().load(url).setCached(cache).into(img).setFadeTime(0).start();
return;
//可以考虑缓存到硬盘里
//Texture2D tex = null;
//byte[] fileData;
@@ -407,6 +410,8 @@ namespace Assets.Scripts
{
if (string.IsNullOrEmpty(url))
return;
Davinci.get().load(url).setCached(true).into(img).setFadeTime(0).start();
return;
if (caches!=null)
{
if (caches.ContainsKey(url))
@@ -494,14 +499,11 @@ namespace Assets.Scripts
}
else
{
Task.Run(() =>
{
var fs = File.Create(filepath);
fs.Write(request.downloadHandler.data, 0, request.downloadHandler.data.Length);
fs.Close();
fs.Dispose();
fs = null;
});
var fs = File.Create(filepath);
fs.Write(request.downloadHandler.data, 0, request.downloadHandler.data.Length);
fs.Close();
fs.Dispose();
request.Dispose();
}
}
if (onCompleted != null)