6210人加入学习
(22人评价)
Unity常用API方法与类详细讲解 - 知识理论篇

制作完成于2022年3月8日,基于Unity2020.3

价格 免费

undate

1.调用情况

a.游戏物体被激活

b,脚本组件被激活

Late Update

1.调用情况

a.游戏物体被激活

b,脚本组件被激活

[展开全文]

Start

1,调用情况

a.游戏物体被激活

b,脚本组件被激活

2,调用时间,次数作用

在脚本实例化激活是在第一帧的Update之前被调用

后于Awake执行,方便控制逻辑的前后调用顺序

[展开全文]

OnEnable

1,调用情况

a,游戏物体被激活

b,脚本组件被激活

2,调用时间,次数与作用

每次游戏物体或脚本被激活都会调用一次

重复赋值,变为初始状态

 

 

Start:

1,调用情况

a,游戏物体被激活

b,脚本组件被激活

[展开全文]

OnEnable(每次激活都会被调用 )

Awake(首次激活被调用)

1.在加载场景时初始化包含脚本的激活状态的Gameject时

2,GameObject从非激活状态变为激活状态

3,在初始化使用Instantiate创建的GameObject之后

2.调用时间次数与作用

在脚本的生存期内,unity仅调用Awake一次。脚本的生存期持续到包含

 

[展开全文]

//使用translate让物体移动 可能涉及的情况 //0.第二个参数不填(实际情况按自身坐标系移动) //grisGo.transform.Translate(Vector2.left * moveSpeed);//自身坐标系 grisGo.transform.Translate(-grisGo.transform.right * moveSpeed);//世界坐标系 //1.第一个参数按世界坐标系移动,第二个参数指定世界坐标系(实际情况按世界坐标系移动) //grisGo.transform.Translate(Vector2.left * moveSpeed,Space.World); //2.第一个参数按世界坐标系移动,第二个参数指定自身坐标系(实际情况按自身坐标系移动) //grisGo.transform.Translate(Vector2.left * moveSpeed, Space.Self); //3.第一个参数按自身坐标系移动,第二个参数指定世界坐标系(实际情况按自身坐标系移动) //grisGo.transform.Translate(-grisGo.transform.right * moveSpeed, Space.World); //4.第一个参数按自身坐标系移动,第二个参数指定自身坐标系(实际情况按世界坐标系移动)(一般不使用) //grisGo.transform.Translate(-grisGo.transform.right * moveSpeed, Space.Self);

[展开全文]

using System.Collections; using System.Collections.Generic; using UnityEngine; ///

/// 刚体(通过物理模拟控制位置) ///

public class No17_Rigidbody : MonoBehaviour { private Rigidbody2D rb; private float moveSpeed = 1; private float angle = 60; void Start() { rb=GetComponent(); print(rb.position); print(rb.gravityScale + ",该对象受重力影响的程度"); rb.gravityScale = 0; //rb.AddForce(Vector2.right * 10);//持续的力 //rb.AddForce(Vector2.right * 10,ForceMode2D.Impulse);//爆发力 rb.velocity = Vector2.right * moveSpeed; } // Update is called once per frame void Update() { } //物理系统一般存放在此 void FixedUpdate() { //rb.MovePosition(rb.position + Vector2.right * moveSpeed * Time.fixedDeltaTime); rb.MoveRotation(rb.rotation + angle*Time.fixedDeltaTime);//一直旋转着 } }

[展开全文]

using System.Collections; using System.Collections.Generic; using UnityEngine; ///

/// 延时调用方法 ///

public class No16_Invoke : MonoBehaviour { public GameObject grisGo; void Start() { //Invoke("CreateGris", 3); InvokeRepeating("CreateGris", 1, 1); InvokeRepeating("Test", 1, 1); //停止 CancelInvoke("CreateGris"); //CancelInvoke();//停止所有 } void Update() { print(IsInvoking("CreateGris")); print(IsInvoking()); } private void CreateGris() { Instantiate(grisGo); Debug.Log("延时调用"); } private void Test() { } }

[展开全文]

using System.Collections; using System.Collections.Generic; using UnityEngine; ///

/// 鼠标回调事件(MonoBehaviour里的方法) ///

public class No14_OnMouseEvent : MonoBehaviour { private void OnMouseDown() { print("在Gris身上按下了鼠标"); } private void OnMouseUp() { print("在Gris身上按下的鼠标抬起了"); } private void OnMouseDrag() { print("在Gris身上鼠标进行拖拽操作"); } private void OnMouseEnter() { print("鼠标移入了Gris"); } private void OnMouseExit() { print("鼠标移出了Gris"); } private void OnMouseOver() { print("鼠标悬停在Gris上方"); } private void OnMouseUpAsButton() { print("鼠标在Gris身上松开了"); } }

[展开全文]

using System.Collections; using System.Collections.Generic; using UnityEngine; public class No10_Animator : MonoBehaviour { public Animator animator; void Start() { animator.Play("Run"); animator.speed = 5; animator.speed = 0.5f; animator.SetFloat("Speed", 1); animator.SetBool("Dead",false); animator.SetInteger("HP", 100); print("当前速度参数是:" + animator.GetFloat("Speed")); } // Update is called once per frame void Update() { //按键切换动画状态 if (Input.GetKeyDown(KeyCode.W)) { animator.CrossFade("Walk", 1f); } } }

[展开全文]