引言
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工程目录:
超简单的练习。。。。。。。