如何为 git 准备 Unity 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21573405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:40:56  来源:igfitidea点击:

How to prepare a Unity project for git?

gitunity3d

提问by German

What are the steps necessary to prepare a Unity project for committing to a git repository eg. github? I don't want to store unnecessary files (specially temp files and avoid binary formats as much as possible).

准备 Unity 项目以提交到 git 存储库所需的步骤是什么,例如。github?我不想存储不必要的文件(特别是临时文件并尽可能避免二进制格式)。

回答by German

On the Unity Editor open your project and:

在 Unity 编辑器上打开您的项目,然后:

  1. Enable External optionin UnityPreferencesPackagesRepository(only if Unity ver < 4.5)
  2. Switch to Visible Meta Filesin EditProject SettingsEditorVersion Control Mode
  3. Switch to Force Textin EditProject SettingsEditorAsset Serialization Mode
  4. Save Scene and Project from Filemenu.
  5. Quit Unity and then you can delete the Libraryand Tempdirectory in the project directory. You can delete everything but keep the Assetsand ProjectSettingsdirectory.
  1. UnityPreferencesPackagesRepository 中启用外部选项(仅当 Unity ver < 4.5)
  2. 编辑项目设置编辑器版本控制模式中切换到可见元文件
  3. 编辑项目设置编辑器资产序列化模式中切换到强制文本
  4. 文件菜单保存场景和项目。
  5. 退出Unity,然后您可以删除项目目录中的LibraryTemp目录。您可以删除所有内容,但保留AssetsProjectSettings目录。

If you already created your empty git repo on-line (eg. github.com) now it's time to upload your code. Open a command prompt and follow the next steps:

如果您已经在线(例如 github.com)创建了空的 git 存储库,现在是时候上传您的代码了。打开命令提示符并按照以下步骤操作:

cd to/your/unity/project/folder

git init

git add *

git commit -m "First commit"

git remote add origin [email protected]:username/project.git

git push -u origin master

You should now open your Unity project while holding down the Option or the Left Alt key. This will force Unity to recreate the Library directory (this step might not be necessary since I've seen Unity recreating the Library directory even if you don't hold down any key).

您现在应该在按住 Option 或左 Alt 键的同时打开您的 Unity 项目。这将强制 Unity 重新创建 Library 目录(这一步可能没有必要,因为我已经看到 Unity 重新创建 Library 目录,即使您没有按住任何键)。

Finally have git ignore the Library and Temp directories so that they won't be pushed to the server. Add them to the .gitignore file and push the ignore to the server. Remember that you'll only commit the Assets and ProjectSettings directories.

最后让 git 忽略 Library 和 Temp 目录,这样它们就不会被推送到服务器。将它们添加到 .gitignore 文件并将忽略推送到服务器。请记住,您将只提交 Assets 和 ProjectSettings 目录。

And here's my own .gitignore recipe for my Unity projects:

这是我自己的 Unity 项目的 .gitignore 配方:

# =============== #
# Unity generated #
# =============== #
Temp/
Obj/
UnityGenerated/
Library/
Assets/AssetStoreTools*

# ===================================== #
# Visual Studio / MonoDevelop generated #
# ===================================== #
ExportedObj/
*.svd
*.userprefs
*.csproj
*.pidb
*.suo
*.sln
*.user
*.unityproj
*.booproj

# ============ #
# OS generated #
# ============ #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db

回答by zasadnyy

Since Unity 4.3 you also have to enable External option from preferences, so full setup process looks like:

从 Unity 4.3 开始,您还必须从首选项中启用外部选项,因此完整的设置过程如下所示:

  1. Enable Externaloption in Unity → Preferences → Packages → Repository
  2. Switch to Hidden Meta Filesin Editor → Project Settings → Editor → Version Control Mode
  3. Switch to Force Textin Editor → Project Settings → Editor → Asset Serialization Mode
  4. Save scene and project from Filemenu
  1. 启用External选项Unity → Preferences → Packages → Repository
  2. 切换到Hidden Meta FilesEditor → Project Settings → Editor → Version Control Mode
  3. 切换到Force TextEditor → Project Settings → Editor → Asset Serialization Mode
  4. File菜单保存场景和项目

Note that the only folders you need to keep under source control are Assetsand ProjectSettigns.

请注意,您需要保持在源代码控制之下的唯一文件夹是AssetsProjectSettigns

More information about keeping Unity Project under source control you can find in this post.

有关将 Unity 项目置于源代码控制之下的更多信息,您可以在这篇文章中找到。