【Unity3D基础】让物体动起来①--基于UGUI的鼠标点击移动html
【Unity3D基础】让物体动起来②--UGUI鼠标点击逐帧移动编程
时光煮雨 Unity3D让物体动起来③—UGUI DoTween&Unity Native2D实现架构
时光煮雨 Unity3D实现2D人物动画① UGUI&Native2D序列帧动画框架
时光煮雨 Unity3D实现2D人物动画② Unity2D 动画系统&资源效率异步
前有慕容小匹夫的一篇《解构C#游戏框架uFrame兼谈游戏架构设计》,引用文中内容异步编程
uFrame是提供给3D开发者使用的一个框架插件,它自己模仿了MVVM这种架构模式(事实上并不包含Model部分,且多出了Controller部分)。由于用于Unity3D,因此它向开发者提供了一套基于Editor的可视化编辑工具,能够用来管理代码结构等。须要指出的是它的一个重要的理念,同时也是软件工程中的一个重要理念就是关注分离(Separation of concern,SoC)。uFrame借助控制反转(IoC)/依赖注入(DI)实现了这种分离,从而进一步实现了MVVM这种模式。且在1.5版本以后,引入了UniRx库,引进了响应式编程的思想。函数
读起来高大上,本文主要想从实际出发,着手最后一句“且在1.5版本以后,引入了UniRx库,引进了响应式编程的思想。”,在Unity中如何使用响应式编程,如何使用UniRx库。工具
固然一下列出这么多新概念性的东西,做为新手必然理解起来有困难的,固然我也但愿你是天赋迥异的人。这里列出几点,若是你不了解,请自行去学习或者复习,回来在看也不迟。学习
一、Linq基础,Linq的本质及与传统命令式编程的区别和优势动画
二、声明式编程和命令式编程的概念和区别
三、什么是响应式编程
四、什么是观察者模式
五、软件编程中Stream的概念
好了装b时间过去了,让咱们简单的说下什么是响应式编程。这里也不废话,引用一段,看的懂得天然明白,不懂得仍是不明白
什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LINQ风格编写基于观察者模式的异步编程模型。简单点说Rx = Observables + LINQ + Schedulers。
这里为何要在游戏开发中引入响应式编程Rx,答案是游戏特别适合RX编程,由于在游戏中普遍应用了时间(帧)和事件(UI)的概念,时间自己是一种流,而事件也是基于时间的一种信号(并非特别准确,意会),而这正是RX所擅长的。
本文以系列文章中的精灵鼠标移动和序列帧动画为基础,没有基础的先参考下传统实现方式一下两篇文章
时光煮雨 Unity3D实现2D人物动画① UGUI&Native2D序列帧动画
时光煮雨 Unity3D实现2D人物动画② Unity2D 动画系统&资源效率
这里引入了UniRx库,来实现基于响应式编程及声明式编程代码重构,代码以下:
using UnityEngine;
using UniRx;
public class PlayerController : MonoBehaviour
{
public float speed;
private Vector3 moveDirection;
private int currentTexture = 0;
public Sprite[] textureArray;
// Use this for initialization
void Start()
{
//鼠标控制移动,每帧更新
Observable.EveryUpdate()
.Subscribe(_ =>
{
//一、得到当前位置
Vector3 curenPosition = this.transform.position;
//二、得到方向
if (Input.GetButton("Fire1"))
{
Vector3 moveToward = Camera.main.ScreenToWorldPoint(Input.mousePosition);
moveDirection = moveToward - curenPosition;
moveDirection.z = 0;
moveDirection.Normalize();
}
//三、插值移动
Vector3 target = moveDirection * speed + curenPosition;
transform.position = Vector3.Lerp(curenPosition, target, Time.deltaTime);
});
//帧动画
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
//定时器每隔5帧
Observable.IntervalFrame(5).Subscribe(_ =>
{
currentTexture++;
if (currentTexture >= textureArray.Length)
{
currentTexture = 0;
}
spriteRenderer.sprite = textureArray[currentTexture];
});
}
}
是的没有看错,你没有发现熟悉的Update函数,若是说以上函数让你看到就是把全部代码就放在了Start里面而已,咱们再重构一下代码,使用提取方法,看看效果,这就是声明式编程的魅力,程序可读性加强,更适合人类的思惟方式
using UnityEngine;
using UniRx;
public class PlayerController : MonoBehaviour
{
public float speed;
private Vector3 moveDirection;
private int currentTexture = 0;
public Sprite[] textureArray;
// Use this for initialization
void Start()
{
//鼠标控制移动,每帧更新
PlayerMove();
//角色 帧动画
PlayerAnimation();
}
/// <summary>
/// 角色 帧动画控制
/// </summary>
private void PlayerAnimation()
{
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
//定时器每隔5帧
Observable.IntervalFrame(5).Subscribe(_ =>
{
currentTexture++;
if (currentTexture >= textureArray.Length)
{
currentTexture = 0;
}
spriteRenderer.sprite = textureArray[currentTexture];
});
}
/// <summary>
/// 鼠标控制移动,每帧更新
/// </summary>
private void PlayerMove()
{
Observable.EveryUpdate()
.Subscribe(_ =>
{
//一、得到当前位置
Vector3 curenPosition = this.transform.position;
//二、得到方向
if (Input.GetButton("Fire1"))
{
Vector3 moveToward = Camera.main.ScreenToWorldPoint(Input.mousePosition);
moveDirection = moveToward - curenPosition;
moveDirection.z = 0;
moveDirection.Normalize();
}
//三、插值移动
Vector3 target = moveDirection*speed + curenPosition;
transform.position = Vector3.Lerp(curenPosition, target, Time.deltaTime);
});
}
}
这里记住UniRx两个方法 Observable.EveryUpdate,Observable.IntervalFrame(这里还记得之前文章里提的定时器吗,这个定时器怎么样简单吧),还有ObservableWWW.GetWWW(上一篇的一个异步加载资源的函数),采用声明式编程的方式,看看函数名就知道是干什么的了吧,还用看文档或者解释什么吗?
文章内容比较简单,实现的功能也简单,函数也简单,但愿大家喜欢。