UnityLearn_Beginner_UnityTips

UnityLearn_Beginner_UnityTips

Tutorial. Unity Tips

Unity官方教程_BeginnerLevel

https://learn.unity.com/tutorial/unity-tips#5c7f8528edbc2a002053b481

Snap

当移动、旋转、缩放物体时,可以按住Ctrl/ Cmd键,能够按步长进行更改。
比如每次移动1m,每次旋转15°等等
步长可以在Edit -> Snap Settings中进行设置

Preview Window:

资源文件的Inspector面板下面有一个Preview面板,一般而言是固定在Inspector下的。
可以右键Preview面板上方的bar,就能undock它

快速定位Camera位置:

在Scene中将视角调到合适位置和角度,选中需要改变的Camera,选择GameObject -> Align With View
  可将相机的位置和角度调至Scene中的视角

选中一个Camera后,选择GameObject -> Align View To Selected
  可将Scene中视角调整至与Camera相同

在Inspector中像调整Slider.value一样调整public variable:

在public variable上加上Range decoration:
  [Range(1.0f, 10.0f)] 即可

在Inspector中显示private variable:

public variable可以在Inspector面板中显示
但是,为了在Inspector面板中显示,而使用了public的话,则vialate the encapsulation principle
解决方法: [SerializeField] attribute

大型项目的注意点:

Before Starting:

Source Control:

Unity Editor集成的source control service: Collaborate

使用external source control: Git/ Svn/ Mercurial/ Perforce等工具,需要注意
1. 不要包括Library, Temp, obj,因为在另一台电脑打开工程时是不需要这些文件的
  它们会导致repository的杂乱、并占用大量存储空间
2. Version control to Visible Meta Files:
  将meta files放置在assets资源文件边上,这些meta files存储了相关属性值和设置值
  这样做的话可以在组员之间共享这些资源文件的配置设置
  否则,会将meta files存放在Library文件夹中,导致不能上传到Git共用
3. Asset Serialization to Force Text:
  make all unity files (ScriptableObjects, Scenes, Prefabs and many others) use a text representation instead of a binary one.
  这么做可以 1. 使Source control只存储对这些文件的更改,而不是一份完整的文件拷贝
  2. 可以手动merge文件更改的conflicts
  在项目初期就进行asset serialization的设置是被推荐的

Scales and Standards:

Units:

对于使用rigidbody的3D游戏: 1 unit = 1 meter,以便物理引擎准确的模拟

对于使用rigidbody的2D游戏: 定义一个"base sprite size" 以确定 1 base size = 1 unit = 1 meter
  比如设置32px = 1 meter = 1 unit.
  将所有的Sprite的Pixel Per Unit都设置为该值(32),以便物理引擎的模拟

Naming Convention:

所有资源文件的文件名都遵循相同的Naming Convention.
比如:
  appending suffixes: 比如 _N for normal map, _AO for ambient occlusion map
  prefixes: 比如 LowRes_ 和 HighRes_
方便于: 
  filter through search
  write custom importer setting value automatically

Project Structure:

Folders:

.meta文件记录了对应资源文件的导入设置(import settings)
因此在移动资源文件时,需要在Unity Editor的Project View中进行操作, 这么做Unity会take care of moving everything properly
或者在操作系统的Explorer中移动时将对应的meta文件也随着资源文件一起移动,否则会导致引用丢失(lose references)和导入设置的丢失

文件夹的创建规则
  两条遵循的方法:
  1. A folder for each type of asset, and subfolders per objects/ zones
    比如(Assets/Materials; Assets/Prefabs; Assets/Materials/Level1;)
  2. A folder per objects/ zones
    比如(Assets/Level1/Enemy/Archer; Assets/Shared/UI; Assets/Forest/Trees)
    所有有关的资源都放置在对应文件夹下
    比如(Assets/Forest/Trees/BigTree.fbx; Assets/Forest/Trees/SmallTree.mat)

如果项目用到Assets Bundles时,使用第二种方法会很方便
  可以很方便地create bundles per content themes by adding the parent folder to a specific bundle
  并splitting the related assets through different bundles.

Prototype Folders:

原文: Keep everything that is related to a prototype or placeholder in a separate folder hierarchy, and avoid cross referencing between those folders and folders which contain shippable assets/code. For example, your Character prefab in the "normal" file hierarchy should not reference a test material in your Prototype folders. Create a prototype character prefab that would reside in the Prototype folders and never use that prefab in a non prototype scenes (you could even write editor scripts that check references used by objects in the prototype folder to be sure that this rules is enforced. )

Resources:

尽可能地在Resources文件夹中存放资源。

