Unity PureMVC框架案例

引言
PureMVC框架解读上:https://blog.csdn.net/qq_29579137/article/details/73692842
PureMVC框架解读下:https://blog.csdn.net/qq_29579137/article/details/73717882
PureMVC框架案例
这个案例实现了点击两个小球,使两个小球更换位置。
这里写图片描述

1、首先实现两个小球的实体类:

/***************************
* 
* Theme:小球位置的信息类
*
* Description:
* 1 
* 
* 2 
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using UnityEngine;

namespace JLSH {

	public class V3Info
	{
	    public Vector3 postion { set; get; }

	}

}

2、创建Notifacation工具类以免出错:

/***************************
* 
* Theme:MySqlDataBase
*
* Description:
* 1 
* 
* 2 
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using UnityEngine;

namespace JLSH {

	public static class NotifactoionSting
	{
	    public static string CHANGEPOSTION = "changepostion";

	    public static string CHANGEDATA = "changedata";


	}

}

3 创建视图层的Mediator:

/***************************
* 
* Theme:实现视图层
*
* Description:
* 1 实现Ball的位置更新
*   
* 2 
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using System.Collections.Generic;
using DG.Tweening;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

namespace JLSH {

	public class BallMediator :Mediator
	{

	    public const string NAME = "BallMediator";

	    public GameObject redBall, blueBall;

	    public float moveTime=0.5f;
	    public BallMediator( GameObject mainGameObject) : base(NAME)
	    {
	        redBall = mainGameObject.transform.Find("redBall").gameObject;
	        blueBall = mainGameObject.transform.Find("blueBall").gameObject;
	    }

	    public override IList<string> ListNotificationInterests()
	    {
            IList<string> list =new List<string>();
            list.Add(NotifactoionSting.CHANGEDATA);
	        return list;
	    }

	    public override void HandleNotification(INotification notification)
	    {
	        base.HandleNotification(notification);
	        if (notification.Name.Equals(NotifactoionSting.CHANGEDATA))
	        {
	            V3Info[] v3Infos = (V3Info[]) notification.Body;
	            if (redBall.transform.position ==v3Infos[0].postion)
	            {
	                redBall.transform.DOMove(v3Infos[1].postion, moveTime);
	                blueBall.transform.DOMove(v3Infos[0].postion, moveTime);
                }
	            else
	            {
	                redBall.transform.DOMove(v3Infos[0].postion, moveTime);
	                blueBall.transform.DOMove(v3Infos[1].postion, moveTime);
                } 
	        }
	    }
	}

}

4 、实现Proxy :

/***************************
* 
* Theme:数据层的操作
*
* Description:
* 1 修改实体类中的位置
* 
* 2 
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using System.Runtime.CompilerServices;
using DG.Tweening;
using PureMVC.Patterns;
using UnityEngine;

namespace JLSH {

	public class BallProxy : Proxy
	{

	    public const string NAME = "BallProxy";

	    public V3Info redBallInfo, blueBallInfo;

	    private V3Info[] v3Infos;

        public BallProxy():base(NAME)
	    {
            redBallInfo =new V3Info();
	        blueBallInfo = new V3Info();
	        BallMediator ballMediator = (BallMediator) Facade.RetrieveMediator(BallMediator.NAME);
	        redBallInfo.postion = ballMediator.redBall.transform.position;
	        blueBallInfo.postion = ballMediator.blueBall.transform.position;
            v3Infos = new V3Info[2];
	    }


	    /// <summary>
        /// 触发改变位置
        /// </summary>
        /// <param name="redBallTra"></param>
        /// <param name="blueBallTra"></param>
	    public void ChangePostion(Transform BallTra1, Transform BallTra2)
	    {
	         redBallInfo.postion = BallTra1.position;
	        blueBallInfo.postion = BallTra2.position;
            v3Infos[0] = redBallInfo; 
            v3Infos[1] = blueBallInfo;
            SendNotification(NotifactoionSting.CHANGEPOSTION);

	    }

	    public void toChangePostion()
	    {
	        SendNotification(NotifactoionSting.CHANGEDATA, v3Infos);
        }
	}

}

5、实现Commend控制,:

/***************************
* 
* Theme:控制层
*
* Description:
* 1 
* 
* 2 
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using NUnit.Framework.Constraints;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

namespace JLSH {

	public class BallCommend : SimpleCommand
	{
	    public const string NAME = "BallCommend";

	    public BallCommend()
	    {


	    }

	    public override void Execute(INotification notification)
	    {
	        if (notification.Name.Equals(NotifactoionSting.CHANGEPOSTION))
	        {
	            BallProxy ballProxy = (BallProxy) Facade.RetrieveProxy(BallProxy.NAME);
                ballProxy.toChangePostion();
	        }
	    }
    }

}

6 注册BallProxy(M)、BallMediator(V)、BallCommend(C):

/***************************
* 
* Theme:Facade
*
* Description:
* 1 注册
* 
* 2 
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using PureMVC.Patterns;
using UnityEngine;

namespace JLSH {

	public class MyFacade : Facade {

	    public MyFacade( GameObject game)
	    {
	        RegisterMediator(new BallMediator(game));
            RegisterProxy(new BallProxy());
	        RegisterCommand(NotifactoionSting.CHANGEPOSTION, typeof(BallCommend));

        }

	}

}

7、最后创建一个测试桥梁就ok了

/***************************
* 
* Theme:
*
* Description:
* 1 传递参数(game)
* 
* 2 判断条件  触发BallMediator中的changepostion
* 
* 3 
* 
* Data: 程延宏 
* 
***************************/

using Boo.Lang;
using PureMVC.Patterns;
using UnityEngine;

namespace JLSH {

    public class Test : MonoBehaviour
    {
        public Camera MainCamera;
        private List<Transform> transforms;
        [Range(0,1)]
        public float movetime;
        void Start()
        {
            new MyFacade(gameObject);
            BallMediator ballMediator = (BallMediator)Facade.Instance.RetrieveMediator(BallMediator.NAME);
            ballMediator.moveTime = movetime;
            transforms = new List<Transform>();
        }

        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {

                Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
                RaycastHit raycastHit;
                if (Physics.Raycast(ray, out raycastHit))
                {
                    Debug.DrawRay(MainCamera.transform.position, ray.origin, Color.red);
                    GameObject game = raycastHit.collider.gameObject;
                    if (transforms.Count == 0 || game.transform != transforms[0])
                    {
                        transforms.Add(game.transform);
                        if (transforms.Count >= 2)
                        {
                            BallProxy ballProxy = (BallProxy) Facade.Instance.RetrieveProxy(BallProxy.NAME);
                            ballProxy.ChangePostion(transforms[0], transforms[1]);
                            transforms.Clear();
                        }
                        Debug.Log(transforms.Count);
                    }
                }
            }
        }
    }
}

Unity工程目录:
这里写图片描述

超简单的练习。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值