码迷,mamicode.com
首页 > 编程语言 > 详细

Unity (十二) MVC框架:关于MVC 编程模式的模拟运用

时间:2017-10-16 19:49:16      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:etc   ext   同步   表示   .text   技术分享   star   head   记录   

 

MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式:
  • Model(模型)表示应用程序核心(比如数据库记录列表)。
  • View(视图)显示数据(数据库记录)。
  • Controller(控制器)处理输入(写入数据库记录)。

 

:模拟    按Button增加经验,经验改变后,玩家达到当前等级*100经验后升级,升级后给玩家增加当前等级*10的金币,每3级增加钻石10;

 

1、创建一个头像面板模拟MVC模式

技术分享

 

 

2、创建一个SQL数据库保存玩家数据

 

 

技术分享

 

2、给Head添加4个脚本:

技术分享

 

   Model(模型) : PlayerModel脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 值改变的委托
/// </summary>
/// <param name="value"></param>
public delegate void OnValueChangeEventHandle(float value);

public class PlayerModel : MonoBehaviour
{


    private float playerExperience;
    private int playerLevel;
    private int playerCoin;
    private int playerDiamond;
    /// <summary>
    /// 经验改变事件(猫进来了=经验改变了),
    /// 观察这只猫的动物身上的逃跑方法触发(注册了经验改变这个事件的对象身上的方法触发))
    /// UI更新显示的方法触发,数据库更新的方法触发
    /// </summary>
    public event OnValueChangeEventHandle OnExperienceChange;

    /// <summary>
    /// 玩家经验
    /// </summary>
    public float PlayerExperience
    {
        set
        {
            playerExperience = value;
            //经验被更新后(经验被改变),触发事件,调用所有绑定了该事件的方法
            OnExperienceChange(PlayerExperience);
        }
        get
        {
            return playerExperience;
        }
    }

    /// <summary>
    /// 玩家等级
    /// </summary>
    public int PlayerLevel
    {
        set
        {
            playerLevel = value;
            //经验更新后触发经验改变事件
            OnExperienceChange(PlayerExperience);
        }
        get
        {
            return playerLevel;
        }
    }
    /// <summary>
    /// 玩家金币
    /// </summary>
    public int PlayerCoin
    {
        set
        {
            playerCoin = value;
            //经验更新后触发事件
            OnExperienceChange(PlayerExperience);
        }
        get
        {
            return playerCoin;
        }
    }
    /// <summary>
    /// 玩家钻石
    /// </summary>
    public int PlayerDiamond
    {
        set
        {
            playerDiamond = value;
            //触发事件
            OnExperienceChange(PlayerExperience);
        }
        get
        {
            return playerDiamond;
        }
    }
}

 

        View(视图):PlayerView

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerView : MonoBehaviour {

    public Text levelText;
    public Text experienceText;
    public Slider experienceSlider;
    public Text coinText;
    public Text diamondText;
    private PlayerModel model;
    public Button addExpBut;
    private void Awake()
    {
        model = GetComponent<PlayerModel>();
    }
    /// <summary>
    /// 显示数据(注册到事件里,当事件被触发,方法被调用)
    /// </summary>
    /// <param name="ex"></param>
    public void ShowData(float ex)
    {
        levelText.text = model.PlayerLevel.ToString();
        experienceText.text = model.PlayerExperience.ToString() + "/"+(model.PlayerLevel*100).ToString();
        coinText.text = model.PlayerCoin.ToString();
        diamondText.text = model.PlayerDiamond.ToString();
        experienceSlider.value = model.PlayerExperience / (model.PlayerLevel * 100);
    }
}

 

 

 

       Controller(控制器):PlayerController代码:

      

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

    private PlayerModel model;
    private PlayerDB db;
    private PlayerView view;
    private void Awake()
    {
        model = GetComponent<PlayerModel>();
        db = GetComponent<PlayerDB>();
        view = GetComponent<PlayerView>();
    }

    void Start()
    {
        //打开数据库
        db.OpenDB("MVC");
        //UI里显示数据的方法,绑定到经验改变事件
        model.OnExperienceChange += view.ShowData;

        //数据库的数据同步到模型
        db.Synchronization();
     
        //数据库操作的方法,绑定到经验改变事件
        model.OnExperienceChange += db.UpdatePlayerData;

        //增加经验的方法,绑定到鼠标点击事件
        view.addExpBut.onClick.AddListener(OnBtnClick);
    }
    private void OnDestroy()
    {
        db.CloseDB(); //关闭数据库
    }

    void OnBtnClick()
    {
        AddExperience(50); //增加经验
    }
    /// <summary>
    /// 增加经验
    /// </summary>
    /// <param name="exp"></param>
    public void AddExperience(float exp)
    {
        //当前的经验 = 已有经验 + 增加的经验
        float currentExp = model.PlayerExperience + exp;
        while (currentExp>model.PlayerLevel*100)
        {
            //获取剩余经验
            currentExp -= model.PlayerLevel * 100;
            //升级
            model.PlayerLevel++;
            //金币增加
            model.PlayerCoin += model.PlayerLevel * 10;

            //每升3级钻石增加10
            if (model.PlayerLevel%3==0)
            {
                model.PlayerDiamond += 10;
            }      
        }
        //剩余经验赋值到模型
        model.PlayerExperience = currentExp;
    }
}

 

       数据库操作:PlayerDB代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mono.Data.Sqlite;
public class PlayerDB : MonoBehaviour
{
private string dataPath;
private SqliteCommand command;
private SqliteConnection con;
private SqliteDataReader reader;
private PlayerModel playerModel;
private void Awake()
{
playerModel = GetComponent<PlayerModel>();
}
/// <summary>
/// 打开数据库
/// </summary>
/// <param name="DBName"></param>
public void OpenDB(string DBName)
{
//拼凑路径
dataPath = "Data Source==" + Application.streamingAssetsPath +
"/" + DBName + ".sqlite";
con = new SqliteConnection(dataPath);
command = con.CreateCommand();
con.Open();

}

/// <summary>
/// 关闭数据库
/// </summary>
public void CloseDB()
{
reader.Close();
reader = null;
con.Clone();
}

/// <summary>
/// 更新玩家数据库(已经注册到经验改变事件里,当经验发生改变时,会调用这个方法)
/// </summary>
/// <param name="ex"></param>
public void UpdatePlayerData(float ex)
{
string query = "Update PlayerData Set playerLevlel=‘" +
playerModel.PlayerLevel + "‘,playerExperience=‘" +
playerModel.PlayerExperience + "‘,playerCoin=‘" +
playerModel.PlayerCoin + "‘,playerDiamond=‘" + playerModel.PlayerDiamond + "‘";
command.CommandText = query;
command.ExecuteNonQuery();
}

/// <summary>
/// 将数据库数据同步到模型
/// </summary>
public void Synchronization()
{

string query = "Select * From PlayerData";
command.CommandText = query;
reader = command.ExecuteReader();
reader.Read();
//同步到模型
playerModel.PlayerLevel = int.Parse(reader.GetValue(0).ToString());
playerModel.PlayerCoin = System.Convert.ToInt32(reader.GetValue(2).ToString());
playerModel.PlayerDiamond = int.Parse(reader.GetValue(3).ToString());
playerModel.PlayerExperience = float.Parse(reader.GetValue(1).ToString());
reader.Close();
}
}

 技术分享

 

Unity (十二) MVC框架:关于MVC 编程模式的模拟运用

标签:etc   ext   同步   表示   .text   技术分享   star   head   记录   

原文地址:http://www.cnblogs.com/yugejuhao/p/7678022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!