Resources文件夹中的资源可以通过脚本Resources.Load()被动态加载,
但是Unity不会去判断该文件夹中的资源是否真的被加载/使用,因此所有资源都会被打包到Build中

使用path/ filename,会对后期项目的管理增加成本,比如文件目录变更、资源名更改等
  推荐的方法:
    1. 使用direct reference: 用拖拽到脚本变量的方式
    2. 如果真的需要使用Resources.Load,请使用Resources.LoadAsync()
      该方法will not block execution
      在某些设备上,Loading会占用很长的时间

ResourceRequest request = Resources.LoadAsync("cube");

request.completed += operation => {
    ResourceRequest op = operation as ResourceRequest;
    Instantiate(op.asset);
};

Assets:

确保资源文件是小的:
  比如对于一个texture资源,可以在import进unity时减小分辨率
  但是这样做并不会减少import资源的时间,导致第一次initial setup工程的时间变得更久

使用压缩格式
  对image和audio文件进行压缩
  Image: 使用.png/ .tga/ .tiff/ 如果图形软件支持的话,也可以使用.dds
  audio: 使用.ogg

Assembly Definition Files:
  ADF compile all scripts in a folder and its subfolders into a separate DLL.
  只需重新编译修改过的脚本属于的dll,可以有效加快编译速度
  比如修改了有关Enemy的脚本,重新编译时只需要编译Enemy对应的dll即可

Scenes Structure:

Hierarchy depth and count

尽可能地减少hierarchy的深度。
Recommend: Prefer multiple small hierarchies at the root to one big hierarchy

因为
1. 当有一个object变化时,Unity的transform system需要追溯该object属于的一整个hierarchy。
  假设有很多个hierarchies时,只需要traverse其中一个hierarchy即可

2. Unity对每一个hierarchy进行查询时,是进行multi-thread的。
  有多个hierarchies时,unity会用多个thread进行traverse,因此可以进行速度优化

不过对于static objects(比如static的environment meshes/ lights/ sounds等)
  如果便于场景管理,则是可以将它们放在同一个hierarchy中的,因为它们的transform不会变

减少depth of hierarchy的方法:
1. 不要将纯游戏物体与graphics/ physics/ audio的物体嵌套在一起
  如果有些东西的位置是不需要移动的,那么就不要把它放在需要移动(比如角色)物体下
  (这一条好像理解错了。。。For example, having a GameObject that handle the patrol points of a navmesh   agent character is a child of that character. All pure gameplay GameObjects should be as close to the root as possible.)

2. 动态初始化创建一个游戏物体,不要将它置于创建它的物体下,除非它需要跟着创建它的物体一起移动

好处:
1. engine performance.
2. ease and comfort of the person working on the project -- easier to understand and traverse

NB.
Use Optimize GameObject in the Rig tab of your Animation import settings. This will prevent the "skeleton" GameObject of your rig being created, reducing the hierarchy. If you need to still expose some (you may use one as the target to attach equipment on the character) you can manually pick it to only expose this one.

Organization:

使用空物体来管理hierarchy
  比如使用一个名为"----Lights----"的空物体作为所有lights的root

卧槽?

Note that you should not make it the parent of your lights, but just place it above it in the hierarchy for reasons explained in Hierarchy depth and count above.
If you want for ease of working to parent all light under your Light "header" so you can collapse everything, you should write an editor script that unparents everything before build and/or entering playmode, otherwise performance could suffer on large scenes.

Dynamic Objects in Editor:

 略

Game Design and Features:

Create cheat function:

cheat functions在开发测试阶段很重要。比如增加金币、定点传送、物品获得等。

实现方法:

1. static functions with [MenuItem("Cheat/MyCheat"): 可以在playmode中通过上方菜单进行作弊

2. 基于按键组合或简单的F1/F2功能按键直接执行

3. 通过按键打开内置作弊控制台,输入commands执行作弊操作

在正式游戏中去除作弊代码:

1. 给作弊方法加上conditional attribute: [Condition(DEVELOPMENT_BUILD || UNITY_EDITOR)]
  这样会让编译器忽略该方法,并忽略对该方法的调用

2. 使用预处理命令
  #if DEVELOPMENT_BUILD || UNITY_EDITOR
  #endif
  注意: 如果对整个方法进行该处理,那么在发布的正式包中,对调用该方法的地方会报错(non-existent function)

Use small feature test scenes:

创建测试专用的小型场景。
1. 开发、优化并核实一些功能,不被新的功能影响
2. 可以把测试出来的问题从整个游戏剥离出来
  比如创建一个小场景,避免在整个游戏中重新玩五分钟来到达某个前置条件,才能重现Bug

 

 

 

原文地址:https://www.cnblogs.com/FudgeBear/p/11141001.html