现在的位置: 首页 > 综合 > 正文

Unity3d资源写入Android内置存储卡

2017年02月16日 ⁄ 综合 ⁄ 共 3184字 ⁄ 字号 评论关闭

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

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

最近一次对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  {

	/// <summary>
	/// 删除文件
	/// </summary>
	/// <param name="path">Path.</param>
	/// <param name="name">Name.</param>
	
	public static void DeleteFile(string path,string name)
	{
		File.Delete(path+"/"+ name);
	}

	/// <summary>
	/// 删除指定目录及其所有子目录
	/// </summary>
	/// <param name="directoryPath">指定目录的绝对路径</param>
	public static void DeleteDirectory(string directoryPath)
	{
		if (IsExistDirectory(directoryPath))
		{
			Directory.Delete(directoryPath, true);
		}
	}


	/// <summary>
	/// Creates the directory.
	/// </summary>
	/// <param name="directoryPath">Directory path.</param>
	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);
	}
	/// <summary>
	/// Md5file the specified file.
	/// </summary>
	/// <param name="file">File.</param>
	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;
		}
	}


	/// <summary>
	/// 获取指定目录中所有文件列表
	/// </summary>
	/// <param name="directoryPath">指定目录的绝对路径</param>        
	public static string[] GetFileNames(string directoryPath)
	{
		//如果目录不存在,则抛出异常
		if (!IsExistDirectory(directoryPath))
		{
			throw new FileNotFoundException();
		}
		
		//获取文件列表
  		return Directory.GetFiles(directoryPath);
 	}//获取文件列
	/// <summary>
	/// 创建文件
	/// </summary>
	

	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 ();
	}

}

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

抱歉!评论已关闭.