Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Can save to persistantData folder, can't load back from it on Mac/IOS

Discussion in 'iOS and tvOS' started by JerryComo, Jul 14, 2023.

  1. JerryComo

    JerryComo

    Joined:
    May 23, 2022
    Posts:
    8
    Like the title says, I can save an image from a URL, but I can't retrieve it. I just get a 'cannot resolve destination host' on the mac editor/ios build. This all works fine on PC. The pic is definitely in the folder and the file.exists call comes back true.

    Must be something to do with that mac library path generation.

    Absolutely pulling my hair out on this !

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.IO;
    4. using System;
    5. using System.Collections.Generic;
    6. using UnityEngine.Networking;
    7. using UnityEngine;
    8. using UnityEngine.UI;
    9. using TMPro;
    10. using UnityEngine.SceneManagement;
    11.  
    12. public class loadClass : MonoBehaviour
    13. {
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         AppAssetPath = Application.persistentDataPath;
    18.         getPicFromUrl();
    19.     }
    20.  
    21.     string picUrl = "https://magneticboots.com/images/gamePicSlackers.jpg";
    22.     string picName = "gamePicSlackers.jpg";
    23.     private string AppAssetPath;
    24.     public RawImage myRaw;
    25.  
    26.     void loadIntoTexture()
    27.     {
    28.      
    29.         string imgName = Path.GetFileName(picName);
    30.         Texture2D tex;
    31.         tex = new Texture2D(4, 4, TextureFormat.RGB24, false);
    32.  
    33.         string pathCombine = Path.Combine(AppAssetPath, imgName);
    34.         if (File.Exists(pathCombine))
    35.         {
    36.             print("load that fecker" + pathCombine);
    37.             StartCoroutine(loadImageFastTest(pathCombine, myRaw));
    38.         }
    39.         else
    40.         {
    41.             print("----that fecker aint there" + imgName);
    42.         }
    43.     }
    44.  
    45.     IEnumerator loadImageFastTest(string url, RawImage raw, bool RESIZE = false, int resizeW = 0, int resizeH = 0, bool RESIZE_MIN = false)
    46.     {
    47. // commented out methods that haven't worked either !!
    48.         // UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
    49.         // while(!www.isDone)
    50.         //     yield return www.Send();
    51.         // raw.texture = DownloadHandlerTexture.GetContent(www);
    52.      
    53.  
    54.      
    55.         // Uri baseUri = new Uri(Application.persistentDataPath);
    56.         // Uri myUri = new Uri(baseUri, picName);
    57.         // print(" *****absUri=" + myUri.AbsoluteUri);
    58.  
    59.         // Texture2D tex;
    60.         // tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
    61.         // using (WWW www = new WWW(url))
    62.         // {
    63.         //     yield return www;
    64.  
    65.         //         if (!string.IsNullOrEmpty(www.error))
    66.         //         {
    67.         //             Debug.Log("error="+www.error);
    68.         //         }
    69.  
    70.         //     www.LoadImageIntoTexture(tex);
    71.         //     raw.texture = tex;
    72.         // }
    73.  
    74.        
    75.        
    76. //url="C:/Users/jerry/AppData/LocalLow/mycompany/testLoad4/gamePicSlackers.jpg";   //hardcoded pc
    77. //url = "/Users/jerryComo/Library/Application Support/mycompany/testLoad4/gamePicSlackers.jpg";   //mac url
    78.  
    79.  
    80.         using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(url))
    81.         {
    82.             yield return uwr.SendWebRequest();
    83.  
    84.             if (uwr.isNetworkError || uwr.isHttpError)
    85.             {
    86.                 Debug.Log("error=" + uwr.error);
    87.             }
    88.             else
    89.             {
    90.                 // Get downloaded asset bundle
    91.                 raw.texture = DownloadHandlerTexture.GetContent(uwr);
    92.  
    93.             }
    94.         }
    95.  
    96.  
    97.     }
    98.  
    99.  
    100.  
    101.  
    102.     void getPicFromUrl()
    103.     {
    104.         string tempFileName = Path.GetFileName(picName);
    105.         string pathCombine = Path.Combine(AppAssetPath, tempFileName);
    106.  
    107.         if (File.Exists(pathCombine))
    108.         {
    109.             print("ALREADY THERE---->" + tempFileName);
    110.             loadIntoTexture();
    111.         }
    112.         else
    113.         {
    114.             StartCoroutine(loadPicsFromUrl(tempFileName));
    115.         }
    116.     }
    117.  
    118.     IEnumerator loadPicsFromUrl(string imgName)
    119.     {
    120.         //print("loading "+imgName);
    121.         Texture2D tex;
    122.         tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
    123.  
    124.         string tempFileName = Path.GetFileName(imgName);
    125.         string pathCombine = Path.Combine(AppAssetPath, tempFileName);
    126.  
    127.         print(pathCombine);
    128.  
    129.         if (File.Exists(pathCombine))
    130.         {
    131.             loadIntoTexture();
    132.         }
    133.         else
    134.         {
    135.             print("*** FILE DOESNT EXIST LOCALLY >" + imgName + "<" + Application.persistentDataPath);
    136.  
    137.             using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(picUrl))
    138.             {
    139.  
    140.                 yield return www.SendWebRequest();
    141.  
    142.                 tex = DownloadHandlerTexture.GetContent(www);
    143.  
    144.                 Texture2D newTexture = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
    145.                 Color32[] cols = tex.GetPixels32();
    146.  
    147.                 newTexture.SetPixels32(0, 0, tex.width, tex.height, cols);
    148.  
    149.  
    150.                 newTexture.Apply();
    151.  
    152.                 if (www.error == null)
    153.                 {
    154.                     ///save to persistantdata folder.
    155.                     if (imgName.IndexOf("jpg") != -1)
    156.                     {
    157.                         byte[] bytes = newTexture.EncodeToJPG(100);
    158.                         string saveName = tempFileName;//Path.GetFileName(imgName);
    159.                         pathCombine = Path.Combine(AppAssetPath, saveName);
    160.                         print(pathCombine);
    161.                         File.WriteAllBytes(pathCombine, bytes);
    162.                     }
    163.                     else
    164.                     {
    165.                         byte[] bytes = newTexture.EncodeToPNG();
    166.                         string saveName = tempFileName;//Path.GetFileName(imgName);
    167.                         pathCombine = Path.Combine(AppAssetPath, saveName);
    168.                         File.WriteAllBytes(pathCombine, bytes);
    169.                     }
    170.                 }
    171.                 else
    172.                 {
    173.                     print("ERROR=" + www.error);
    174.                 }
    175.  
    176.  
    177.                 Destroy(newTexture);
    178.                 Destroy(tex);
    179.                 loadIntoTexture();
    180.  
    181.             }
    182.         }
    183.     }
    184. }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    Path is not URL.
    Code (CSharp):
    1. new System.Uri(pathCombine).AbsoluteUri
     
    JerryComo likes this.
  3. JerryComo

    JerryComo

    Joined:
    May 23, 2022
    Posts:
    8
    yes - I tried that earlier in the commented out bits. AbsoluteUri also adds "file://" which isn't required by on IOS.
    My bad for calling it url though. that was wrong.
    It is deffo something to do with the way UWR handles that path on iOS. Even with the path hardcoded it doesn't get it. It *does* work with File.Read but that process is too slow when loading up the jpgs.
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    Yes, that is what you want. file:// prefix makes it an URI that points to a local file and UWR can read it.
    UWR doesn't support paths. I think on Windows it may work by chance because paths there do look a bit like uris, but the proper use is to pass file:// uri.
     
    JerryComo likes this.
  5. JerryComo

    JerryComo

    Joined:
    May 23, 2022
    Posts:
    8
    my bad - I DID have to add "file://" after all . but ALSO was combining persistantDataPath to the filename - completely discounting the project folder name.
     
  6. JerryComo

    JerryComo

    Joined:
    May 23, 2022
    Posts:
    8
    i love that i spent ALL day trying to get this working. ho ho ho. I look forward to remembering this one on my deathbed.

    "I could have been out at the zoo feeding the tapirs...."