百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
U

unity-cheat-sheet

> 编程语言
开源

适用于 Unity 的 C# 速查表

333 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

适用于 Unity 的 C# 速查表

Unity Cheat Sheet

Table of Contents

  • Basics
    • MonoBehaviour
    • Transform
    • Vector3
    • Quaternion
    • Euler Angles
  • Movement & Rotation
    • Move Object
      • Transform.Translate()
      • Vector3.MoveTowards()
      • Vector3.Lerp()
      • Vector3.SmoothDamp()
    • Rotate Object
      • Transform.rotation
      • Transform.eulerAngles
      • Transform.Rotate()
      • Transform.RotateAround()
      • Transform.LookAt()
      • Quaternion.LookRotation()
      • Quaternion.FromToRotation()
      • Quaternion.ToAngleAxis()
  • Physics
    • Raycast
    • Collisions & Triggers
    • Ignore Collision
  • Input
    • Keyboard
    • Mouse
    • Touch
    • New Input System
  • UI
    • Button
    • Slider
  • Audio
    • Basic Audio Play
  • Scripting
    • Coroutines
    • Async/Await
      • Basic Structure
      • Loading Resources
      • Web Requests
      • Scene Loading
      • Parallel Operations
      • Timeout Handling
      • Best Practices
    • Event Systems
    • Scriptable Objects
    • Custom Editor Scripts
  • Design Patterns
    • Singleton
    • Factory Pattern
    • Observer Pattern
    • Command Pattern
    • State Pattern
      • Basic Example
      • Detailed Example - Game Onboarding System
    • Strategy Pattern
      • Basic Example
      • Detailed Example - Combat System
    • Object Pooling Pattern
    • Chain of Responsibility Pattern
      • Basic Example
      • Detailed Example - Input Handling System
  • Shortcuts
    • Scene View Editing
    • Scene View Navigation
    • Hierarchy Management
    • Layout
  • Practical Use Cases
    • Check if object is on the ground
    • Get the transform of a Body Bone
    • Make object look at the camera
    • Camera follow & orbit
    • Fade UI element
    • Load next scene
  • TBD (To Be Documented)
    • Scripting

Basics

MonoBehaviour

MonoBehaviour Life Cycle Flow Chart

csharp
// MonoBehaviour is the base class from which every Unity script derives.
// Offers some life cycle functions that are easier for you to develop your game.

// Some of the most frequently used ones are as follows;
Awake()
Start()
Update()
FixedUpdate()
LateUpdate()
OnGUI()
OnEnable()
OnDisable()

Transform

…

Vector3

csharp
// Vector3 is representation of 3D vectors and points
// It's used to represent 3D positions,considering x,y & z axis.

Vector3 v = new Vector3(0f, 0f, 0f);

Quaternion

csharp
// A Quaternion stores the rotation of the Transform in world space.
// Quaternions are based on complex numbers and don't suffer from gimbal lock.
// Unity internally uses Quaternions to represent all rotations.
// You almost never access or modify individual Quaternion components (x,y,z,w); 

// A rotation 30 degrees around the y-axis
Quaternion rotation = Quaternion.Euler(0, 30, 0);

Euler Angles

…

Movement & Rotation

Move Object

Transform.Translate()

csharp
// Moves the transform in the direction and distance of translation.
public void Translate(Vector3 translation);
public void Translate(Vector3 translation, Space relativeTo = Space.Self);

transform.Translate(Vector3.right * movementSpeed);

Vector3.MoveTowards()

csharp
// Calculate a position between the points specified by current and target
// Moving no farther than the distance specified by maxDistanceDelta
public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

Vector3 targetPosition;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime);

Vector3.Lerp()

csharp
// Linearly interpolates between two points. Results in a smooth transition.
public static Vector3 Lerp(Vector3 startValue, Vector3 endValue, float interpolationRatio);

Vector3 targetPosition;
float t = 0;
t += Time.deltaTime * speed;
transform.position = Vector3.Lerp(transform.position, targetPosition, t);

Vector3.SmoothDamp()

…

Rotate Object

Transform.rotation

csharp
// A Quaternion stores the rotation of the Transform in world space.
// Quaternions are based on complex numbers and don't suffer from gimbal lock.
// Unity internally uses Quaternions to represent all rotations.

transform.rotation = new Quaternion(rotx, roty, rotz, rotw);

Transform.eulerAngles

csharp
// Transform.eulerAngles represents rotation in world space. 
// It is important to understand that although you are providing X, Y, and Z rotation values to describe your rotation
// those values are not stored in the rotation. Instead, the X, Y & Z values are converted to the Quaternion's internal format.

transform.eulerAngles = Vector3(rotx, roty, rotz);

Transform.Rotate()

csharp
// Applies rotation around all the given axes.
public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);
public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);

transform.Rotate(rotx, roty, rotz);

Transform.RotateAround()

