Vector 结构体#
Vector3#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class Vector : MonoBehaviour |
| { |
| |
| void Start() |
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class Vector : MonoBehaviour { |
| |
| void Start() { |
| |
| Vector3 v = new Vector3(); |
| |
| |
| |
| |
| |
| |
| |
| |
| v = Vector3.right; |
| |
| Vector3 v2 = Vector3.forward; |
| |
| |
| Debug.Log(Vector3.Angle(v, v2)); |
| |
| Debug.Log(Vector3.Distance(v, v2)); |
| |
| Debug.Log(Vector3.Dot(v, v2)); |
| |
| Debug.Log(Vector3.Cross(v, v2)); |
| |
| Debug.Log(Vector3.Lerp(Vector3.zero, Vector3.one, 0.5f)); |
| |
| Debug.Log(v.magnitude); |
| |
| Debug.Log(v.normalized); |
| } |
| |
| |
| void Update() { |
| |
| } |
| } |
| |
| |
Quaternion 旋转#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class Rotate : MonoBehaviour { |
| |
| void Start() { |
| |
| Vector3 rotate=new Vector3(0,30,0); |
| Quaternion quaternion=Quaternion.identity; |
| |
| quaternion = Quaternion.Euler(rotate); |
| |
| rotate = quaternion.eulerAngles; |
| |
| quaternion = Quaternion.LookRotation(new Vector3(0, 0, 0)); |
| } |
| |
| |
| void Update() { |
| |
| } |
| } |
| |
Debug 调试类#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class DebugTest : MonoBehaviour { |
| |
| void Start() { |
| Debug.Log("Test"); |
| Debug.LogWarning("TestWarning"); |
| Debug.LogError("TestError"); |
| } |
| |
| |
| void Update() { |
| |
| Debug.DrawLine(new Vector3(1, 0, 0), new Vector3(1, 1, 0), Color.blue); |
| |
| Debug.DrawRay(new Vector3(1, 0, 0), new Vector3(1, 1, 0), Color.red); |
| } |
| } |

GameObject 游戏对象类#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class Empty : MonoBehaviour { |
| public GameObject square; |
| |
| public GameObject preFab; |
| |
| |
| void Start() { |
| |
| GameObject go = this.gameObject; |
| |
| Debug.Log(go.name); |
| |
| Debug.Log(go.tag); |
| |
| Debug.Log(go.layer); |
| |
| Debug.Log(square.name); |
| Debug.Log(square.activeInHierarchy); |
| Debug.Log(square.activeSelf); |
| square.SetActive(true); |
| |
| Transform trans = this.transform; |
| Debug.Log(trans.position); |
| |
| BoxcastCommand bc = GetComponent<BoxcastCommand>(); |
| |
| |
| |
| gameObject.AddComponent<AudioSource>(); |
| square.AddComponent<AudioSource>(); |
| |
| GameObject test = GameObject.Find("Test"); |
| Debug.Log(test.name); |
| |
| test = GameObject.FindWithTag(test.tag); |
| Debug.Log(test.tag); |
| |
| Instantiate(preFab); |
| Instantiate(preFab, trans); |
| GameObject goo = Instantiate(preFab, Vector3.zero, Quaternion.identity); |
| |
| Destroy(goo); |
| } |
| |
| |
| void Update() { |
| |
| } |
| } |

Time 时间类#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class TimeTest : MonoBehaviour |
| { |
| private float timer = 0; |
| |
| |
| void Start() |
| { |
| |
| Debug.Log(Time.time); |
| |
| Debug.Log(Time.timeScale); |
| |
| Debug.Log(Time.fixedDeltaTime); |
| |
| } |
| |
| |
| void Update() |
| { |
| |
| timer += Time.deltaTime; |
| |
| Debug.Log(Time.deltaTime); |
| } |
| } |
| |
Application 类#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class ApplicationTest : MonoBehaviour { |
| |
| void Start() { |
| |
| Debug.Log(Application.dataPath); |
| |
| Debug.Log(Application.persistentDataPath); |
| |
| Debug.Log(Application.streamingAssetsPath); |
| |
| Debug.Log(Application.temporaryCachePath); |
| |
| Debug.Log(Application.runInBackground); |
| |
| Application.OpenURL("https://www.baidu.com/"); |
| |
| Application.Quit(); |
| } |
| |
| |
| void Update() { |
| |
| } |
| } |
场景相关类 Scene、SceneManager#

