Unity 脚本接口值Path

来源:互联网 发布:中小学网络答题活动 编辑:程序博客网 时间:2024/06/01 10:17

前言

在做游戏或者应用的时候,我们往往会考虑到本地存储数据。但是本地存储数据的时候我们需要考虑到路径的正确性。我们通常会使用Path类中封装的方法来实现。

Path

Path类位于 System.IO;程序集中,主要用于处理与路径相关的字符串。
Path中的方法和字段:

namespace System.IO{    [ComVisible(true)]    public static class Path    {        public static readonly char AltDirectorySeparatorChar;        public static readonly char DirectorySeparatorChar;        public static readonly char PathSeparator;        public static readonly char VolumeSeparatorChar;        public static string ChangeExtension(string path, string extension);        public static string Combine(string path1, string path2);        public static string GetDirectoryName(string path);        public static string GetExtension(string path);        public static string GetFileName(string path);        public static string GetFileNameWithoutExtension(string path);        public static string GetFullPath(string path);        public static char[] GetInvalidFileNameChars();        public static char[] GetInvalidPathChars();        public static string GetPathRoot(string path);        public static string GetRandomFileName();        public static string GetTempFileName();        public static string GetTempPath();        public static bool HasExtension(string path);        public static bool IsPathRooted(string path);    }}

一些常用的方法

    void Start () {        string path = "D:/Test/A/B/C.jpg";        Debug.Log(Path.GetDirectoryName(path));        Debug.Log(Path.GetFullPath(path));        Debug.Log(Path.GetExtension(path));        Debug.Log(Path.GetFileNameWithoutExtension(path));        Debug.Log(Path.GetFileName(path));    }

输出:

D:/Test/A/BD:\Test\A\B\C.jpg.jpgCC.jpg

通过名字对于后面三个方法比较好理解,对于前两个函数进行仔细分析。

Path.GetDirectoryName(path)
获取到路径除了文件名和文件扩展名的其他内容,也就是获得文件夹的路径。

Path.GetFullPath(path)
获得完整路径,包括文件名和文件扩展名,但是按照了运行系统的习惯进行了修改。