Unity 官方入门教程观看笔记

今天看了一下Unity官网最新的入门教程,在看的过程中简单总结了其中涉及的概念以及需要注意的内容。

GameObject:游戏对象

游戏对象是Unity编辑器中最重要的概念。游戏中的每个对象都是游戏对象,从角色、可收集的道具到灯光、摄像机和特效。然而,一个游戏对象不能自己做任何事情;在它成为角色、环境或特殊效果之前,你需要赋予它属性。

游戏对象是Unity中代表角色、道具和场景的基本对象。它们本身并没有完成多少工作,但它们充当实现功能的组件的容器。

所以GameObject其实没什么好讲的,它能做什么,基本取决于它有什么Component。

3D

在游戏中可以创建很多的基础类型,如cubu,sphere等

可以通过多种方式创建新的对象,创建以后,双击或者按F键可以聚焦到该对象

选择该对象后,右侧有些基础的属性,这些叫做Component

UI

TextMesh Pro(TMP)

这种UI的对象是生效在2D纬度的,所以需要切换到2D视角去观察。

它的Transform比较特殊,叫做Rect Transform

Anchor指的是轴心在哪里,position指的是相对轴心的偏移位置

这种就是轴心在左上角,偏移量为0

快速切换的方式,就是点击左上角那个Anchor Preset,有些预先设置好的Anchor可以直接选,按住option键可以同时设置偏移量

Component

Transformer

分为Position,Rotate和Scale,即位置,旋转和缩放

Mesh Filter

The Mesh Filter takes a mesh from your assets and passes it to the Mesh Renderer for rendering on the screen.

When importing mesh assets, Unity automatically creates a Skinned Mesh Renderer if the mesh is skinned, or a Mesh Filter along with a Mesh Renderer, if it is not.

In order for non-skinned Meshes to be rendered, both the Mesh Filter and Mesh Renderer components must be present for legacy reasons.

初步理解,Mesh Filter是一个用于渲染GameObject的纯网格,会自动被传给同一个对象的Mesh Render用于去渲染整个对象

Mesh Renderer

利用Mesh Filter,Material等来渲染GameObject到场景中。

  • Material:用于渲染的材质

  • Lighting

  • Cast Shadows:是否开启阴影,关了就不会在光源下产生影子

  • Probe:翻译叫做探针,具体作用待补充

  • Light Probes:

  • Additional Settings:

Mesh Collider

The Mesh Collider takes a Mesh Asset and builds its Collider based on that Mesh. It is more accurate for collision detection than using primitives for complicated Meshes. Mesh Colliders that are marked as Convex can collide with other Mesh Colliders.

网格碰撞体,基于Mesh Filter,可以更为精确的计算碰撞,标记为Convex的网格碰撞体可以和其他的网格碰撞体碰撞(也就是说开启的和开启的才会产生碰撞,没开启的和没开启的产生碰撞)

MeshCollider.convex 凸面网格

这意味着如果您将此设置为 true,则您的网格碰撞器将不会有孔或入口。 凸面网格可以与其他凸面碰撞器和非凸面网格发生碰撞。 因此凸网格碰撞器适用于刚体,如果您确实需要比原始碰撞器提供的更详细的碰撞器。

注意:物理引擎要求凸面网格具有非零体积。 平面网格(例如标记为凸面的四边形或平面)将被物理引擎修改为具有满足此要求的厚度(以及体积)。 生成的网格的厚度与其尺寸成正比,最多可达网格平面中最长尺寸的 0.05。

Collider.isTrigger

触发器不会向传入的刚体注册碰撞。相反,当刚体进入或退出触发器卷时,它发送OnTriggerEnter, OnTriggerExit和OnTriggerStay消息。

当一个Collider是一个Trigger时,碰撞器就是单纯的碰撞检测,就不会阻止其他碰撞体与自己重叠,

Material

In Unity, you use materials and shaders together to define the appearance of your scene

To draw something in Unity, you must provide information that describes its shape, and information that describes the appearance of its surface. You use meshes to describe shapes, and materials to describe the appearance of surfaces.

Materials and shaders are closely linked; you always use materials with shaders.

A material contains a reference to a Shader object . If that Shader object defines material properties, then the material can also contain data such as colors or references to textures.

Add a component that inherits from Renderer. MeshRenderer is the most common and is suitable for most use cases, but SkinnedMeshRenderer, LineRenderer, or TrailRenderer might be more suitable if your GameObject has special requirements.

Shader

A shader asset is an asset in your Unity project that defines a Shader object . It is a text file with a .shader extension. It contains shader code.

Texture:纹理

Normally, the mesh geometry of an object only gives a rough approximation of the shape while most of the fine detail is supplied by Textures . A texture is just a standard bitmap image that is applied over the mesh surface.

Textures are applied to objects using Materials . Materials use specialised graphics programs called Shaders to render a texture on the mesh surface.

到这里,我们可以得出一个大概的结论,Mesh Filter定义了GameObject的网格信息,Mesh Render利用Mesh Filter和Material去渲染,而Material是和Shader紧密联系使用的,Material可以使用Shader去把Texture渲染到Mesh的表面。

Texture也包含粒子系统,以后再仔细看

Rigidbody:刚体

添加了刚体才会受物理引擎的影响

Rigidbodies enable your GameObjects to act under the control of physics. The Rigidbody can receive forces and torque to make your objects move in a realistic way. Any GameObject must contain a Rigidbody to be influenced by gravity, act under added forces via scripting, or interact with other objects through the NVIDIA PhysX physics engine

