Unity入门——重要组件和Api Aholic~茜 2025-08-28 2025-09-07 最小单位GameObject 成员变量
1 2 3 4 5 6 7 8 9 10 11 12 print(this .gameObject.name); this .gameObject.name = "带土" ;print(this .gameObject.activeSelf); print(this .gameObject.isStatic); print(this .gameObject.layer); print(this .gameObject.tag); print(this .gameObject.transform.position);
GameObject中的静态方法 只要得到了一个GameObject对象,就可以得到它身上的所有脚本信息。
1 2 3 4 GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube); obj.name = "11" ; obj.GetComponent();
查找对象 1 2 3 4 5 6 7 8 9 10 11 12 GameObject obj = GameObject.Find("xxx" ); if (obj != null ){ print(obj.name); } GameObject obj = GameObject.FindWithTag("Player" ); if (obj != null ){ print(obj.name); }
查找多个对象 1 2 GameObject[] obj = GameObject.FindGameObjectsWithTag("Player" ); print(obj.Length);
实例化对象(克隆对象)的方法 1 2 3 4 public GameObject obj;GameObject.Instantiate(obj); Instantiate(obj);
删除对象的方法(一般在下一帧删除) 1 2 3 4 5 6 7 8 GameObject.Destroy(obj); GameObject.Destroy(obj,5 ); GameObject.Destroy(this ); GameObject.DestroyImmediate(obj);
一般来说换场景时之前的场景就会被自动移除,可以让他不移除 过场景不移除
1 2 3 GameObject.DontDestroyOnLoad(this .GameObject);
GameObject中的成员方法 创建空物体 1 2 3 4 GameObject obj = new GameObject(); GameObject obj1 = new GameObject("newGameObject" ); GameObject obj2 = new GameObject("newGameObject1" , typeof (xxx));
为对象添加脚本 1 2 xxx x = obj.AddComponent(typeof (xxx)) as xxx; xxx x1 = obj.AddComponent<xxx>();
标签比较 1 2 3 4 5 6 7 8 if (this .gameObject.CompareTag("Player" )){ } if (this .gameObject.tag == Player){ }
设置激活失活
1 2 obj.SetActive(false ); obj.SetActive(true );
次要了解的方法,不建议使用
1 2 3 this 。gameObject.SendMessage("TestFun" );
时间相关Time Time主要用于游戏中参与位移、计时、时间暂停等
时间缩放比例 时间停止
1 2 3 4 Time.timeScale = 0 ; Time.timeScale = 1 ; Time.timeScale = 2 ;
帧间隔时间 帧间隔时间就是最近的一帧用了多长时间(s) 主要是用来计算位移的: 如果希望游戏暂停了就不移动了,就用deltatime。 如果希望不收暂停影响就用unscaleddeltatime。
1 2 3 4 5 print(Time.deltaTime); print(Time.unscaledDeltaTime);
游戏开始到现在的时间 1 2 3 4 print(Time.time); print(Time.unscaledTime);
物理帧间隔时间 1 2 3 4 print(Time.fixedDeltaTime); print(Time.fixedUnscaleDeltaTime);
帧数 从游戏开始到现在跑了多少帧
Vector3 Vector3主要是用来表示三维坐标系中的一个点或者一个向量的。
1 2 3 4 5 6 7 8 Vector3 v = new Vector3(); v.x = 10 ; v.y = 10 ; v.z = 10 ; Vector3 v = new Vector3(10 , 10 ); Vector3 v = new Vector3(10 , 10 , 10 );
1 2 3 4 Vector3 v1 = new Vector3(1. 1. 1 ); Vector3 v2 = new Vector3(1. 1. 1 ); print(v1 + v2); print(v1 - v2);
常用代码 1 2 3 4 5 6 7 8 print(Vector3.zero); print(Vector3.right); print(Vector3.left); print(Vector3.up); print(Vector3.down); print(Vector3.forward); print(Vector3.back); Vector3.Distance(v1,v2);
位置 相对世界坐标系的位置 1 this .transform.position;
相对于父对象 1 this .transform.localposition;
不能单独改变某一个位置的值,只能整体进行赋值
1 2 3 4 5 6 7 8 9 this .transform.position = new Vector3(10 , 10 , 10 );this .transform.position = new Vector3(10 , this .transform.position.y, this .transform.position.z);Vector3 vpos = this .transform.position; vpos.x = 100 ; vpos.y = 100 ; vpos.z = 100 ; this .transform.position = vpos;
对象当前的朝向 1 2 3 this .transform.forward;同理可得其他的朝向
位移 1 2 this .transform.position = this .transform.position + this .transform.forward * 1 * Time.deltaTime;
API 1 2 3 4 5 6 7 8 9 this .transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.World);this .transform.Translate(this .transform.forward * 1 * Time.deltaTime, Space.Self);this .transform.Translate(this .transform.forward * 1 * Time.deltaTime, Space.World);this .transform.Translate(Vector3.forward * 1 * Time.deltaTime, Space.Self);
角度和旋转 角度 1 2 3 4 5 6 this .transform.eulerAngles;this .transform.localEulerAngles;this .transform.eulerAngles = new Vector3(10 ,10 ,10 );
旋转 1 2 3 4 5 6 7 8 9 10 11 12 this .transform.Rotate(new Vector3(0 ,10 * Time.deltaTime, 0 ));this .transform.Rotate(new Vector3(0 ,10 * Time.deltaTime, 0 ), Space.World);this .transform.Rotate(new Vector3(0 ,10 * Time.deltaTime, 0 ), Space.Self);this .transform.Rotate(Vector3.up, 10 * Time.deltaTime, Space.World);this .transform.RotateAround(Vector3.zero, Vector3.up, 10 * Time.deltaTime);
缩放和看向 缩放 1 2 3 4 5 6 7 8 this .transform.lossyScale;this .transform.localScale;this .transform.localScale = new Vector3(3 , 3 , 3 );this .transform.localScale += Vector3.one * Time.deltaTime;
看向 1 2 3 4 5 public transform lookAtObj;this .transform.LookAt(Vector3.zero);this .transform.LookAt(lookAtObj);
父子关系 获取和设置父对象 1 2 3 4 5 6 7 8 9 10 this .transform.parent.name;this .transform.parent = null ;this .transform.parent = GameObject.Find("xxx" ).transform;this .transform.SetParent(null );this .transform.SetParent(GameObject.Find("xxx" ).transform);this .transform.SetParent(GameObject.Find("xxx" ).transform, true );
抛妻弃子 1 2 this .transform.DetachChildren();
获取子对象 1 2 3 4 5 6 7 8 9 10 11 this .transform.Find("xxx" );int t = this .transform.childCount;this .transform.GetChild(0 );for (int i = 0 ; i < t; i++){ }
儿子的操作 1 2 3 4 5 6 7 8 9 10 11 public Transform son;spn.IsChildOf(this .transform); print(son.GetSiblingIndex()); son.SetAsFirstSibling(); son.SetAsLastSibling(); son.SetSiblingIndex(5 );
坐标转换 世界坐标系转本地坐标系 1 2 3 4 5 6 7 8 this .transform.InverseTransformPoint(Vector3.forward);this .transform.InverseTransformDirection(Vector3.forward);this .transform.InverseTransformVector(Vector3.forward);
本地坐标系转世界坐标系 1 2 3 4 5 6 7 8 this .transform.TransformPoint(Vector3.forward);this .transform.TransformDirection(Vector3.forward);this .transform.TransformVector(Vector3.forward);
输入相关内容是写在update函数中的
鼠标和键盘输入 检测鼠标输入 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 Input.mousePosition; if (Input.GetMouseButtonDown(0 )){ } if (Input.GetMouseButtonUp(0 )){ } if (Input.GetMouseButton(0 )){ } Input.mouseScrollDelta
检测键盘输入 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 if (Input.GetKeyDown(keyCode.W)){ } if (Inpute.GetKeyDown("w" )){ } if (Input.GetKeyUp(keyCode.W)){ } if (Input.GetKey(keyCode.W)){ }
检测默认轴输入 1 2 3 4 5 6 7 8 9 10 Input.GetAxis("Horizontal" ); Input.GetAxis("Vertical" ); Input.GetAxis("Mouse X" ); Input.GetAxis("Mouse Y" );
其他 1 2 3 4 5 6 7 8 9 10 11 12 if (Input.anyKey){ } if (Input.anyKeyDown){ } Input.inputString;
手柄输入相关 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 string [] strs = Input.GetJoystickNames();if (Input.GetButtonDown("xxx" )){ } if (Input.GetButtonUp("xxx" )){ } if (Input.GetButton("xxx" )){ }
移动设备触摸相关 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 if (Input.touchCount > 0 ){ Touch t1 = Input.touches[0 ]; t1.position; t1.deltaPosition; } Input.multiTouchEnabled = false ; Input.gyro.enabled = true ; Input.gyro.gravity; Input.gyro.rotationRate; Input.gyro.attitude;
屏幕相关Screen 静态属性 当前分辨率 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 Resolution r = Screen.CurrentResolution; print(r.width + " " + r.height); Screen.Width; Screen.Weight; Screen.sleepTimeout = SleepTimeout.NeverSleep; Screen.fullScreen = true ; Screen.fullScreenMode = FullScreenMode.ExclusiveFullScreen; Screen.fullScreenMode = FullScreenMode.FullScreenWindow; Screen.fullScreenMode = FullScreenMode.MaximizedWindow; Screen.fullScreenMode = FullScreenMode.Windowed; Screen.autorotateToLandscapeLeft = true ; Screen.autorotateToLandscapeRight = true ; Screen.autorotateToPortrait = true ; Screen.autorotateToPortraitUpsideDown = true ; Screen.Orientation = ScreenOrientation.xxx;
静态方法 设置分辨率 1 2 Screen.SetResolution(1920 , 1080 , false );
Camera Camera组件信息 Clear Flags 如何清除背景
skybox天空盒
Solid Color颜色填充
Depth only 只画该图层,背景透明
Dont Clear 不移除,覆盖渲染
Culling Mask 选择性渲染部分层级——可以指定只渲染对应层级的对象
Projection Perspective 透视模式
FOV Axis —— 现场角 轴 决定了光学仪器的视野范围
Field of view——视口大小
Physical Camera(摄影相关)……. orthography 正交摄像机(一般用于制作2D游戏)
Clipping Planes 裁剪平面距离
Viewport Rect 视口范围,屏幕上将绘制该摄像机试图的位置
Depth 渲染顺序上的深度
Redering path 渲染路径
Target Texture 渲染纹理
可以把摄像机画面渲染到一张图上——主要用于制作小地图
在Project右键创建一个Render Texture
Occlusion Culling 是否启用剔除遮挡
Allow HDR 是否允许高动态范围渲染
Allow MSAA 是否允许抗锯齿
Allow Dynamic Resolution 是否允许动态分辨率呈现
Target Display 用于哪个显示器——主要用来开发多个屏幕的平台游戏
Camera代码相关 重要的静态成员 获取摄像机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Camera.main.name; Camera.allCameraCount; Camera[] cas = Camera.allCamera; Camera.onPreCull += (c) => { }; Camera.onPreRender += (c) => { }; Camera.onPostRender += (c) => { };
重要成员 1 2 3 4 5 6 7 8 9 Camera.main.depth = 10 ; Vector3 v = Camera.main.WorldToScreenPoint(this .transform.position); Vector3 x = Input.mousePosition; x.z = 10 ; Vector3 v = Camera.main.ScreenToWorldPoint(x);