在Unity项目中使用Git

本文参考了Unity官网的Mastering Unity Project Folder Structure - Version Control SystemsUsing External Version Control Systems with Unity

首先需要了解一下Unity的目录结构。假设有一个叫做testproject的项目,那么它的结构应该如下图所示。

Assembly-CSharp-vs.csproj and Assembly-CSharp.csproj – Visual Studio (with -vs suffix) and MonoDevelop project files generated for your C# scripts.

Assembly-CSharp-vs.csproj 和 Assembly-CSharp.csproj :Visual Studio(带有-vs后缀)和MonoDevelop为C#脚本生成的项目文件。

Assembly-UnityScript-vs.unityproj and Assembly-UnityScript.unityproj – the same project files but for JavaScript scripts.

Assembly-UnityScript-vs.unityproj 和 Assembly-UnityScript.unityproj:相同的项目文件,只不过是为JavaScript脚本。

testproject.sln and testproject-csharp.sln – solution files for IDEs, first one includes all C#, JavaScript and Boo projects, while the second one – only C# projects and is designed to be opened in Visual Studio, because VS doesn’t know to handle JavaScript and Boo projects.

testproject.sln 和 testproject-csharp.sln:IDE的解决方案文件,第一个包括所有的C#、JavaScript和Boo脚本;而第二个只包括C#脚本,被设计用来在Visual Studio中打开,因为VS不知道如何处理JavaScript和Boo项目。

testproject.userprefs and testproject-csharp.userprefs – configuration files where MonoDevelop stores current opened files, breakpoints, watches etc.

testproject.userprefs 和 testproject-csharp.userprefs:MonoDevelop用来存储当前打开文件、断点、观测等的配置文件。

Assets – folder where all game resources are stored, including scripts, textures, sound, custom editors etc. Definitely the most important folder in your project.

Assets:存储所有游戏资源的文件夹,包括脚本、纹理、声音、编辑器定制等。当然是项目中最重要的文件夹。

ProjectSettings – in this folder Unity stores all project settings like Physics, Tags, Player settings etc. In other words everything you setup from Edit → Project Settings set of menus goes into this folder.

ProjectSettings:在这个文件夹中Unity存储所有的项目设置,如物理、标签、角色设置等。或者说,所有你从 Edit → Project 的菜单中设置的都在这个文件夹中。

Library – local cache for imported assets, when using external version control system should be completely ignored.

Library:被导入资源的本地缓存,当使用外部版本控制系统时应当被完全忽略。

obj and Temp – folders for temporary files generated during build, first one used by MonoDevelop, second – by Unity.

obj 和 Temp:存储构建时产生的临时文件的文件夹,第一个用于MonoDevelop,第二个用于Unity。

看完了以上的内容就可以知道,我们只需要将AssetsProjectSettings两个文件夹纳入版本控制即可。但在编写.gitignore文件前,还需要以下几个步骤,将设置以文本形式存储以利于版本控制:

  1. 在 Edit->Project Settings->Editor->Version Control Mode 中选择 Visible Meta files
  2. 在 Edit->Project Settings->Editor->Asset Serialization Mode 中选择 Force Text
  3. 保存场景和项目。

下面是一份参考的.gitignore文件:

=============== 
Unity generated
===============
Temp/
Library/

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

============
OS generated
============
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
要在Unity使用Git,您需要按照以下步骤进行操作: 1. 安装Git:如果您还没有安装Git,需要在您的计算机上安装Git。您可以从官方网站https://git-scm.com/downloads下载适用于您操作系统的Git版本。 2. 创建Git仓库:在您的Unity项目文件夹创建一个Git仓库。您可以在命令行使用以下命令创建Git仓库: ``` cd path/to/project/folder git init ``` 3. 添加文件:在您的Unity项目添加文件并保存更改。 4. 添加更改:在命令行使用以下命令将更改添加到Git仓库。 ``` git add . ``` 这将添加所有更改到Git仓库。如果您只想添加特定文件,请使用以下命令: ``` git add path/to/file ``` 5. 提交更改:在命令行使用以下命令提交更改。 ``` git commit -m "commit message" ``` 在提交更改时,您需要提供有关提交更改的简短描述。您可以在引号替换“commit message”为您选择的任何描述。 6. 远程仓库:如果您想将您的代码存储在远程仓库,您需要将其与本地仓库关联。您可以在GitHub、BitBucket等服务上创建新的远程仓库。在命令行使用以下命令将本地仓库与远程仓库关联。 ``` git remote add origin remote_repository_URL ``` 在上面的命令,将“remote_repository_URL”替换为您的远程仓库的URL。 7. 推送更改:在命令行使用以下命令将更改推送到远程仓库。 ``` git push origin master ``` 在上面的命令,“master”是您的本地仓库的主分支。如果您使用其他分支,请将其替换为您的分支名称。 使用Git可以帮助您跟踪Unity项目的更改并轻松地管理代码。如果您在使用Git时遇到问题,请检查Git文档或寻求更多帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值