9

I wish to copy my existing Unity project into a new blank Unity project. It appears all the object references and many scripts are not configured properly/present in the Hierarchy in my scenes.

I only copied my assets folder/package.json into the new Unity project, because the rest of the files are dirty and have cached a lot of useless information.

Where are the files, detailing the scenes data located? I want to copy that over so I can run my scenes with all the object references, and scripts set onto the correct game objects.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Aiden Faulconer
  • 411
  • 1
  • 7
  • 18

2 Answers2

21

Before starting always make Backups - you never know!


Cleaning Up / Migrating as a project folder

In general you will always need the folders

  • Assets: Contains all the assets like your scripts, scenes, prefabs, 3D models, textures, sounds, materials, etc
  • ProjectSettings: Contains project settings like e.g. physics, lighting,etc
  • Packages: Contains a manifest.json describing which packages should be installed as dependencies and a packagelock.json with further information of the currently installed packages like e.g. their current dependency nesting levels and exact versions/git branches etc

(state Unity 2019 - relevant folders/files might change between versions)

All the rest you can basically delete and Unity will recreate/recompile them when you open the project again.

Also see Behind the Scenes (for newer Unity versions refer to Using external Version Control instead)

When backing up a project, or adding a project to a Version Control Repository, you should include the main Unity project folder, containing both the Assets and ProjectSettings and Packages folders. All the information in these folders is crucial to the way Unity works.

For me also some of the Library/*.asset files make sense to keep .. they store e.g. the build settings, target platform etc. so I don't have to configure them / switch platform from scratch every time I "reset". It's up to you of course if you want to do this as well.


You can also use any other version control system ... or of course also remove according folders and files manually but I always use GIT for my projects - even if it is only local or even once for cleaning up before zipping the project as a backup ;)

Also refer to Using external version control systems with Unity for some general information about the setup for version controlling your project(s).

So when I want to clean up a repository before coping it I usually use the following as *.gitignore (it is very basic - for a more complete and constantly updated version see the one on Github !).

Everything listed here can basically be deleted and will be re-compiled the next time you open the project in Unity. (Lines starting with ! are exceptions I added because as said for me it made sense to keep those as well.)

# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Recordings can get excessive in size
/[Rr]ecordings/

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage
*.app

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

and as said for me personally it sometimes makes sense to add

# keep the *.asset files from Library
!/[Ll]ibrary/*.asset

If not done yet first initialize the repository running

cd your/project/root/path
git init
git add .gitignore *

this will show some warnings for every file that is listed in the .gitignore but you can ignore those warnings. It only says something similar like

You are trying to add an ignored file to the commit and it will be skipped

than run

git commit -m "Initial commit"

to make your first commit.

Now finally you can simply run git clean

git clean -xfd

which removes every file that is not tracked (so be sure to have always all files you want to keep at least staged (git add) or better commited first) or would be ignored by the *.gitignore.

-f
If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.

-d
Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

-x
Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

If unsure you can also add a

-n
Don’t actually remove anything, just show what would be done.


Note If you already had a .git repository but added / edited the .gitignore afterwards also refer to How to make Git "forget" about a file that was tracked but is now in .gitignore?


Migrating using UnityPackage (Assets only!)

Another option for moving scenes or parts of the Assets between two different projects is using a UnityPackage. Note that this will not include any installed packages, settings etc but purely the assets and their asset-dependencies (linked prefab, materials, textures etc)!

From your current project export a UnityPackage

Excerpts from the Docs:

Use Export Package to create your own Custom Package.

  1. Open the Project you want to export Assets from.

  2. Choose AssetsExport Package from the menu to bring up the Exporting Package dialog box. (See Exporting Package dialog box image below.)

  3. In the dialog box, select the Assets you want to include in the package by clicking on the boxes so they are checked.

  4. Leave the include dependencies box checked to auto-select any Assets used by the ones you have selected.

  5. Click on Export to bring up the file explorer, and choose where you want to store your package file.

  6. Name and save the package anywhere you like.

HINT: When exporting a package Unity can export all dependencies as well. So, for example, if you select a Scene and export a package with all dependencies, then Unity exports all Models, Textures and other Assets that appear in the Scene as well. This can be a quick way of exporting several Assets without manually locating them all.

enter image description here

alternatviely to step 2 you can also Right-Click on the Assets folder in the Project View and find the Export Package option in the context menu.

and then in the new project Import the UnityPackage

To import an Asset package:

  1. Open the Project you want to import Assets into.
  2. Choose AssetsImport PackageCustom Package.
  3. In the file explorer, select the package you want and the Import Unity Package dialog box appears, with all the items in the package pre-checked, ready to install. (See Import Unity Package dialog box image below.)
  4. Select Import and Unity puts the contents of the package into the Assets folder, which you can access from your Project view.

enter image description here

Alternatively to step 2 and 4 you can even simply drag and drop the unitypackage file into the Assets folder via the Unity Editor.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 3
    Adding to this very comprehensive answer, when stuff gets unlinked, its a sign that you managed to lost you hidden .meta files, and Unity assigned new fresh IDs to everything. If you make sure you copy assets including the metafiles, and move a version up (its a mixed bag when going down a version) everything should link up – zambari May 23 '19 at 07:55
-1

I wanted to do something similar which would apply to the "Move to Another PC" part.

I noticed that my projects are taking quite the amount of space and wanted to clean the proyect. I followed derHugo answer recommended "git clean" procedure but using his linked GitHub .gitignore file.

I had to make some changes so it work's on Unity 19.4:

# This .gitignore file should be placed at the root of your Unity project directory
#
# Modified from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
# Keep Library Folder
#/[Ll]ibrary/
/[Ll]ibrary/Artifacts/
/[Ll]ibrary/ArtifactDB*
/[Ll]ibrary/PackageCache/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/

# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.aab
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*

# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*

Basically what I did was to not ignore the entire Library folder and only ignore the following:

/[Ll]ibrary/Artifacts/
/[Ll]ibrary/ArtifactDB*
/[Ll]ibrary/PackageCache/

Here's the procedure you should follow:

  • Backup your Project just in case.
  • Put the supplied .gitignore file on your project's root directory
  • Run the following commands:

# Initialize Repository
git init

# Add .gitignore file
git add .gitignore

# Add the rest of the files
git add *

# Commit changes
git commit -m "Initial Commit"

# Clean all untracked files and folders
git clean -Xfd

*Notice the -X option is capitalized on the git clean command, this avoids erasing any folder that's empty and untracked.

To verify everything is fine:

  • Load your Unity Project and check everything is still working correctly
  • Run git clean -Xfd once more to clean the project's files that were rebuilt by Unity.

Again, thanks derHugo for his detailed answer which was used to make this work on latest Unity releases.

  • In general the entire Library folder should be ignored since Unity recreates it dynamically. Me force including some asset files that are e.g. build settings etc is not a general suggestion just a personal preference – derHugo Jul 08 '22 at 14:45