(1)选择左上角的“文件”
(2)选择“生成设置”
(3)看到“Build 中的场景”
(4)
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| using UnityEngine.SceneManagement; |
| |
| public class SceneBreak : MonoBehaviour { |
| |
| void Start() { |
| |
| |
| SceneManager.LoadScene(1); |
| SceneManager.LoadScene("MyScene"); |
| |
| Scene scene = SceneManager.GetActiveScene(); |
| Debug.Log(scene.name); |
| |
| Debug.Log(scene.isLoaded); |
| |
| Debug.Log(scene.path); |
| |
| Debug.Log(scene.buildIndex); |
| |
| GameObject[] gos = scene.GetRootGameObjects(); |
| Debug.Log(gos.Length); |
| |
| |
| |
| Scene newScene = SceneManager.CreateScene("newSceneName"); |
| |
| Debug.Log(SceneManager.sceneCount); |
| |
| SceneManager.UnloadSceneAsync(newScene); |
| |
| SceneManager.LoadScene("MyScene",LoadSceneMode.Additive); |
| } |
| |
| |
| void Update() { |
| |
| } |
| } |
| |
异步加载场景和读取进度条#
同步和异步的区别:

| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| using UnityEngine.SceneManagement; |
| |
| public class SceneLoadAsync : MonoBehaviour { |
| AsyncOperation operation; |
| |
| |
| void Start() { |
| StartCoroutine(LoadScene()); |
| } |
| |
| |
| IEnumerator LoadScene() { |
| operation = SceneManager.LoadSceneAsync(1); |
| |
| operation.allowSceneActivation = false; |
| yield return operation; |
| } |
| |
| |
| float timer = 0; |
| |
| |
| void Update() { |
| |
| Debug.Log(operation.progress); |
| |
| timer += Time.deltaTime; |
| |
| if (timer > 5) { |
| operation.allowSceneActivation = true; |
| } |
| } |
| } |
| |
每个游戏对象都有Transform,包含位置、旋转和缩放。
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class TransformTest : MonoBehaviour { |
| |
| void Start() { |
| |
| Debug.Log(transform.position); |
| Debug.Log(transform.localPosition); |
| |
| Debug.Log(transform.rotation); |
| Debug.Log(transform.localRotation); |
| Debug.Log(transform.eulerAngles); |
| Debug.Log(transform.localEulerAngles); |
| |
| Debug.Log(transform.localScale); |
| |
| Debug.Log(transform.forward); |
| Debug.Log(transform.right); |
| Debug.Log(transform.up); |
| } |
| |
| |
| void Update() { |
| |
| transform.LookAt(Vector3.zero); |
| |
| transform.Rotate(Vector3.up,10); |
| |
| transform.RotateAround(Vector3.zero, Vector3.up, 5); |
| |
| transform.Translate(Vector3.forward * 0.1f); |
| } |
| } |
| |
Transform处理父子关系

| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class TransformTest : MonoBehaviour { |
| |
| void Start() { |
| |
| |
| |
| Debug.Log(transform.childCount); |
| |
| transform.DetachChildren(); |
| |
| Transform trans = transform.Find("Child"); |
| trans = transform.GetChild(0); |
| |
| bool res=trans.IsChildOf(transform); |
| Debug.Log(res); |
| |
| trans.SetParent(transform); |
| } |
| |
| |
| void Update() { |
| } |
| } |
| |
鼠标使用#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class KeyboradTest : MonoBehaviour { |
| |
| void Start() { |
| |
| } |
| |
| |
| void Update() { |
| |
| |
| if(Input.GetMouseButtonDown(0)) { |
| Debug.Log("按下鼠标左键了"); |
| } |
| |
| if (Input.GetMouseButton(0)) { |
| Debug.Log("持续按下鼠标左键"); |
| } |
| |
| if(Input.GetMouseButtonUp(0)) { |
| Debug.Log("抬起鼠标左键了"); |
| } |
| } |
| } |
| |
键盘使用#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class KeyboradTest : MonoBehaviour { |
| |
| void Start() { |
| |
| } |
| |
| |
| void Update() { |
| |
| if(Input.GetKeyDown(KeyCode.A)) { |
| Debug.Log("按下了A"); |
| } |
| |
| if (Input.GetKey("a")) { |
| Debug.Log("持续按下了A"); |
| } |
| |
| if (Input.GetKeyUp(KeyCode.A)) { |
| Debug.Log("抬起了A"); |
| } |
| } |
| } |
虚拟轴 虚拟按键#
虚拟轴是一个数值在-1~1内的数轴,最重要的就是-1、0、1。

