unity3d android存储文件,Unity3d资源写入Android内置存储卡

还是在研究更新,发现如果你打算开始做一个游戏,在出来详细的策划后,接下来就是资源收集和整理,游戏更新大部分更新的都是资源,

所以应该在做游戏之前就想出一套很好的资源管理,使用,更新的方案,不能等游戏的导出包达到一定程度再合计资源管理,坑啊

最近一次对Assetbundle进行打包写入手机Application.persistentDataPath目录下面的时候发现,竟然占据了15M空间,逆天的大啊,我就受不了了

由于接触unity3d时间很短,也没有接受系统的培训,一路上都是自己和团队的小伙伴们在探索,我发现其他的游戏都会在我的手机的内部存储上创建属于游戏自己的

文件夹,里面存放着各种各样的东西,羡慕呀,于是我也想做,就在网上找方法,发现Java貌似可以办到,可是本人大学的Java是一门选修课,丫的老师是个菜鸟

害的我啥也不会,我就想C#来实现这个功能,我就到论坛上面去问,发现,没人鸟我,不知道问题是不是问的太菜b了,就还是自己找方法,下班之前没想明白,郁闷地回家了

第二天就还在找,参考了大神们的博文,下面一篇对于路径给予我了很大帮助,献上链接:点击打开链接,根据他对于安卓手机的路径提示,我就使用了“/storage/sdcard0/”这是手机内置存储卡的路径,参考圣殿上面大神的文献点击打开链接,System.IO.Directory.CreateDirectory("d:/11"); 创建文件目录,这边是创建目录的方法,

最后通过相对应的File相关方法,就可以将资源写到手机的内置存储开的自定义位置,真棒,共计耗时半天,我这个问题就解决了,下面奉上我的FileHelper.cs供大家参考使用

using UnityEngine;

using System.Collections;

using System.IO;

using System.Collections.Generic;

using System.Text;

using System;

public class FileHelper {

///

/// 删除文件

///

/// Path.

/// Name.

public static void DeleteFile(string path,string name)

{

File.Delete(path+"/"+ name);

}

///

/// 删除指定目录及其所有子目录

///

/// 指定目录的绝对路径

public static void DeleteDirectory(string directoryPath)

{

if (IsExistDirectory(directoryPath))

{

Directory.Delete(directoryPath, true);

}

}

///

/// Creates the directory.

///

/// Directory path.

public static void CreateDirectory(string directoryPath)

{

//如果目录不存在则创建该目录

if (!IsExistDirectory(directoryPath))

{

//Debug.Log("path doesnot exit");

Directory.CreateDirectory(directoryPath);

}

}

public static bool IsExistDirectory(string directoryPath)

{

return Directory.Exists(directoryPath);

}

///

/// Md5file the specified file.

///

/// File.

public static string md5file(string file) {

try {

FileStream fs = new FileStream(file, FileMode.Open);

System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

byte[] retVal = md5.ComputeHash(fs);

fs.Close();

StringBuilder sb = new StringBuilder();

for (int i = 0; i < retVal.Length; i++) {

sb.Append(retVal[i].ToString("x2"));

}

return sb.ToString();

} catch (Exception ex) {

throw new Exception("md5file() fail, error:" + ex.Message);

}

}

public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)

{

//如果目录不存在,则抛出异常

if (!IsExistDirectory(directoryPath))

{

throw new FileNotFoundException();

}

try

{

if (isSearchChild)

{

return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);

}

else

{

return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);

}

}

catch (IOException ex)

{

throw ex;

}

}

///

/// 获取指定目录中所有文件列表

///

/// 指定目录的绝对路径

public static string[] GetFileNames(string directoryPath)

{

//如果目录不存在,则抛出异常

if (!IsExistDirectory(directoryPath))

{

throw new FileNotFoundException();

}

//获取文件列表

return Directory.GetFiles(directoryPath);

}//获取文件列

///

/// 创建文件

///

public static void CreateModuleFile(string writepath,string name,byte[] info,int length){

Debug.Log (writepath + "//" + name);

Stream sw = null;

FileInfo t = new FileInfo (writepath + "/" + name);

//stringToEdit +="t.Exists=="+ t.Exists + "\n";

if (!t.Exists) {

sw = t.Create ();

} else {

return;

}

sw.Write (info, 0, length);

sw.Close ();

sw.Dispose ();

}

}

这个脚本里面有一些关于文件操作的函数,很容易使用,哈哈,这都是我东拼西凑的。

转载自:心林相夕

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D中,我们可以使用C#的文件读写方法来实现在安卓上读取文件的功能。 首先,我们需要将要读取的文件放置在安卓设备的存储空间中。可以使用Android的API方法来实现文件存储。例如,可以使用Unity的Application.persistentDataPath属性来获得设备存储空间的路径,然后使用System.IO命名空间下的File类来创建和写入文件。 接下来,要在安卓设备上读取文件,我们需要使用System.IO命名空间下的File类和StreamReader类。首先,通过File.OpenText方法打开文件,该方法接受文件的完整路径作为参数。然后,使用StreamReader来读取文件内容。StreamReader的ReadLine方法可以按行读取文件内容。 读取完文件内容后,记得要关闭StreamReader和File,释放资源。 以下是一个简单示例代码,展示了在Unity3D中如何读取安卓设备上的文本文件: ```c# using System.IO; using UnityEngine; public class AndroidFileReader : MonoBehaviour { void Start() { string filePath = Path.Combine(Application.persistentDataPath, "example.txt"); ReadFile(filePath); } private void ReadFile(string path) { if (File.Exists(path)) { using (StreamReader sr = File.OpenText(path)) { string line; while ((line = sr.ReadLine()) != null) { Debug.Log(line); } } } else { Debug.Log("File does not exist."); } } } ``` 上述代码将会从设备存储空间中读取名为"example.txt"的文件,并逐行输出文件内容到Unity控制台中。 当在Unity3D中开发安卓应用时,很重要的一点是要确保获取到的路径是设备存储空间的路径,即通过Application.persistentDataPath属性获得的路径。这样才能保证在不同的安卓设备上都能正常读取文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值