Search Unity

Why my list is not getting updated even after clicking button multiple times?

Discussion in 'Scripting' started by zyonneo, May 22, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Hi I have a list which contains information of files from a path.After deleting files ,the list gets not get refreshed.When also clicking the button to show the list,it does not get updated.
    If I close my app and click the button to list map,the list show all the files correctly.
    Code (CSharp):
    1.  public void ListMap()
    2.     {
    3.         Listobject.Clear();
    4.      
    5.         panellist.SetActive(true);
    6.         string mainpath = Application.persistentDataPath;
    7.         DirectoryInfo dir = new DirectoryInfo(mainpath);
    8.  
    9.  
    10.            // infos = dir.GetFileSystemInfos("*.worldmap").OrderBy(i => i.CreationTime).ToArray();
    11.           infos = dir.GetFileSystemInfos("*.json").OrderBy(i => i.CreationTime).ToArray();
    12.           infomaps = dir.GetFileSystemInfos("*.worldmap").OrderBy(i => i.CreationTime).ToArray();
    13.      
    14.         for(int i = 1;i<=infos.Length;i++)
    15.         {
    16.          
    17.          
    18.             if (Parentcontent.childCount > 0)
    19.             {
    20.                 GameObject gg = Parentcontent.GetChild(i - 1).gameObject;
    21.                 Destroy(gg);
    22.            
    23.             }
    24.  
    25.             lisobj = Instantiate(prefabpanellist);
    26.             Listobject.Add(lisobj);
    27.  
    28.  
    29.  
    30.         }
    31.  
    32.      
    33.  
    34.         for (int i = 0; i < infos.Length; i++)
    35.          
    36.         {
    37.  
    38.  
    39.  
    40.             Listobject[i].transform.SetParent(Parentcontent);
    41.             var index = i;
    42.          
    43.             var button = Listobject[i].transform.GetChild(3).GetComponent<Button>();
    44.             var Loadbutton=Listobject[i].transform.GetChild(2).GetComponent<Button>();
    45.  
    46.             button.onClick.AddListener(() => Deleteinformation(index));
    47.        
    48.  
    49.             var numText = Listobject[i].transform.GetChild(0).GetComponent<Text>();
    50.             numText.text = (i+1).ToString();
    51.      
    52.  
    53.             var filenameText = Listobject[i].transform.GetChild(1).GetComponent<Text>();
    54.             filenameText.text = infos[i].Name;
    55.             string Filename = filenameText.text;
    56.             Loadbutton.onClick.AddListener(() => Load(Filename));
    57.        
    58.         }
    59.  
    60.      
    61.  
    62.    
    63.     }
    64.  
    65.     public void Deleteinformation(int index)
    66.     {
    67.         Debug.Log("Index is " + index);
    68.         string mapnamewithpath = infos[index].Name;
    69.         string mapnam =  Path.GetFileNameWithoutExtension(mapnamewithpath);
    70.  
    71.         //  Debug.Log("Path is " + infos[index].FullName);
    72.  
    73.  
    74.  
    75. #if UNITY_EDITOR
    76.  
    77.         {
    78.             File.Delete(infos[index].FullName);
    79.  
    80.             Debug.Log("Path is " + infos[index].FullName);
    81.             Deletetext(mapnam, "data.txt");
    82.         }
    83.  
    84.  
    85. #else
    86.         {
    87.  
    88.         Debug.Log("Path is " + infomaps[index].FullName);
    89.         // Listobject.RemoveAt(index);
    90.         Destroy(Listobject[index]);
    91.         File.Delete(infos[index].FullName);
    92.         File.Delete(infomaps[index].FullName);
    93.  
    94.         Deletetext(mapnam, "data.txt");
    95.         Deletetext(mapnam, "world.txt");
    96.  
    97.  
    98.         }
    99.  
    100.  
    101. #endif
    102.  
    103.         Debug.Log("Delete Path is " + mapnam);
    104.      
    105.      
    106.         BackMap();
    107.  
    108.  
    109.     }
    110.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    How are you detecting when the file system is changed - you havent posted any code relating to this

    Your ListMap function is set up incorrectly
    You are deleting existing buttons based on the number of files found
    If you have 6 buttons, but only 4 files, you will only delete 4 buttons. You should probably be deleting all buttons
    for(int i = 0;i<Parentcontent.childCount;i++)
    then instantiate by infos.length
     
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    I have added the below method to the above code,how to chk if files have been changed?

    Code (CSharp):
    1.  public void CreateFileWatcher(string path)
    2.     {
    3.         // Create a new FileSystemWatcher and set its properties.
    4.         FileSystemWatcher watcher = new FileSystemWatcher();
    5.         watcher.Path = path;
    6.         /* Watch for changes in LastAccess and LastWrite times, and
    7.            the renaming of files or directories. */
    8.         watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
    9.            | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    10.         // Only watch text files.
    11.         //watcher.Filter = "*.txt";
    12.  
    13.         // Add event handlers.
    14.         watcher.Changed += new FileSystemEventHandler(OnChanged);
    15.         watcher.Created += new FileSystemEventHandler(OnChanged);
    16.         watcher.Deleted += new FileSystemEventHandler(OnChanged);
    17.        // watcher.Renamed += new RenamedEventHandler(OnRenamed);
    18.  
    19.         // Begin watching.
    20.         watcher.EnableRaisingEvents = true;
    21.     }
    22.  
    23.     // Define the event handlers.
    24.     private  void OnChanged(object source, FileSystemEventArgs e)
    25.     {
    26.         // Specify what is done when a file is changed, created, or deleted.
    27.         // Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    28.  
    29.         Debug.Log("File: " + e.FullPath + " " + e.ChangeType);
    30.         string mainpath = Application.persistentDataPath;
    31.         DirectoryInfo dir = new DirectoryInfo(mainpath);
    32.         infos = dir.GetFileSystemInfos("*.json").OrderBy(i => i.CreationTime).ToArray();
    33.         infomaps = dir.GetFileSystemInfos("*.worldmap").OrderBy(i => i.CreationTime).ToArray();
    34.     }
     
  4. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    If I pass the path to the method CreateFileWatcher(path) ...How will it call OnChanged event?Confused with how will it call Onchanged event without giving Correct arguments.
     
    Last edited: May 23, 2019
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You need to call the method like:
    Code (CSharp):
    1.             string path = Application.dataPath + "/Test";
    2.             CreateFileWatcher(path);
    Then when you make a change you will see in the console:
    File: E:\Work\Unity\Decorating\Assets\Test\asdf.txt Deleted

    OnChanged is called by the FileSystemEventHandler