首页 > 解决方案 > 在 Unity 3d Android 平台中将图像从一个文件夹复制到另一个文件夹

问题描述

我正在寻找这个问题很多天。我正在开发 Unity 3d Android 项目。是否可以在按下按钮时选择图像,然后在按下另一个按钮时将该图像复制到另一个文件夹?我见过太多的插件,其中大部分是 UnityEditor(PC 平台)和 android 平台都不起作用。请回复。

标签: androidimageunity3dcopytextures

解决方案


这是我的代码

    using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

    public class FileC : MonoBehaviour
    {
    public void save()
        {
            string fileName = "a.png";

        string sourcePath = @"/mnt/sdcard/Test1";

        string targetPath =  @"/mnt/sdcard/Test2";


            // Use Path class to manipulate file and directory paths.
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            // To copy a folder's contents to a new location:
            // Create a new target folder, if necessary.
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(sourcePath))
        {
            System.IO.Directory.CreateDirectory(sourcePath);
        }

            // To copy a file to another location and 
            // overwrite the destination file if it already exists.
            System.IO.File.Copy(sourceFile, destFile, true);

            // To copy all the files in one directory to another directory.
            // Get the files in the source folder. (To recursively iterate through
            // all subfolders under the current directory, see
            // "How to: Iterate Through a Directory Tree.")
            // Note: Check for target path was performed previously
            //       in this code example.
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
        }


    }

推荐阅读