Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to read and write files on uwp platform

Discussion in 'Windows' started by wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk, Jul 6, 2020.

  1. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    I mount the script in unity, read out the location of the GameObject and write it to a TXT file. It works well in editing state. However, an error is reported or does not run after it is released to uwp. Is there a good solution?
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,702
    Where are you writing the file to? On UWP, you are generally only allowed to write to AppData directory (unless you use a file picker and allow the user to select where he wants it written).
     
  3. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    My main problem now is that t
    he code​
    which write in #if NETFX_CORE does not run. Even if I only write a simple output statement, the console will not output anything in debug mode. An error is also reported for asynchronism under uwp~
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,702
    Which Unity version are you on? On Unity versions 5.6+ (anything 3 years or more recent than that), you should use ENABLE_WINMD_SUPPORT define instead.
     
  5. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    My version is 2019.3.6f. upload_2020-7-8_11-10-39.png As shown in the picture, this is what I wrote. But there was no reaction. After being released to the uwp platform, the console still has no output. Is there something wrong with my settings?
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,702
    I'm not exactly sure where System.Diagnostics.Debug.WriteLine goes to. Did you try doing Debug.Log instead?
     
  7. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    I tried. No response.
    Code (CSharp):
    1.     void Update()
    2.     {
    3. #if ENABLE_WINMD_SUPPORT
    4.    Debug.Log("print");
    5. #endif
    6.  
    7.     }
     

    Attached Files:

  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,702
    That code looks correct. Are you saying that after building the project to UWP, this log statement doesn't appear in UnityPlayer.log?
     
  9. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    Its output appears in the UnityPlayer.log . But my question is why it doesn't appear in the development console of uwp? There is also a problem. The C # version of unity does not support async / await asynchronous mechanism (which must be used by storagefile) which was born in 4.5. How to solve the problem of reading and writing?
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,702
    Which development console are you talking about?

    Unity supports .NET 4.5, including async/await just fine (as of a few years ago).

    For writing files on UWP, you can either use System.IO (which should be fully functional), or you can use Windows.Storage APIs like StorageFile/FilePickers as long as you put the code inside #if ENABLE_WINMD_SUPPORT/#endif define.
     
  11. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    development console : upload_2020-7-9_16-12-25.png
    I write this with system io. In UnityPlayer.log There is no debug information output in, and the file is not created successfully.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. public class writefile : MonoBehaviour
    7. {
    8.  
    9.     string gameobjectStr = string.Empty;
    10.     int i = 0;
    11.     public GameObject GameObject1;
    12.     float timer = 0;
    13.  
    14.     public void WriteFileByLine(string file_path, string file_name, string str_info)
    15.  
    16.     {
    17.  
    18.         StreamWriter sw;
    19.  
    20.         if (!File.Exists(file_path + "//" + file_name))
    21.  
    22.         {
    23.  
    24.             sw = File.CreateText(file_path + "//" + file_name);
    25.  
    26.             Debug.Log("Success!" + file_path);
    27.  
    28.         }
    29.  
    30.         else
    31.  
    32.         {
    33.  
    34.             sw = File.AppendText(file_path + "//" + file_name);
    35.  
    36.         }
    37.  
    38.         sw.WriteLine(str_info);
    39.  
    40.         sw.Close();
    41.  
    42.         sw.Dispose();
    43.  
    44.     }
    45.  
    46.  
    47.  
    48.     public void WriteFileByLine1(string file_path, string file_name, string str_info)
    49.  
    50.     {
    51.  
    52.         StreamWriter sw;
    53.  
    54.         FileInfo file_info = new FileInfo(file_path + "//" + file_name);
    55.  
    56.         if (!file_info.Exists)
    57.  
    58.         {
    59.  
    60.             sw = file_info.CreateText();
    61.  
    62.             Debug.Log("Success1!" + file_path);
    63.  
    64.         }
    65.  
    66.         else
    67.  
    68.         {
    69.  
    70.             sw = file_info.AppendText();
    71.  
    72.         }
    73.  
    74.         sw.WriteLine(str_info);
    75.  
    76.         sw.Close();
    77.  
    78.         sw.Dispose();
    79.  
    80.     }
    81.     // Start is called before the first frame update
    82.     void Start()
    83.     {
    84.        
    85.     }
    86.  
    87.     // Update is called once per frame
    88.     void Update()
    89.     {
    90.         if (timer >= 1f)
    91.         {
    92.             timer = 0;
    93.         }
    94.         timer += Time.fixedDeltaTime;
    95.  
    96.         //cameraStr = i + "\t" + camera.transform.position.x.ToString("#0.0000") + "\t" + camera.transform.position.y.ToString("#0.0000") + "\t" + camera.transform.position.z.ToString("#0.0000") + "\t" + timer;
    97.         gameobjectStr = i + "\t" + GameObject1.transform.position.x.ToString("#0.0000") + "\t" + GameObject1.transform.position.y.ToString("#0.0000") + "\t" + GameObject1.transform.position.z.ToString("#0.0000") + "\t" + timer;
    98.         WriteFileByLine(Application.persistentDataPath, "gamebject.txt", gameobjectStr);
    99.         WriteFileByLine1(Application.persistentDataPath, "gamebject1.txt", gameobjectStr);
    100.     }
    101. }
    102.  
     
  12. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    I use Windows.Storage API to write, the result reports the following error.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.IO;
    5. using System.Text;
    6. using System.Threading;
    7. using System.Threading.Tasks;
    8. using UnityEngine;
    9. #if ENABLE_WINMD_SUPPORT
    10. using Windows.Storage;
    11. using Windows.Storage.Streams;
    12. using System.Linq;
    13. #endif
    Error statement:
    Code (CSharp):
    1.     public async Task create_ApplicationData(string fileName)
    2.     {
    3. #if ENABLE_WINMD_SUPPORT
    4.         StorageFolder folder = ApplicationData.Current.LocalFolder;
    5.  
    6.         file_demonstration = await folder.CreateFileAsync(fileName);
    7. #endif
    8.     }
    upload_2020-7-9_16-29-31.png
     
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,702
    Normal logs don't go to the development console. Only errors. Try using Debug.LogError.

    When you say file is not created: where are you looking for it? To be honest, that path building looks wrong: you're using double forward slash. I'd suggest using Path.Combine instead. Once you change that, it should start working.

    Regarding the "GetAwaiter" error: you're missing "using System" namespace at the top of the file.
     
  14. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    Thank you very much. I succeeded
     
  15. wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    wechat_os_Qy09CvCUR4Rh6ThSiydKSgKRk

    Joined:
    Jul 6, 2020
    Posts:
    11
    Excuse me, how to write code in unity and make it become the uwp platform, and then open and run other. Exe programs in uwp?