csharp
// Rotates the transform about axis passing through point in world coordinates by angle degrees.
public void RotateAround(Vector3 point, Vector3 axis, float angle);

// Spin the object around the target at 20 degrees/second.
Transform target;
transform.RotateAround(target.position, Vector3.up, 20 * Time.deltaTime);

Transform.LookAt()

csharp
// Points the positive 'Z' (forward) side of an object at a position in world space.
public void LookAt(Transform target);
public void LookAt(Transform target, Vector3 worldUp = Vector3.up);

// Rotate the object's forward vector to point at the target Transform.
Transform target;
transform.LookAt(target);

// Same as above, but setting the worldUp parameter to Vector3.left in this example turns the object on its side.
transform.LookAt(target, Vector3.left);

Quaternion.LookRotation()

csharp
// Creates a rotation with the specified forward and upwards directions.
public static Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);

// The following code rotates the object towards a target object.
Vector3 direction = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = rotation;

Quaternion.FromToRotation()

csharp
// Creates a rotation (a Quaternion) which rotates from fromDirection to toDirection.
public static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection);

// Sets the rotation so that the transform's y-axis goes along the z-axis.
transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);

Quaternion.ToAngleAxis()

csharp
// Converts a rotation to angle-axis representation (angles in degrees).
// In other words, extracts the angle as well as the axis that this quaternion represents.
public void ToAngleAxis(out float angle, out Vector3 axis);

// Extracts the angle - axis rotation from the transform rotation
float angle = 0.0f;
Vector3 axis = Vector3.zero;
transform.rotation.ToAngleAxis(out angle, out axis);

Physics

Raycast

csharp
void FixedUpdate() {
    // Bit shift the index of the layer (8) to get a bit mask
    int layerMask = 1  For the complete guide with collision matrix, 2D events, and common patterns, see [Collisions & Triggers](docs/physics/collisions.md)

#### Collision Events (Physics Response)

```csharp
// Called when collision begins
void OnCollisionEnter(Collision collision)
{
    Debug.Log("Hit: " + collision.gameObject.name);
    
    // Access collision details
    Vector3 impactVelocity = collision.relativeVelocity;
    ContactPoint contact = collision.GetContact(0);
    Vector3 hitPoint = contact.point;
}

// Called every frame during collision
void OnCollisionStay(Collision collision) { }

// Called when collision ends
void OnCollisionExit(Collision collision) { }

Trigger Events (No Physics Response)

csharp
// Called when entering trigger (requires "Is Trigger" checked on collider)
void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player"))
    {
        // Pickup item, enter zone, etc.
        Destroy(gameObject);
    }
}

// Called every frame while inside trigger
void OnTriggerStay(Collider other) { }

// Called when exiting trigger
void OnTriggerExit(Collider other) { }

2D Equivalents

csharp
void OnCollisionEnter2D(Collision2D collision) { }
void OnTriggerEnter2D(Collider2D other) { }
// Also: OnCollisionStay2D, OnCollisionExit2D, OnTriggerStay2D, OnTriggerExit2D

Requirements

  • Both objects need a Collider (or Collider2D)
  • At least one object needs a Rigidbody (or Rigidbody2D)
  • For triggers: Check "Is Trigger" on at least one collider

Ignore Collision

csharp
// Makes the collision detection system ignore all collisions between collider1 and collider2.
public static void IgnoreCollision(Collider collider1, Collider collider2, bool ignore = true);

// Here we're disabling the collision detection between the colliders of ally and bullet objects.
Transform bullet;
Transform ally;
Physics.IgnoreCollision(bullet.GetComponent(), ally.GetComponent());

Input

Keyboard

csharp
// Returns true during the frame the user starts pressing down the key
if (Input.GetKeyDown(KeyCode.Space)) {
    Debug.Log("Space key was pressed");
}

// Jump is also set to space in Input Manager
if (Input.GetButtonDown("Jump")) {
    Debug.Log("Do something");
}

Mouse

csharp
if (Input.GetAxis("Mouse X")  0) {
    Debug.Log("Mouse moved up");
}

if (Input.GetMouseButtonDown(0)) {
    Debug.Log("Pressed primary button.");
}

if (Input.GetMouseButtonDown(1)) {
    Debug.Log("Pressed secondary button.");
}

if (Input.GetMouseButtonDown(2)) {
    Debug.Log("Pressed middle click.");
}

Touch

csharp
if (Input.touchCount > 0) {
    touch = Input.GetTouch(0);

    if (touch.phase == TouchPhase.Began) {
        Debug.Log("Touch began");
    }

    if (touch.phase == TouchPhase.Moved) {
        Debug.Log("Touch moves");
    }

    if (touch.phase == TouchPhase.Ended) {
        Debug.Log("Touch ended");
    }
}

New Input System

The Input System package is the standard for Unity 6 and recom

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#cheatsheetcsharpunityunity-scripts

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言