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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Screenshots won't be loaded when built

Discussion in 'Scripting' started by Penny_Lu, Apr 30, 2022.

  1. Penny_Lu

    Penny_Lu

    Joined:
    Oct 28, 2019
    Posts:
    12
    I write a script for saving screenshots and showing them in an album. It runs perfectly in the editor. However, after I built it into exe. files, the screenshots didn't show in the album (I still can find them in the local folder). I can neither delete any of the photos. What is the possible problem? Please help me, thanks a lot!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using UnityEngine.UI;
    6. using System.IO;
    7. using UnityEditor;
    8. using System.Linq;
    9.  
    10. public class AlbumManager : MonoBehaviour
    11. {
    12.    
    13.     public GameObject albumobj;    
    14.     public GameObject alertWindow;
    15.     public GameObject UI;
    16.     GameObject slot;
    17.  
    18.     public Button albumCloseBtn;
    19.     public Button alertBtn;
    20.     public Button deleteBtn;  
    21.    
    22.     bool isOpen = false;
    23.     private int screenshotCount = 0;
    24.     private int fileCount;
    25.     private string screenshotFileName;
    26.     public string slotname;
    27.     private string folderPath;
    28.     DirectoryInfo texturedir;
    29.    
    30.     UnityEngine.Object[] textureobjs;
    31.     Sprite[] sprites;
    32.     Texture2D[] textures;
    33.     String[] textureNames;
    34.  
    35.     static List<int> screenshotlist = new List<int>();
    36.     Dictionary<String, String> dic = new Dictionary<String, String>();
    37.     public List<GameObject> slots = new List<GameObject>();  
    38.          
    39.     void Awake()
    40.     {
    41.         CreateSprites();
    42.         albumCloseBtn.onClick.AddListener(ShowOrHideAlbum);
    43.         alertBtn.onClick.AddListener(CloseAlertWindow);
    44.         deleteBtn.onClick.AddListener(DeleteTextures);
    45.     }
    46.  
    47.     void Start()
    48.      {
    49.         //PlayerPrefs.DeleteAll();      
    50.         folderPath = Application.dataPath + "/Resources/Textures/";
    51.         texturedir = new DirectoryInfo(folderPath);        
    52.         screenshotlist.Add(screenshotCount);
    53.         if (!System.IO.Directory.Exists(folderPath))
    54.             System.IO.Directory.CreateDirectory(folderPath);
    55.         screenshotCount = PlayerPrefs.GetInt("MaxNumber");
    56.         ReplaceImages();          
    57.      }          
    58.  
    59.     void Update()
    60.     {
    61.         if (Input.GetKeyDown(KeyCode.A))
    62.         {
    63.             ShowOrHideAlbum();
    64.         }
    65.         if (Input.GetKeyDown(KeyCode.S))
    66.          {
    67.               fileCount = texturedir.GetFiles("*.png").Length;
    68.               if(fileCount < slots.Count)
    69.               {
    70.                 StartCoroutine(CaptureScreenshot());
    71.               }
    72.               else
    73.               {
    74.                   this.GetComponent<AlbumManager>().alertWindow.SetActive(true);
    75.               }                                  
    76.          }
    77.     }
    78.  
    79.     private IEnumerator CaptureScreenshot()
    80.      {
    81.           screenshotCount ++;
    82.           screenshotlist.Add(screenshotCount);
    83.           PlayerPrefs.SetInt("MaxNumber", screenshotlist.Max());
    84.           screenshotFileName = "Screenshot_" + screenshotCount + ".png";
    85.           ScreenCapture.CaptureScreenshot(folderPath + screenshotFileName);
    86.           yield return null;
    87.         #if UNITY_EDITOR
    88.           AssetDatabase.Refresh();
    89.         #endif
    90.           ReplaceImages();      
    91.      }
    92.  
    93.     public void CreateSprites()
    94.     {
    95.         textureobjs = Resources.LoadAll("Textures", typeof(Texture2D));
    96.         Array.Resize(ref textures, textureobjs.Length);
    97.         Array.Resize(ref sprites, textureobjs.Length);
    98.         Array.Resize(ref textureNames, textureobjs.Length);
    99.         for (int i = 0; i < textureobjs.Length; i++)
    100.         {      
    101.             textures[i] = (Texture2D) textureobjs[i];
    102.             sprites[i] = Sprite.Create(textures[i], new Rect(0f, 0f, textures[i].width, textures[i].height), Vector2.zero, 100.0f);
    103.             sprites[i].name = "Sprite" + i;
    104.             textureNames[i] = textures[i].name;
    105.         }
    106.         ImagesDic();
    107.     }
    108.  
    109.     public void ReplaceImages()
    110.     {
    111.         CreateSprites();
    112.          for (int j = 0; j < slots.Count; j++)
    113.         {
    114.             slots[j].transform.GetChild(0).gameObject.GetComponent<Image>().sprite = null;
    115.             slots[j].transform.GetChild(0).gameObject.SetActive(false);
    116.         }
    117.         for (int i = 0; i < sprites.Length; i++)
    118.         {          
    119.             if (slots[i].transform.GetChild(0).gameObject.activeInHierarchy == false)              
    120.             {
    121.                 slots[i].transform.GetChild(0).gameObject.SetActive(true);
    122.                 slots[i].transform.GetChild(0).gameObject.GetComponent<Image>().sprite = sprites[i];                  
    123.             }    
    124.         }      
    125.     }      
    126.  
    127.     public void ImagesDic()
    128.     {
    129.         dic.Clear();
    130.         for (int i = 0; i < sprites.Length; i++)
    131.         {
    132.             dic.Add(textureNames[i], sprites[i].name);
    133.         }
    134.     }
    135.  
    136.     public void DeleteTextures()
    137.     {      
    138.         slot = GameObject.Find(slotname);
    139.         string slotspritename = slot.transform.GetChild(0).gameObject.GetComponent<Image>().sprite.name;  
    140.         string key = dic.FirstOrDefault(item => item.Value == slotspritename).Key;
    141.         string texturepath = Application.dataPath + "/Resources/Textures/" + key + ".png";
    142.         slot.transform.GetChild(0).gameObject.GetComponent<Image>().sprite = null;
    143.         slot.transform.GetChild(0).gameObject.SetActive(false);
    144.         File.Delete(texturepath);
    145.        
    146.         #if UNITY_EDITOR
    147.             UnityEditor.AssetDatabase.Refresh();
    148.         #endif
    149.         ReplaceImages();
    150.     }
    151.  
    152.     public void ShowOrHideAlbum()
    153.     {
    154.         isOpen = !isOpen;
    155.         albumobj.SetActive(isOpen);
    156.     }
    157.     public void CloseAlertWindow()
    158.     {
    159.         alertWindow.SetActive(false);
    160.     }
    161.  
    162. }
    163.  
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Looks like you're using resources to load the textures. This will work in editor but not in builds. Resources folders are essentially built into an asset bundle on build, so you can't add to them at runtime U less you're running in the editor.

    There are a few different ways to do what you want, I think texture2d has a load image method that takes a byte array that you'll need to read in using stabdard File.IO. Another method is using UnityWebRequest with a local file path url.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
  4. Penny_Lu

    Penny_Lu

    Joined:
    Oct 28, 2019
    Posts:
    12