Unity 脚本接口值Path

前言

在做游戏或者应用的时候,我们往往会考虑到本地存储数据。但是本地存储数据的时候我们需要考虑到路径的正确性。我们通常会使用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/B
D:\Test\A\B\C.jpg
.jpg
C
C.jpg

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值