unity对本地文件的相关操作

【1】在指定路径下创建文件夹
//在桌面创建文件夹“测试文件夹”,并在此文件夹内创建其子文件夹“A”
	if (!Directory.Exists(@"C:\Users\USER\Desktop\测试文件夹\A"))
            {
                Directory.CreateDirectory(@"C:\Users\USER\Desktop\测试文件夹\A");
                print("文件夹不存在,创建");
            }
【2】根据文件路径删除文件
 //删除指定文件夹下的文件(包含后缀)
	if (File.Exists(@"C:\Users\USER\Desktop\测试文件夹\a.txt"))
            {
                File.Delete(@"C:\Users\USER\Desktop\测试文件夹\a.txt");
                print("查找到文件并删除");
            }
【3】加载文件夹中的图片
【3-1】WWW类加载
 Texture2D texture;
    public GameObject cube;
    void Start()
    {
        StartCoroutine(LoadTexture());
        //将读取到的图片赋给材质
        cube.GetComponent<MeshRenderer>().material.mainTexture = texture;
    }
     
    IEnumerator LoadTexture() 
    {
       string str = @"file:///C:\Users\USER\Desktop\a.jpg";

       WWW  www = new WWW(str);

       texture = www.texture;

       yield return texture; 
    }
【3-2】 文件流加载
 public GameObject cube;
    void Start()
    {
        string imagePath = @"C:\Users\USER\Desktop\a.jpg";

        FileStream files = new FileStream(imagePath, FileMode.Open);

        byte[] imgByte = new byte[files.Length]; files.Read(imgByte, 0, imgByte.Length);

        files.Close();

        Texture2D newtexture = new Texture2D(100, 100); 

        newtexture.LoadImage(imgByte);

        //将加载到的图片赋给材质
        cube.GetComponent<MeshRenderer>().material.mainTexture = newtexture;
    }
【4】将项目中图片复制到指定文件夹中
Texture2D texture;
    void Start()
    {
        StartCoroutine(LoadTexture());

        byte[] imagebytes = texture.EncodeToJPG();

        string newName="新命名";
        string savePath = @"C:\Users\USER\Desktop\测试文件夹";
        
        //将加载到的图片保存到此路径
        string dirPath = savePath + newName;
        File.WriteAllBytes(dirPath, imagebytes);
    }
     
    IEnumerator LoadTexture() 
    {
       string str = @"file:///C:\Users\USER\Desktop\a.jpg";

       WWW  www = new WWW(str);

       texture = www.texture;

       yield return texture; 

    }
【5】将文件夹中某个文件复制到另一文件夹中
	string fileName= "新建文本文档.txt";
    string fromDirPath = @"C:\Users\USER\Desktop\A";
    string targetDirPath= @"C:\Users\USER\Desktop\B";

	void Start () 
    {
        CopyDirectory(fromDirPath, targetDirPath, fileName);
	}

    /// <summary>
    /// 文件复制
    /// </summary>
    /// <param name="fromDir">起始文件夹</param>
    /// <param name="targetDir">目标文件夹</param>
    /// <param name="fileName">起始文件夹中要复制的文件名(包含后缀)</param>
    public static void CopyDirectory(string fromDir, string targetDir, string fileName)
    {
        if (!File.Exists(targetDir + "/" + fileName))//如果目标文件夹中没有此文件
        {
            File.Copy(fromDir + "/" + fileName, Path.Combine(targetDir, fileName), true);
        }
        else
        {
            Debug.Log("文件夹中已包含此文件,请手动复制。路径为:" + targetDir + "/" + fileName);
        }
    }
【6】读取文件夹中文件个数
		string dirPath = @"C:\Users\USER\Desktop\测试文件夹";
        //判断给定的路径是否存在,如果不存在则退出
        if (!Directory.Exists(dirPath))
        {
            return;
        }

        int number = 0;

        //定义一个DirectoryInfo对象
        DirectoryInfo folder = new DirectoryInfo(dirPath);

        //通过GetFiles方法,获取di目录中的所有文件的大小
        foreach (FileInfo fi in folder.GetFiles())
        {
            number++;
        }

        print(number);
【7】读取文件夹下所有文件名
        string dirPath = @"C:\Users\USER\Desktop\测试文件夹";
        DirectoryInfo dir = new DirectoryInfo(dirPath);
        FileInfo[] files = dir.GetFiles(); //获取所有文件信息
        foreach (var file in files)
        {
            print(file.Name);
        }
【8】读取文件夹下所有文件路径
        string dirPath = @"C:\Users\USER\Desktop\测试文件夹";
        string[] dirs = Directory.GetFiles(dirPath);
        for (int j = 0; j < dirs.Length; j++)
        {
            print(dirs[j]);
        }
【9】Unity对Txt文件的读写操作
	//写入
    void WriteTxt(string info)
    {  
        StreamWriter sw;
        FileInfo fi = new FileInfo(txt文件路径);
        sw = fi.CreateText();        //直接重新写入,如果要在原文件后面追加内容,应用fi.AppendText()
        sw.Write(info);
        sw.Close();
        sw.Dispose();
    }

    //读取
    void ReadTxt()
    {
        StreamReader sr = null;
        sr = File.OpenText(txt文件路径);
        //下面进行你的操作。 ReadToEnd()是获取到txt中的内容。
        //LastComName = sr.ReadToEnd();

        sr.Close();
        sr.Dispose();
    }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值