目录
对于刚学unity的新手,c#脚本基础尤为重要,一文带你搞定一些简单实用的小脚本,让你轻松入门。
GameObject
GameObject 是 Unity 中的核心类,代表场景中的一个对象。
GameObject.Find(string name):根据名称查找对象。GameObject.CreatePrimitive(PrimitiveType type):创建一个带有基本碰撞体的对象(如 Cube、Sphere)。GameObject.SetActive(bool isActive):设置对象的激活状态。GameObject.AddComponent<T>():为对象添加组件。GameObject.GetComponent<T>():获取对象上的指定组件。GameObject.GetComponents<T>():获取对象上的所有指定组件。GameObject.GetComponentInChildren<T>():获取对象子级中的指定组件。GameObject.GetComponentInParent<T>():获取对象父级中的指定组件。GameObject.FindWithTag(string tag):根据标签查找对象。GameObject.CompareTag(string tag):检查对象是否带有指定标签。
Transform
Transform 是 Unity 中用于控制对象位置、旋转和缩放的组件。
Transform.position:获取或设置对象的世界坐标位置。Transform.localPosition:获取或设置对象相对于父级的位置。Transform.rotation:获取或设置对象的世界旋转(四元数)。Transform.localRotation:获取或设置对象相对于父级的旋转。Transform.eulerAngles:获取或设置对象的欧拉角旋转。Transform.localScale:获取或设置对象的缩放比例。Transform.Translate(Vector3 translation):按指定方向移动对象。Transform.Rotate(Vector3 eulerAngles):按指定欧拉角旋转对象。Transform.LookAt(Transform target):让对象面向目标位置。Transform.parent:获取或设置对象的父级。Transform.SetParent(Transform parent):设置对象的父级。Transform.GetChild(int index):获取指定索引的子对象。Transform.childCount:获取子对象的数量。Transform.Find(string name):根据名称查找子对象。
Vector
Vector 是 Unity 中用于表示向量和点的结构体。常见的向量类型包括 Vector2 和 Vector3。
Vector3.zero:表示零向量 (0, 0, 0)。Vector3.one:表示单位向量 (1, 1, 1)。-
Vector3.up:表示世界坐标系中的“上”方向。值为Vector3(0, 1, 0)。 -
Vector3.down:表示世界坐标系中的“下”方向。值为Vector3(0, -1, 0)。 -
Vector3.right:表示世界坐标系中的“右”方向。值为Vector3(1, 0, 0)。 -
Vector3.left:表示世界坐标系中的“左”方向。值为Vector3(-1, 0, 0)。 -
Vector3.forward:表示世界坐标系中的“前”方向。值为Vector3(0, 0, 1)。 -
Vector3.back:表示世界坐标系中的“后”方向。值为Vector3(0, 0, -1)。 Vector3.Lerp(Vector3 a, Vector3 b, float t):在两个向量之间线性插值。Vector3.Distance(Vector3 a, Vector3 b):计算两个向量之间的距离。Vector3.Normalize():将向量归一化。Vector3.Dot(Vector3 a, Vector3 b):计算两个向量的点积。Vector3.Cross(Vector3 a, Vector3 b):计算两个向量的叉积。
Quaternion
Quaternion 是 Unity 中用于表示旋转的结构体,避免了欧拉角的万向节锁问题。
Quaternion.identity:表示无旋转。Quaternion.Euler(Vector3 eulerAngles):将欧拉角转换为四元数。
Input
Input 类用于处理玩家的输入(如键盘、鼠标、触摸、控制器等)。
Input.GetKey(KeyCode key):当指定键按住时返回true。Input.GetMouseButtonDown(int button):当指定鼠标按钮按下时返回true。Input.mousePosition:获取当前鼠标的位置(屏幕坐标)。Input.GetAxis("Horizontal"):获取水平轴的输入值。Input.GetAxis("Vertical"):获取垂直轴的输入值。Input.touchCount:获取当前触摸点的数量。Input.GetTouch(int index):获取指定索引的触摸点信息。
Physics / Physics2D
Physics 和 Physics2D 类用于处理物理相关的功能(如碰撞检测、射线检测等)。
Physics.Raycast(Ray ray, out RaycastHit hit):检测射线是否与碰撞体相交。Physics2D.Raycast(Vector2 origin, Vector2 direction):2D 射线检测。Physics2D.OverlapCircle(Vector2 point, float radius):检测圆形范围内的碰撞体。Physics.SphereCast(Vector3 origin, float radius, Vector3 direction):球形射线检测。
Rigidbody / Rigidbody2D
Rigidbody 和 Rigidbody2D 用于在 Unity 中实现物理效果。
Rigidbody.velocity:获取或设置刚体的速度。Rigidbody.angularVelocity:获取或设置刚体的角速度。Rigidbody.AddForce(Vector3 force):对刚体施加力。Rigidbody.AddTorque(Vector3 torque):对刚体施加扭矩。Rigidbody2D.AddTorque(float torque):对 2D 刚体施加扭矩。
Time
Time 类用于处理与时间相关的功能(如帧时间、时间缩放等)。
Time.deltaTime:获取上一帧到当前帧的时间(秒)。Time.time:获取游戏开始到当前的总时间(秒)。Time.timeScale:控制游戏时间的缩放比例(如暂停游戏)。
Camera
Camera 类用于控制摄像机的功能。
Camera.main:获取主摄像机(通过MainCamera标签)。Camera.ScreenPointToRay(Vector3 screenPosition):从摄像机发出一条射线,指向屏幕上的指定位置。Camera.WorldToScreenPoint(Vector3 worldPosition):将世界坐标转换为屏幕坐标。Camera.ViewportToScreenPoint(Vector3 viewportPosition):将视口坐标转换为屏幕坐标。Camera.fieldOfView:获取或设置摄像机的视野角度。Camera.orthographic:获取或设置摄像机是否为正交投影。Camera.backgroundColor:获取或设置摄像机的背景颜色。
Animator
Unity 的动画系统用于控制对象的动画播放。
Animator.SetTrigger(string triggerName):设置触发器,触发动画状态。Animator.SetBool(string parameterName, bool value):设置布尔参数。Animator.SetFloat(string parameterName, float value):设置浮点参数。Animator.SetInteger(string parameterName, int value):设置整数参数。Animator.Play(string stateName):播放指定动画状态。
2万+

被折叠的 条评论
为什么被折叠?



