🎮
MonoBehaviour
Input
一种是命名好的,一种是对应的码
检测空格按着 Input.GetKey(“space”);
检测空格按下 Input.GetKeyDown(“space”);
键名大全
十字移动: up down left right
KeyCode.A👉Z
鼠标事件
鼠标按下👈🗡 GetMouseButtonDown(0)
鼠标按着👈🗡 GetMouseButton(0)
鼠标抬起👈🗡 GetMouseButtonUp(0)
请留意:
0代表👈🗡
1👉🗡
2🀄🗡
检测两个物体碰撞
OnCollisionEnter是Mono内置函数
1 2 3 4 5 6 7 8 9
| void OnCollisionEnter(Collision collision) { if (collision.transform.name == "Floor") { Debug.Log("hit the floor"); } }
|
旋转
transform.RotateAround(参数一:某个点(相当于公转),参数二:这个物体的某个轴(相当于自传),参数三:角度(旋转的速度));
机位
只能有一个摄像机生效,但您可以根据需要来使用代码来更改
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameController : MonoBehaviour { public GameObject[] gameCameras; int gameCamerasCount = 0; private void Start() { print(gameCameras.Length); focusOnCamera(gameCamerasCount); } private void Update() { if (Input.GetMouseButtonDown(0)) { changeCamera(1);
} } void focusOnCamera(int index) {
for (int i = 0; i < gameCameras.Length; i++) { gameCameras[i].SetActive(i == index); } } void changeCamera(int direction) { gameCamerasCount += direction; if (gameCamerasCount >= gameCameras.Length) { gameCamerasCount = 0; } if (gameCamerasCount < 0) { gameCamerasCount = gameCameras.Length - 1; } focusOnCamera(gameCamerasCount); } }
|
相机追逐
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ChaseCamera : MonoBehaviour {
public Transform target; void Start() { }
void Update() { transform.LookAt(target); } }
|
在一个脚本内,访问/修改其他脚本文件中的属性
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
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class player : MonoBehaviour {
public GameObject bulletPrefab; void Start() { }
void Update() { if (Input.GetMouseButtonDown(0)) { GameObject bulletObject = Instantiate(bulletPrefab); bullet bullet = bulletObject.GetComponent<bullet>(); Vector3 shootingDirection = new Vector3( .3f, .2f, 1 ); bullet.shootingDirection = shootingDirection.normalized; } {
} } }
|
物体生命周期
以时间为判断
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
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class bullet : MonoBehaviour { public float shootingForce = 10f; public Vector3 shootingDirection; public float lifeTime = 3f; void Start() { GetComponent<Rigidbody>().AddForce(shootingDirection * shootingForce); }
void Update() { lifeTime -= Time.deltaTime; if(lifeTime <= 0) { Destroy(gameObject); } } }
|
String
CompareTo
这是字符串比较的函数,用法👇
1 2 3 4 5
| string s1 = “c”; string s2 = “b”; if(s1.CompareTo(s2)==1) { }
|
它有三个返回值
当s1>s2时,s1.CompareTo(s2)==1
当s1=s2时,s1.CompareTo(s2)==0
当s1<s2时 s1.CompareTo(s2)==-1
以上为例,c的 ascⅡ码 大于b,所以返回1
Replace
s.Replace(“.”,”-“)
用-来替换s里的所有的.
Split
指定一个字符来切割元字符串
Substring
1 2 3
| string s = "www.llbwy.com";
s.Substring(4,5);
|
ToLower & ToUpper
转换成 小写 和 大写
Trim
删除字符串首尾的空白,请留意是首尾
IndexOf & LastIndexOf
在一个字符串中查找有没有包含的字符串,有的话返回第括号内字符第一次出现的下标,不存在的话返回-1
常常用来判断字符串内是否包含一个子字符串
LastIndexOf查看这个特定字符串最后出现的位置
区分大小写
IndexOfAny & LastIndexOfAny
1 2 3 4 5 6 7
| String s = “Hello”;
char[] anyOf={'H','e','l'};
int i1 = s.IndexOfAny(anyOf));
int i2 = s.LastIndexOfAny(anyOf));
|
区分大小写