解决什么问题?
解决游戏在不同平台上的操作兼容问题。比如:电脑上有键盘操作,而Switch上没有键盘。
所以让游戏代码中控制虚拟轴,虚拟轴在不同平台上进行相应的控制。
设置
(1)选择左上角的“编辑”
(2)选择“项目设置”
(3)看到“输入管理器”
(4)
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class AxisTest : MonoBehaviour { |
| |
| void Start() { |
| |
| } |
| |
| |
| void Update() { |
| |
| float horizontal = Input.GetAxis("Horizontal"); |
| |
| float vertical = Input.GetAxis("Vertical"); |
| Debug.Log(horizontal+" "+vertical); |
| |
| |
| if (Input.GetButtonDown("Jump")) { |
| Debug.Log("空格按下"); |
| } |
| if (Input.GetButton("Jump")) { |
| Debug.Log("空格持续按下"); |
| } |
| if (Input.GetButtonUp("Jump")) { |
| Debug.Log("空格抬起"); |
| } |
| } |
| } |
手机平板-触摸操作#
| using System.Collections; |
| using System.Collections.Generic; |
| using UnityEngine; |
| |
| public class TouchTest : MonoBehaviour { |
| |
| void Start() { |
| |
| Input.multiTouchEnabled = true; |
| } |
| |
| |
| void Update() { |
| |
| if (Input.touchCount == 1) { |
| |
| Touch touch = Input.touches[0]; |
| |
| Debug.Log(touch.position); |
| |
| switch (touch.phase) { |
| case TouchPhase.Began: |
| break; |
| case TouchPhase.Moved: |
| break; |
| case TouchPhase.Stationary: |
| break; |
| case TouchPhase.Ended: |
| break; |
| case TouchPhase.Canceled: |
| break; |
| } |
| } |
| |
| |
| if(Input.touchCount == 2) { |
| Touch touch1 = Input.touches[0]; |
| Touch touch2 = Input.touches[1]; |
| |
| Debug.Log(touch1.position); |
| |
| switch (touch1.phase) { |
| case TouchPhase.Began: |
| break; |
| case TouchPhase.Moved: |
| break; |
| case TouchPhase.Stationary: |
| break; |
| case TouchPhase.Ended: |
| break; |
| case TouchPhase.Canceled: |
| break; |
| } |
| |
| Debug.Log(touch2.position); |
| |
| switch (touch2.phase) { |
| case TouchPhase.Began: |
| break; |
| case TouchPhase.Moved: |
| break; |
| case TouchPhase.Stationary: |
| break; |
| case TouchPhase.Ended: |
| break; |
| case TouchPhase.Canceled: |
| break; |
| } |
| } |
| } |
| } |
音乐添加 AudioClip AudioSource#
直接添加#
摄像机上记得勾选 
在物体对象中添加组件 Audio Source,里面进行相关设置 
示例脚本#


视频播放 VideoPlayer VideoClip#
在要播放的物体中,添加 Video Player 组件。

简单操作:

脚本编写与音乐播放类似
角色控制器 CharacterController#
为游戏物体添加 CharacterController 组件

示例脚本

为物体添加重力#
为游戏物体添加 Rigidbody 组件

碰撞器 Colliding#
产生条件:(1)碰撞的2个物体必须有碰撞器(2)其中至少一个有刚体(Rigidbody)
三个方法:
(1)OnCollisionEnter 监听发生碰撞,只执行一次
(2)OnCollisionStay 持续碰撞(3)OnCollisionExit 结束碰撞时
脚本作用:火焰碰撞到平面,火焰消失,产生爆炸效果,过一秒后,爆炸效果也消失
(1)把这个火焰脚本放到火焰那个游戏物体中

**(2)把下面的脚本放到爆炸图那个游戏物体中,就在项目Assets中的图片直接设置 **——过1秒就会自动销毁自身

这个示例说明,每个脚本作用放到自身要作用的游戏物体身上,不要弄复杂。一个脚本一个作用。
触发器#
在给物体添加刚体组件,其中有个 ”是触发器“ 选项,选择勾选,这样就变成触发器了。


特殊的物理关节#


固定关节#

物理材质的影响#

游戏中的红外线,射线检测#


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?