PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对
Part 1 IEnumerator 协程:
防止Unity因大量数据操作而导致,应用程序崩溃
协程的特点:
1、在主线程 里面运行的 任务片段 。
2、分段完成 不是一次性完成 。
常用方式为:使用带有 yield的指令函数
IEnumerator change()
{
yield return null;
}
使用Demo页面跳转:
public class ChangeScence : MonoBehaviour
{
void OnClick()
{
//阻塞式
// SceneManager.LoadScene(1);
StartCoroutine(Change());
Debug.Log(" change Finish!!");
}
// Start is called before the first frame update
void Start()
{
// 切换场景的时候 保留哪些东西不销毁
DontDestroyOnLoad(transform.parent.gameObject);
Button tmpBtn = gameObject.GetComponent<Button>();
tmpBtn.onClick.AddListener(OnClick);
}
AsyncOperation operate;
IEnumerator Change()
{
// 协程加载场景 避免过大 程序卡死
operate = SceneManager.LoadSceneAsync(1);
yield return operate;
}
// Update is called once per frame
void Update()
{
if(operate != null)
Debug.Log("progress=="+ operate.progress);
}
}
Part 2 Animator 动画:
所有动画都是通用的,例如 人的动画有 17个骨骼点, 猩猩动画也有 17个骨骼点, 则这两个动画在一定程度上是可以户用的。
例如这里的 老鼠 采取的就是人的动画
动画管理器
添加动画事件:
切换动画,在动画管理器中 选中 要更切换动画 之间的关联线
代码实现按键切换:
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
//Debug.Log("KeyCode W !!!");
myAnimator.SetInteger("index", 0);
}
if (Input.GetKeyDown(KeyCode.A))
{
//Debug.Log("KeyCode A !!!");
myAnimator.SetInteger("index", 1);
}
if (Input.GetKeyDown(KeyCode.D))
{
//Debug.Log("KeyCode D !!!");
myAnimator.SetInteger("index", 2);
}
}
完成了按键控制动画的功能,然后就是实现,角色移动的功能:
组件移动主要有三种:1、transform.translate
2、刚体添加力 或 速度
3、引用CharacterController组件
以 引用组件为例:
在角色身上添加组件
代码实现:
private void OnAnimatorMove()
{
AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
if(info.IsName("Base Layer.Run"))
{
character.SimpleMove(transform.forward);
}
if (info.IsName("Base Layer.JumpLeft"))
{
character.SimpleMove( - transform.right);
}
if (info.IsName("Base Layer.Right"))
{
character.SimpleMove(transform.right);
}
}
根据以上内容,可完成简易跑酷吃金币的游戏。。。