自己写的框架用了几年了,也做了几个游戏,应该是经受住项目考验了吧,现在分享下简本。可以直接去下载。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerBase
{
protected MVCMannger _MVCMannger;
///
/// control对应的相应view
///
private ViewBase _viewBase;
///
/// 初始化
///
public virtual void Init()
{
_MVCMannger = MVCMannger._instance;
}
///
/// 销毁
///
public virtual void Destroy() { }
///
///一个control绑定一个view
///
///
public virtual void SetControlView(ViewBase viewBase)
{
_viewBase = viewBase;
_viewBase.Init();
}
public virtual void ShowView(int layer = 1)
{
if (_viewBase == null)
{
Debug.LogError("请先为该control绑定对应view,也就是先调用SetControlView方法");
}
else
{
_viewBase.ShowView(layer);
}
}
///
/// 隐藏ui
///
public virtual void HideView()
{
_viewBase.HideView();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ViewBase
{
//view视图的对象
public GameObject ui;
protected MVCMannger _MVCMannger;
///
/// 初始化
///
public virtual void Init()
{
_MVCMannger = MVCMannger._instance;
if (!ui)
{
var name = this.GetType().ToString();
ui = GameObject.Instantiate(Resources.Load("view/"+name)) as GameObject;
ui.transform.localPosition=Vector3.zero;
ui.transform.localScale=Vector3.one;
ui.name = name;
}
}
///
/// 销毁
///
public virtual void Destroy()
{
GameObject.Destroy(ui);
}
///
/// 取model里的数据给view赋值
///
public virtual void SetData() { }
///
///
/// 0置灰层,1常规层,2对话框层,3提示层
public virtual void ShowView(int layer = 1)
{
ui.SetActive(true);
switch ((Layer)layer)
{
case Layer.greyLayer:
ui.transform.SetParent(_MVCMannger.greyLayer);
break;
case Layer.normalLayer:
ui.transform.SetParent(_MVCMannger.normalLayer);
break;
case Layer.dialogLayer:
ui.transform.SetParent(_MVCMannger.dialogLayer);
break;
case Layer.tipsLayer:
ui.transform.SetParent(_MVCMannger.tipsLayer);
break;
}
ui.transform.localPosition = Vector3.zero;
ui.transform.localScale = Vector3.one;
}
public virtual void HideView()
{
ui.SetActive(false);
ui.transform.SetParent(_MVCMannger.greyLayer);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelBase {
protected MVCMannger _MVCMannger;
///
/// 初始化
///
public virtual void Init()
{
_MVCMannger = MVCMannger._instance;
}
///
/// 销毁
///
public virtual void Destroy() { }
}
上面是mvc的三个基类,对于数据模块要结合项目的实体类来设计,这里简化。有了mvc,怎样使用呢?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ModelBase {
protected MVCMannger _MVCMannger;
///
/// 初始化
///
public virtual void Init()
{
_MVCMannger = MVCMannger._instance;
}
///
/// 销毁
///
public virtual void Destroy() { }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameLaunch : MonoBehaviour {
// Use this for initialization
void Start ()
{
ReadyUI();
LaunchMainUI();
}
///
/// 准备ui系统
///
private void ReadyUI()
{
var ui = GameObject.Find("ui");
ui.transform.position = Vector3.zero;
MVCMannger._instance.Start();
}
///
/// 启动主ui
///
private void LaunchMainUI()
{
//获取相应的control
var control=MVCMannger._instance.GetController();
//显示ui
control.ShowView();
}
}
这两个脚本就是对mvc基类的使用,需要建一个空物体,将GameLaunch.cs挂在上面
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainControl : ControllerBase {
public override void Init()
{
base.Init();
MainView _mainView=new MainView();
SetControlView(_mainView);
}
public override void Destroy()
{
base.Destroy();
}
}
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class MainView : ViewBase
{
public override void Init()
{
base.Init();
}
public override void ShowView(int layer = 1)
{
base.ShowView(layer);
}
public override void SetData()
{
base.SetData();
}
}
这里的MainControl和MainView就是对这里的应用,当然了,还需要一个和view同名的预制体,去链接里下载demo,就可得到。点击打开链接