刚体和碰撞体什么关系,两个刚体撞在一起会产生力吗?如果一个Collider.isTrigger是true,而另一个不是,会吗?

拥有刚体和碰撞器的GameObject会被认为是动态的,在碰撞过程中物理引擎会生效。

但如果刚体的kinematic设置为true,就不会对力进行响应,只可以通过transform属性进行脚本控制。

Animator

- Animation Clips:当前模型的动作列表。

- Animator Controller:状态机,用于控制当前模型在不同条件下在不同Clip之间的转换。

- Avatar:人物模型特有的属性,可以为模型提供更为丰富的操作。

Script

- 在脚本中,可以使用GetComponent()的方式获得所有事先加入的Component,如GetComponent()可以获得我们加入的刚体组件。

- 定义为private的变量,不会在inspector显示,而public可以,然后我们可以在inspector或者其他脚本给public的变量赋值

LifeCycle

Update

每一帧执行一次

FixedUpdate

what is the difference between Update & FixedUpdate in Unity?

每一帧执行次数取决于物理引擎作用的次数

LateUpdate

每一帧执行一次,但是是在所有的GameObject的Update执行之后再执行所有GameObject的LateUpdate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{
public float speed = 0f;
private Rigidbody rb;
private float movementX;
private float movementY;

// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}

// InputSystem发送的信息会被这个函数接收
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();

movementX = movementVector.x;
movementY = movementVector.y;
}


void FixedUpdate()
{
Vector3 movemnt = new Vector3(movementX, 0f, movementY);

rb.AddForce(movemnt * speed);
}
}

Event

Collider.OnTriggerEnter

When a GameObject collides with another GameObject, Unity calls OnTriggerEnter.

OnTriggerEnter happens on the FixedUpdate function when two GameObjects collide. The Colliders involved are not always at the point of initial contact.

Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled (因为两个Collider都不是Trigger会阻止物理对象的重叠), and contain a Rigidbody.

If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.(这里说的是产生碰撞,不是触发OnTriggerEvent事件)

Collider.OnCollisionEnter

https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

这个只要二者发生接触就会触发

Function

GameObject.SetActive(Boolean)

A GameObject may be inactive because a parent is not active. In that case, calling SetActive will not activate it, but only set the local state of the GameObject, which you can check using GameObject.activeSelf. Unity can then use this state when all parents become active.

GameObject的状态,如果是deactive,那么所有的component都不会工作,也就不会渲染,同时Update等生命周期函数也不会执行

Prefab

Prefabs are a special type of Asset that represent a GameObject or collection of GameObjects with components that are already set up. They’re like a blueprint which you can use to easily make instances of the same thing. Each instance of a Prefab is linked to the Prefab Asset, so changing the Asset will change all versions of the Prefab in all Scenes.

The first use for this system in your Project will be to make the character a Prefab. This means that if you go on to make multiple levels for the game, you won’t need to remake JohnLemon for every level — you can just instantiate a new Prefab.

It’s something close: in Unity, models work like read-only Prefabs. They’re blueprints for creating instances of that model, but the blueprint itself cannot be changed.

也就是说,我们从外部导入的模型,一般是只读的Prefab,如果我们想要把它变成可以编辑的,可以重新将拖拽出来的GameObject拖拽回文件夹中,此时会询问你是创建Original Prefab还是Variant of Prefab,看着意思,一个是新的Prefab,一个是对旧有Prefab的引用,我们需要创建一个新的,从而可以编辑它,所以选择Original Prefab

Tag

可以对所有的Object添加Tag。

Packages

Unity提供的一些包,提供一些定制的能力

InputSystem

如InputSytem,当作一个component加入一个GameObject后,可以接收输入事件,并且发送message到函数中

Animation

Unity - Manual: Animation System Overview

Unity has a rich and sophisticated animation system (sometimes referred to as ‘Mecanim’). It provides:

  • Easy workflow and setup of animations for all elements of Unity including objects, characters, and properties.

  • Support for imported animation clips and animation created within Unity

  • Humanoid animation retargeting the ability to apply animations from one character model onto another.

  • Simplified workflow for aligning animation clips.

  • Convenient preview of animation clips, transitions and interactions between them. This allows animators to work more independently of programmers, prototype and preview their animations before gameplay code is hooked in.

  • Management of complex interactions between animations with a visual programming tool.

  • Animating different body parts with different logic.

  • Layering and masking features

Unity的动画系统是基于动画剪辑(animation Clips)的概念,它包含特定对象如何随着时间改变其位置、旋转或其他属性的信息。每个片段都可以看作是一个单一的线性录音。来自外部来源的动画剪辑由艺术家或动画师使用第三方工具如Autodesk 3ds Max或Autodesk Maya,或来自动作捕捉工作室或其他来源。然后,动画剪辑被组织成一个结构化的流程图式系统,称为动画控制器。动画控制器充当状态机

一个非常简单的动画控制器可能只包含一两个片段,例如控制道具旋转和弹跳,或者在正确的时间动画一扇门的打开和关闭。更高级的动画控制器可能包含数十个类人动画,用于所有主要角色的动作,并可能同时混合多个剪辑,以便在玩家在场景中移动时提供流体运动。Unity的动画系统也有许多处理类人角色的特殊功能,这给了你从任何来源重新定位类人动画的能力。(例如:动作捕捉;资产存储;或其他第三方动画库)到您自己的角色模型,以及调整肌肉定义。这些功能是在统一的“Avatar”系统中实现的。