Unity practical function: store local files

   It is a relatively common small function. Many times, we need to use this function because we need to store some data at runtime in other file formats, such as txt text or png images.

  The writing method is also relatively simple, the specific function is as follows:

    /// <summary>
    /// 编辑器模式的文本存储
    /// </summary>
    /// <param name="_path">路径,类似于Assets/Resources</param>
    /// <param name="_name">名字,需要带后缀</param>
    /// <param name="_str">存储的文本</param>
    void WindowsSave(string _path, string _name, string _str)
    {
#if UNITY_EDITOR
        System.IO.Directory.CreateDirectory(_path);
        using (System.IO.StreamWriter writer = System.IO.File.CreateText(_path + "/" + _name))
            writer.Write(_str);
        UnityEditor.AssetDatabase.Refresh();
#endif
    }

  Let's write a simple test case to test this function

 void Start()
    {
        var _str = "存储测试";
        WindowsSave("Assets/Resources", "测试文本.txt", _str);
    }

  Then start unity to see the test results:

   This function can cooperate with many other functions, such as format conversion, capture test data, edit data storage and other functions, it is quite practical.

Guess you like

Origin blog.csdn.net/Tel17610887670/article/details/131124673