Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created January 20, 2022 08:17
Show Gist options
  • Select an option

  • Save unitycoder/deff3006d6fbed1ed80e4fb045c7cb4e to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/deff3006d6fbed1ed80e4fb045c7cb4e to your computer and use it in GitHub Desktop.
Copy files between projects (unity)
// // https://forum.unity.com/threads/shared-script-to-copypaste-files-and-directories.1227576/
// This script allows you to copy and paste files or directories between editors.
// The paths of the copied files are stored in a Textfile that you define (here: "D:/UnityCopyFile.txt")
// Metafiles also get copied.
// Only works on the right column of the "Project"-tab (Two column layout).
// Do a rightclick there and see "FileCopy" and "FilePaste" entries
// Place this script in a folder named "Editor".
// Feel free to use and improve + share this script.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class FileCopy
{
[MenuItem("Assets/FileCopy")]
public static void Copy()
{
{
if (Selection.objects.Length == 0)
Debug.LogWarning("No asset selected");
else
{
string source = "";
Debug.LogWarning("More than one asset selected");
foreach (Object obj in Selection.objects)
{
string s = Application.dataPath.Substring(0, Application.dataPath.Length - 7) + "/" + AssetDatabase.GetAssetPath(obj);
source += "\n" + s;
source += "\n" + s + ".meta";
}
System.IO.File.WriteAllText("D:/UnityCopyFile.txt", source);
}
}
}
[MenuItem("Assets/FilePaste")]
public static void Paste()
{
string allSources = System.IO.File.ReadAllText("D:/UnityCopyFile.txt");
string[] lines = allSources.Split("\n"[0]);
foreach (string source in lines)
{
if (source != "")
{
string target = "";
//File:
if (source.Contains("."))
{
target = Application.dataPath.Substring(0, Application.dataPath.Length - 7) + "/" + AssetDatabase.GetAssetPath(Selection.activeObject) + "/" + Path.GetFileName(source);
System.IO.File.Copy(source, target, true);
}
// Folder
else
{
// https://code.4noobz.net/c-copy-a-folder-its-content-and-the-subfolders/
string foldername = source.Substring(source.LastIndexOf(@"/"), source.Length - source.LastIndexOf(@"/"));
target = Application.dataPath.Substring(0, Application.dataPath.Length - 7) + "/" + AssetDatabase.GetAssetPath(Selection.activeObject) + foldername;
CopyFolder(source, target);
}
}
}
AssetDatabase.Refresh();
}
public static void CopyFolder(string sourceDirectory, string targetDirectory)
{
var diSource = new DirectoryInfo(sourceDirectory);
var diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
Debug.Log("target.FullName: " + target.FullName);
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment