Parcourir la source

updated card images to webp, added system to load them from web and save locally

mbunch_kascope il y a 2 ans
Parent
commit
b6d2d144de

+ 160 - 15
Assets/Scripts/StreamingAssetsUtility.cs

@@ -4,6 +4,14 @@ using System.IO;
 using System;
 using System.Text;
 using System.Threading.Tasks;
+using UnityEngine.Networking;
+using UnityEditor.PackageManager;
+using System.Security.Policy;
+using WebP;
+using UnityEngine.Rendering;
+using System.Data;
+using UnityEngine.XR;
+
 public class StreamingAssetsUtility
 {
     public static async Task<byte[]> ReadFile(string path)
@@ -27,22 +35,101 @@ public class StreamingAssetsUtility
 
     public static async Task<Sprite> GetSprite(string fileName, bool isCard = false, bool isLauncher = false)
     {
-        string path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
-        Debug.Log(path);
-        if (!File.Exists(path))
+        string path = "";
+
+        if (isCard)
         {
-            path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
+            if (fileName.Contains("-token"))
+            {
+                return await GetTokenImageData(Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/"));
+            }
+            else
+            {
+                path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.webp").Replace("\\", "/");
+
+                if (!File.Exists(path))
+                {
+                    return await GetCardImageData(fileName, path);
+                }
+                else 
+                {
+                    return await GetCardImageDataLocal(path);
+                }
+            }
+        }
+        else
+        {
+            return await GetSpriteImage(fileName, isLauncher);
         }
 
-        if (isCard)
+        /*    if (isCard)
         {
-            path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/");
+            if (fileName.Contains("-token"))
+            {
+                return await GetTokenImageData(Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.png").Replace("\\", "/"));
+            }
+            else
+            {
+                
+            }            
+        }
+        else
+        {
+            path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
 
             if (!File.Exists(path))
+                path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
+
+            if (File.Exists(path))
             {
-                path = Path.Combine(GetStreamingAssetPath(isLauncher), $"Card/{fileName}.jpg").Replace("\\", "/");
+                byte[] imageBuff = await ReadFile(path);
+                Texture2D tex = BinaryToTexture(imageBuff);
+
+                Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
+
+                return sprite;
             }
         }
+        
+        await Task.Yield();
+
+        if (File.Exists(path))
+        {
+            byte[] imageBuff = await ReadFile(path);
+            Texture2D tex;
+            Sprite sprite = null;
+
+            if (isCard)
+            {
+                tex = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
+
+                if (lError == WebP.Error.Success)
+                {
+                    sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
+                }
+                else
+                {
+                    Debug.LogError("Webp Load Error : " + lError.ToString());
+                }
+            }
+            else
+            {
+                tex = BinaryToTexture(imageBuff);
+                sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
+            }
+
+            return sprite;
+        }*/
+
+        return null;
+    }
+
+    public static async Task<Sprite> GetSpriteImage(string fileName, bool isLauncher = false)
+    {
+        string path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.jpg").Replace("\\", "/");
+
+        if(!File.Exists(path))
+            path = Path.Combine(GetStreamingAssetPath(isLauncher), $"{fileName}.png").Replace("\\", "/");
 
         if (File.Exists(path))
         {
@@ -56,25 +143,83 @@ public class StreamingAssetsUtility
 
         return null;
     }
-    #endregion
 
-    public static bool IsCardExists(CEntity_Base cEntity_Base)
+    public static async Task<Sprite> GetTokenImageData(string path)
     {
-        string path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
-
         if (File.Exists(path))
         {
-            return true;
+            byte[] imageBuff = await ReadFile(path);
+            Texture2D tex = BinaryToTexture(imageBuff);
+
+            Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
+            return sprite;
         }
 
-        path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.jpg").Replace("\\", "/");
+        return null;
+    }
 
+    public static async Task<Sprite> GetCardImageDataLocal(string path)
+    {
         if (File.Exists(path))
         {
-            return true;
+            byte[] imageBuff = await ReadFile(path);
+            Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(imageBuff, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
+
+            if (lError == WebP.Error.Success)
+            {
+                Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
+
+                return sprite;
+            }
+        }
+
+        return null;
+    }
+
+    public static async Task<Sprite> GetCardImageData(string fileName, string filePath)
+    {
+        string urlPath = $"https://raw.githubusercontent.com/TakaOtaku/Digimon-Card-App/main/src/assets/images/cards/{fileName}.webp";
+
+        UnityWebRequest webReq_CardImage = UnityWebRequest.Get(urlPath);
+        UnityWebRequestAsyncOperation operation = webReq_CardImage.SendWebRequest();
+
+        while (!operation.isDone)
+        {
+            await Task.Yield();
         }
 
-        return false;
+        Debug.Log($"WebRequest isDone: {fileName}");
+        if (webReq_CardImage.result == UnityWebRequest.Result.ConnectionError)
+            return null;
+        else if (webReq_CardImage.result == UnityWebRequest.Result.ProtocolError)
+            return null;
+        else
+        {
+            File.WriteAllBytes(filePath, webReq_CardImage.downloadHandler.data);
+
+            Texture2D texture = Texture2DExt.CreateTexture2DFromWebP(webReq_CardImage.downloadHandler.data, lMipmaps: true, lLinear: false, lError: out WebP.Error lError);
+
+            if (lError == WebP.Error.Success)
+            {
+                Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
+
+                return sprite;
+            }
+
+            return null;
+        }
+    }
+
+    #endregion
+
+    public static bool IsCardExists(CEntity_Base cEntity_Base)
+    {
+        string path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.webp").Replace("\\", "/");
+
+        if (cEntity_Base.CardSpriteName.Contains("token"))
+            path = Path.Combine(GetStreamingAssetPath(false), $"Card/{cEntity_Base.CardSpriteName}.png").Replace("\\", "/");
+
+        return File.Exists(path);
     }
 
     #region テキストファイルの取得

+ 11 - 1
Packages/manifest.json

@@ -1,5 +1,6 @@
 {
   "dependencies": {
+	"com.netpyoung.webp": "https://github.com/netpyoung/unity.webp.git?path=unity_project/Assets/unity.webp#0.3.10",
     "com.unity.2d.sprite": "1.0.0",
     "com.unity.cinemachine": "2.8.9",
     "com.unity.editorcoroutines": "1.0.0",
@@ -42,5 +43,14 @@
     "com.unity.modules.vr": "1.0.0",
     "com.unity.modules.wind": "1.0.0",
     "com.unity.modules.xr": "1.0.0"
-  }
+  },
+  "scopedRegistries": [
+    {
+      "name": "Unity NuGet",
+      "url": "https://unitynuget-registry.azurewebsites.net",
+      "scopes": [
+        "org.nuget"
+      ]
+    }
+  ]
 }

+ 7 - 0
Packages/packages-lock.json

@@ -1,5 +1,12 @@
 {
   "dependencies": {
+    "com.netpyoung.webp": {
+      "version": "https://github.com/netpyoung/unity.webp.git?path=unity_project/Assets/unity.webp#0.3.10",
+      "depth": 0,
+      "source": "git",
+      "dependencies": {},
+      "hash": "a502ba690d177f11db4a01043620948229f72574"
+    },
     "com.unity.2d.sprite": {
       "version": "1.0.0",
       "depth": 0,

+ 2 - 2
ProjectSettings/PackageManagerSettings.asset

@@ -1,3 +1,3 @@
 version https://git-lfs.github.com/spec/v1
-oid sha256:6a05ef78104cc0f3d23af145e478955bf980709437e91bfbddb7196457eb80c5
-size 1046
+oid sha256:d834052b4d5baa0ba05b91226c407d1af2ef81e6bafea9036bb0905130771cfc
+size 1172