荣耀彩票代理

  • 热门专题

Unity基于响应式编程(Reactive programming)入门

作者:伊利丹·怒风  发布日期:2016-11-30 20:23:49
  •  

    背景

    荣耀彩票代理QIANYOUMURONGXIAOPIFUDEYIPIAN《JIEGOUC#YOUXIKUANGJIAuFrameJIANTANYOUXIJIAGOUSHEJI》,YINYONGWENZHONGNEIRONG

    荣耀彩票代理uFrameSHITIGONGJI3DKAIFAZHESHIYONGDEYIGEKUANGJIACHAJIAN,TABENSHENMOFANGLEMVVMZHEIZHONGJIAGOUMOSHI(SHISHISHANGBINGBUBAOHANModelBUFEN,QIEDUOCHULEControllerBUFEN)。YINWEIYONGYUUnity3D,SUOYITAXIANGKAIFAZHETIGONGLEYITAOJIYUEditorDEKESHIHUABIANJIGONGJU,KEYIYONGLAIGUANLIDAIMAJIEGOUDENG。XUYAOZHICHUDESHITADEYIGEZHONGYAODELINIAN,TONGSHIYESHIRUANJIANGONGCHENGZHONGDEYIGEZHONGYAOLINIANJIUSHIGUANZHUFENLI(Separation of concern,SoC)。uFrameJIEZHUKONGZHIFANZHUAN(IoC)/YILAIZHURU(DI)SHIXIANLEZHEIZHONGFENLI,CONGERJINYIBUSHIXIANLEMVVMZHEIZHONGMOSHI。QIEZAI1.5BANBENZHIHOU,YINRULEUniRxKU,YINJINLEXIANGYINGSHIBIANCHENGDESIXIANG。

    荣耀彩票代理DUQILAIGAODASHANG,BENWENZHUYAOXIANGCONGSHIJICHUFA,ZHESHOUZUIHOUYIJU“QIEZAI1.5BANBENZHIHOU,YINRULEUniRxKU,YINJINLEXIANGYINGSHIBIANCHENGDESIXIANG。”,ZAIUnityZHONGRUHESHIYONGXIANGYINGSHIBIANCHENG,RUHESHIYONGUniRxKU。

    荣耀彩票代理DANGRANYIXIALIECHUZHEIMEDUOXINGAINIANXINGDEDONGXI,ZUOWEIXINSHOUBIRANLIJIEQILAIYOUKUNNANDE,DANGRANWOYEXIWANGNISHITIANFUJIONGYIDEREN。ZHEILILIECHUJIDIAN,RUGUONIBULEJIE,QINGZIXINGQUXUEXIHUOZHEFUXI,HUILAIZAIKANYEBUCHI。

    1、LinqJICHU,LinqDEBENZHIJIYUCHUANTONGMINGLINGSHIBIANCHENGDEQUBIEHEYOUDIAN

    2、SHENGMINGSHIBIANCHENGHEMINGLINGSHIBIANCHENGDEGAINIANHEQUBIE

    3、SHENMESHIXIANGYINGSHIBIANCHENG

    4、SHENMESHIGUANCHAZHEMOSHI

    荣耀彩票代理5、RUANJIANBIANCHENGZHONGStreamDEGAINIAN

    荣耀彩票代理HAOLEZHUANGbSHIJIANGUOQULE,RANGWOMENJIANDANDESHUOXIASHENMESHIXIANGYINGSHIBIANCHENG。ZHEILIYEBUFEIHUA,YINYONGYIDUAN,KANDEDONGDEZIRANMINGBAI,BUDONGDEHAISHIBUMINGBAI

    SHENMESHIFANYINGSHIBIANCHENG:FANYINGSHIBIANCHENG(Reactive programming)JIANCHENGRx,TASHIYIGESHIYONGLINQFENGGEBIANXIEJIYUGUANCHAZHEMOSHIDEYIBUBIANCHENGMOXING。JIANDANDIANSHUORx = Observables + LINQ + Schedulers。

    ZHEILIWEISHENMEYAOZAIYOUXIKAIFAZHONGYINRUXIANGYINGSHIBIANCHENGRx,DAANSHIYOUXITEBIESHIHERXBIANCHENG,YINWEIZAIYOUXIZHONGGUANGFANYINGYONGLESHIJIAN(ZHEN)HESHIJIAN(UI)DEGAINIAN,SHIJIANBENSHENSHIYIZHONGLIU,ERSHIJIANYESHIJIYUSHIJIANDEYIZHONGXINHAO(BINGBUSHITEBIEZHUNQUE,YIHUI),ERZHEIZHENGSHIRXSUOSHANZHANGDE。

    600216-20151118151734936-2055036453

    实现

    BENWENYIXILIEWENZHANGZHONGDEJINGLINGSHUBIAOYIDONGHEXULIEZHENDONGHUAWEIJICHU,MEIYOUJICHUDEXIANCANKAOXIACHUANTONGSHIXIANFANGSHIYIXIALIANGPIANWENZHANG

    SHIGUANGZHUYU Unity3DSHIXIAN2DRENWUDONGHUA① UGUI&Native2DXULIEZHENDONGHUA

    SHIGUANGZHUYU Unity3DSHIXIAN2DRENWUDONGHUA② Unity2D DONGHUAXITONG&ZIYUANXIAOLV

    ZHEILIYINRULEUniRxKU,LAISHIXIANJIYUXIANGYINGSHIBIANCHENGJISHENGMINGSHIBIANCHENGDAIMAZHONGGOU,DAIMARUXIA:

    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(_ =>
             {
                 //1、获得当前位置
                 Vector3 curenPosition = this.transform.position;
                 //2、获得方向
                 if (Input.GetButton('Fire1'))
                 {
    荣耀彩票代理                  Vector3 moveToward = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                     moveDirection = moveToward - curenPosition;
                     moveDirection.z = 0;
                     moveDirection.Normalize();
                 }
                 //3、插值移动
                 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(_ =>
                {
                    //1、获得当前位置
                    Vector3 curenPosition = this.transform.position;
                    //2、获得方向
                    if (Input.GetButton('Fire1'))
                    {
                        Vector3 moveToward = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                        moveDirection = moveToward - curenPosition;
                        moveDirection.z = 0;
                        moveDirection.Normalize();
                    }
                    //3、插值移动
                    Vector3 target = moveDirection*speed + curenPosition;
                    transform.position = Vector3.Lerp(curenPosition, target, Time.deltaTime);
                });
        }
    }

    总结

    荣耀彩票代理ZHEILIJIZHUUniRxLIANGGEFANGFA Observable.EveryUpdate,Observable.IntervalFrame(ZHEILIHAIJIDEYIQIANWENZHANGLITIDEDINGSHIQIMA,ZHEIGEDINGSHIQIZENMEYANGJIANDANBA),HAIYOUObservableWWW.GetWWW(SHANGYIPIANDEYIGEYIBUJIAZAIZIYUANDEHANSHU),CAIYONGSHENGMINGSHIBIANCHENGDEFANGSHI,KANKANHANSHUMINGJIUZHIDAOSHIGANSHENMEDELEBA,HAIYONGKANWENDANGHUOZHEJIESHISHENMEMA?

    WENZHANGNEIRONGBIJIAOJIANDAN,SHIXIANDEGONGNENGYEJIANDAN,HANSHUYEJIANDAN,XIWANGNIMENXIHUAN。

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规