Time.deltaTime
Time.timeScale
Time.time
Time.realtimeSinceStartup
Time.timeSinceLevelLoad
Time.fixedDeltaTime
Time.maximumDeltaTime
Time.smoothDeltaTime
Time.frameCount
void Awake()
void Start()
void Update()
void LateUpdate()
void FixedUpdate()
transform.position
transform.localPosition
transform.eulerAngles
transform.localEulerAngles
transform.rotation
transform.localRotation
transform.localScale
transform.up
transform.forward
transform.parent
transform.localToWorldMatrix
transform.worldToLocalMatrix
transform.root
transform.childCount
transform.lossyScale
transform.Translate(Vector3 translation , Space relativeTo = Space.Self)
transform.Rotate(Vector3 eulers , Space relativeTo = Space.Self)
transform.RotateAround(Vector3 point, Vector3 axis, float angle)
transform.LookAt(Vector3 position)
transform.TransformDirection(Vector3 direction)
transform.InverseTransformDirection(Vector3 direction)
transform.TransformPoint(Vector3 position)
transform.InverseTransformPoint(Vector3 position)
transform.DetachChildren()
transform.Find(string n)
transform.IsChildOf(Transform parent)
Quaternion.LookRotation(Vector3 forward, Vector3 upwards = Vector3.up)
Vector3.MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
Vector3.Lerp(Vector3 a, Vector3 b, float t)
Vector3.Distance(Vector3 a, Vector3 b)
void OnTriggerEnter(Collider c)
void OnTriggerExit(Collider c)
void OnTriggerStay(Collider c)
void OnCollisionEnter(Collision c)
void OnCollisionExit(Collision c)
void OnCollisionStay(Collision c)
getComponent
getComponents
GetComponentInChildren
GetComponentsInChildren
GameObject.Find("GameObject/obj")
GameObject.FindWithTag("player")
GameObject.FindGameObjectsWithTag("player")
Physics.Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
layerMask = 1<
layerMask = ~1<
LayerMask.GetMask("Player", "Default"):检测Player层和Default层
~LayerMask.GetMask("Player", "Default"):检测除Player层和Default层之外的图层
QueryTriggerInteraction.Collider:检测开启触发器的碰撞体
QueryTriggerInteraction.Ignore:不检测开启触发器的碰撞体
QueryTriggerInteraction.UseGlobal:isTrigger钩选时检测,isTrigger不钩选时不检测
协程的创建和使用
void Start(){
//
StartCoroutine("Fun");
//
}
IEnumerator Fun(){
//
yield return new WaitForSeconds(2.0f);
//
}
WaitForFixedUpdate()
WaitForEndOfFrame()
WaitForSecondsRealtime(float time)
WaitUntil(bool fun())
WaitWhile(bool fun())
协程的停止
void Start(){
//第一种写法
StartCoroutine("Fun");
StopCoroutine("Fun");
//第二种写法
IEnumerator ie = Fun();
StartCoroutine(ie);
StopCoroutine(ie);
//第三种写法
Coroutine coroutine=StartCoroutine(Fun());
StopCoroutine(coroutine);
//关闭所有协程
StopAllCoroutines();
}
IEnumerator Fun(){
//
yield return new WaitForSeconds(2.0f);
//
}
委托的创建和使用
public class Player : MonoBehaviour{
public delegate void PlayMotify(); //创建委托的固定写法
public event PlayMotify OnPlay; //定义委托的一个事件
void Play(){
if(this.OnPlay!=null){ //判断是否有其他类接收该事件
this.OnPlay(); //发送该事件
}
}
}
委托事件的接收
public class Game : MonoBehaviour{
public Player player; //创建Player对象
void Start(){
this.player.OnPlay+=Player_OnPlay; //监听player下委托的事件,并调用Player_OnPlay函数
}
void Player_OnPlay(){
//触发事件后,做自己的处理
}
}
Unity自带的委托调用
using UnityEngine.Events;
public class Player : MonoBehaviour{
public UnityAction OnScore; //泛型为OnScore函数参数
void Play(){
if(this.OnScore!=null){ //判断是否有其他类接收该事件
this.OnScore(1); //发送该事件
}
}
}
public class Game : MonoBehaviour{
public Player player; //创建Player对象
void Start(){
this.player.OnScore=GetScore; //监听player下委托的事件,并调用GetScore函数
}
void GetScore(int score){
//触发事件后,做自己的处理
}
}
Invoke(string methodName , float time)
InvokeRepeating(string methodName , float time , float repeatRate)
Instantiate(T original , Vector3 position , Quaternion rotation)
3D游戏物体的运动控制脚本
void Update(){
//竖直轴 -1,0,1
float vertical = Input.GetAxis("Vertical");
//水平轴 -1,0,1
float horizontal = Input.GetAxis("Horizontal");
//方向
Vector3 dir = new Vector3(horizontal,0,vertical);
if(dir != Vector3.zero){
//先转向
transform.rotation=Quaternion.LookRotation(dir);
//后行走
transform.Translate(Vector3.forward * 2 * Time.deltatime);
}
}
2D游戏物体的运动控制脚本
void Update(){
//水平轴 -1,0,1
float horizontal = Input.GetAxis("Horizontal");
//往左(-1)或往右(1)
if(horizontal!=0){
//将x轴的缩放取反,来实现人物转向
transform.localScale=new Vector3(horizontal>0?1:-1, 1, 1);
//移动
transform.Translate(Vector2.right * horizontal * Time.deltaTime);
}
}
导航寻路脚本
//导航
private NavMeshAgent agent;
//玩家
private Transform player;
void Start(){
agent=GetComponent();
player=GameObject.FindWithTag("player").transform;
}
void Update(){
float dis = Vector3.Distance(transform.position,player.position);
if(dis>1.5f){ //与玩家距离大于1.5时,自动寻路到玩家
agent.isStopped=false;
agent.SetDestination(player.position);
}else{
agent.isStopped=true;
}
}
相机跟随目标脚本
//跟随目标
private Transform player;
//向量:摄像机的位置减去目标位置
private Vector3 dir;
void Start(){
player = GameObject.FindWithTag("player").transform;
dir = transform.position-player.position; //保证该差值不变,实现目标的跟踪
}
void Update(){
transform.position = player.position + dir; //更新摄像机的位置
}
AudioSource控制脚本
public class AudioManager:MonoBehaviour{
//单例
public static AudioManager instance;
//背景音乐
public AudioSource musicPlayer;
//音效音乐
public AudioSource soundPlayer;
void Start(){
instance=this;
}
//播放背景音乐
public void PlayMusic(string name){
if(musicPlayer.isPlaying==false){
//播放音乐片段,从资源文件中通过名字来加载音乐文件
AudioClip clip=Resources.Load(name);
//换碟
musicPlayer.clip=clip;
//播放
musicPlayer.play();
}
}
//停止播放
public void StopMusic(){
musicPlayer.stop();
}
//播放音效
public void PlaySound(string name){
//播放音乐片段,从资源文件中通过名字来加载音乐文件
AudioClip clip=Resources.Load(name);
//播放一次
soundPlayer.PlayOneShot(clip);
}
}
Canvas
- Screen Space - Overlay:UI显示在最前方,遮挡其它3D物体
- Screen Space -Camera:摄像机包含整个UI界面,和3D物体有层次关系
- World Space:Canvas完全变为一个3D物体,可自由调节Canvas
Canvas Scaler
- Constant Pixel Size:UI以恒定的像素大小显示
- Scale With Screen Size:UI按屏幕的大小自动缩放
- Constant Physical Size:UI以恒定的物理尺寸显示
- Reference Resolution:参考分辨率,参照UI依据的分辨率,随着分辨率的变化而自动缩放
Anchor Presets
Image
- Simple:简单模式,图片正常拉伸
- Slice:切分样式,可以将图片切分成多块
- Tiled:平铺样式,图片拉伸时,会有相同的图片显示在多余的区域
- Filled:填充样式,可以按特定的填充方式来显示图片
组件中的拖拽条
public class Game:MonoBehaviour{
[Range(0,1)]
public float value;
}
持续学习更新......