问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Unity学习21:Application类与文件存储的位置

创作时间:
作者:
@小白创作中心

Unity学习21:Application类与文件存储的位置

引用
1
来源
1.
https://www.zxcms.com/content/wuma2gs331l62u.html

Unity引擎在开发跨平台游戏时,文件存储位置的管理是一个重要的环节。本文将详细介绍Unity中Application类的相关属性,包括dataPath、streamingAssetsPath、persistentDataPath和temporaryCachePath等关键路径的用途、特点和测试方法,并提供相应的C#代码示例。

1 Unity是一个跨平台的引擎

Unity输出的游戏包可以适应各种平台,比如Android、iOS和一些主机游戏平台。在开发过程中,了解如何使用Application类去读写文件是非常重要的。

1.1 使用 Application类,去读写文件

Application类提供了多个属性来获取不同的文件路径,例如:

  • Application.dataPath
  • Application.persistentDataPath

这些属性可以帮助开发者确定文件存储的位置,从而实现文件的读写操作。

1.2 路径特点

1.2.1 相对位置/相对路径

dataPath和streamingAssetsPath这两个属性的返回值一般是相对于程序安装目录的位置。它们适用于在多平台移植中设置要读取外部数据文件的路径,并且这两个文件夹是只读的。

1.2.2 固定位置/绝对路径

persistentDataPath和temporaryCachePath这两个属性的返回值一般是程序所在平台的固定位置。对于不同的平台,其位置是不一样的,适合存放程序运行过程中产生的一些数据文件。这些路径是可读可写的。

1.3 测试方法,仍然挂一个C#脚本在gb上

2 游戏数据文件夹路径(只读)Application.dataPath

2.1 Application.dataPath

此属性用于返回程序的数据文件所在文件夹的路径(只读)。返回路径为相对路径,不同的游戏平台的数据文件保存路径不同。

在Windows的Unity编辑器下,这个文件夹路径其实就是Unity编辑器的工程Assets目录路径。

2.2 跨平台时不同,相对路径需要拼接外部路径

Unity可以跨平台,Application.dataPath在不同的平台下的路径是不同的:

  1. Windows的Unity编辑器下是Unity编辑器的工程Assets目录路径
  2. Win player:<包含可执行文件的文件夹路径>/Data
  3. Mac player:<应用程序路径>/<AppName.app>/Data

2.3 特点

  1. 只读
  2. 加密+压缩

2.4 测试

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ApplicationTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Application.dataPath" + Application.dataPath);
        Debug.Log("Application.dataPath" + Application.dataPath + "/Readme");
    }

    // Update is called once per frame
    void Update()
    {
    }
}

3 Application.streamingAssetsPath (只读)

3.1 Application.streamingAssetsPath

此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一下外部数据文件的路径。在Windows的Unity下,这个文件夹路径其实就是Unity编辑器的工程Assets目录路径下一个子文件夹。

3.2 在Unity的编辑器默认是没有的,也不显示

但是,在Unity的编辑器默认是没有的,也不显示。需要手动创建。需要在工程里手动创建这个文件夹,Application.streamingAssetsPath。

3.3 特点

  1. 只读
  2. 加密+压缩

3.4 测试

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ApplicationTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Application.dataPath" + Application.dataPath);
        Debug.Log("Application.dataPath" + Application.dataPath + "/Readme");
        Debug.Log("Application.persistentDataPath" + Application.persistentDataPath);
        Debug.Log("Application.streamingAssetsPath" + Application.streamingAssetsPath);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

4 游戏持久化文件路径 persistentDataPath (可读可写)

4.1 persistentDataPath

持久化文件路径。persistentDataPath:此属性用于返回一个持久化数据存储目录的路径(只读),可以在该路径下存储持久化的数据文件。对于同一平台,在不同程序中调用此属性时,其返回值是相同的,但是在不同的运行平台下,其返回值会不一样。

4.2 特点

  1. 可读,可写
  2. 可见,不加密,不压缩,方便阅读和拷贝

存储的文件并不在Unity项目的Assets目录中。可以看到下面的测试结果,这个persistantDataPath是存储在C盘的,也是跨平台的。

4.3 测试

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ApplicationTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Application.dataPath" + Application.dataPath);
        Debug.Log("Application.dataPath" + Application.dataPath + "/Readme");
        Debug.Log("Application.persistentDataPath" + Application.persistentDataPath);
        Debug.Log("Application.streamingAssetsPath" + Application.streamingAssetsPath);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

5 游戏的临时文件夹 Application.temporaryCachePath

5.1 Application.temporaryCachePath

此属性用于返回一个临时数据的缓存目录(只读)。对于同一平台,在不同程序中调用此属性时,其返回值是相同的,但是在不同的运行平台下,其返回值是不一样的。游戏读写文件时的临时保存文件夹,可能就是类缓存之类的。

5.2 测试

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ApplicationTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Application.dataPath" + Application.dataPath);
        Debug.Log("Application.dataPath" + Application.dataPath + "/Readme");
        Debug.Log("Application.persistentDataPath" + Application.persistentDataPath);
        Debug.Log("Application.streamingAssetsPath" + Application.streamingAssetsPath);
        Debug.Log("Application.temporaryCachePath" + Application.temporaryCachePath);
    }

    // Update is called once per frame
    void Update()
    {
    }
}
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号