1. 程式人生 > >Unity中MVC的簡單應用

Unity中MVC的簡單應用

model

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

/// <summary>
/// 模型委託(當用戶資訊發生變化時執行)
/// </summary>
public delegate void OnValueChange(int val);

public class PlayerMsgModel
{
    //玩家等級
    private int playerLevel;
    //玩家經驗
    private int playerExperience;
    //玩家升級經驗
    private int playerFullExperience;
    //金幣數量
    private int goldNum;
    //宣告委託物件,接收當等級發生變化時,觸發的事件
    public OnValueChange OnLevelChange;
    //宣告委託物件,接收當經驗發生變化時,觸發的事件
    public OnValueChange OnExperienceChange;
    //宣告委託物件,接收當升級經驗發生變化時,觸發的事件
    public OnValueChange OnFullExperienceChange;
    //宣告委託物件,接收當金幣數量發生變化時,觸發的事件
    public OnValueChange OnGoldNumChange;

    //單例
    private static PlayerMsgModel _instance;

    public static PlayerMsgModel Instance()
    {
        if (_instance == null)
        {
            _instance = new PlayerMsgModel();
        }
        return _instance;
    }

    private PlayerMsgModel()
    {
    }

    /// <summary>
    /// 玩家等級屬性
    /// </summary>
    /// <value>The player level.</value>
    public int PlayerLevel
    {
        get
        {
            return playerLevel;
        }
        set
        {
            playerLevel = value;
            //如果委託物件不為空
            if (OnLevelChange != null)
            {
                //執行委託
                OnLevelChange(playerLevel);
            }
        }
    }

    /// <summary>
    /// 玩家經驗屬性
    /// </summary>
    /// <value>The player experience.</value>
    public int PlayerExperience
    {
        get
        {
            return playerExperience;
        }
        set
        {
            playerExperience = value;
            if (OnExperienceChange != null)
            {
                OnExperienceChange(playerExperience);
            }
        }
    }

    /// <summary>
    /// 玩家升級經驗屬性
    /// </summary>
    /// <value>The player full experience.</value>
    public int PlayerFullExperience
    {
        get
        {
            return playerFullExperience;
        }
        set
        {
            playerFullExperience = value;
            if (OnFullExperienceChange != null)
            {
                OnFullExperienceChange(playerFullExperience);
            }
        }
    }

    /// <summary>
    /// 金幣數量屬性
    /// </summary>
    /// <value>The gold number.</value>
    public int GoldNum
    {
        get
        {
            return goldNum;
        }
        set
        {
            goldNum = value;
            if (OnGoldNumChange != null)
            {
                OnGoldNumChange(goldNum);
            }
        }
    }
}

view

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

public class PlayerMsgView : MonoBehaviour {

    //UI
    public Text playerLevel;
    public Text playerExperience;
    public Text goldNum;
    public Button experienceUpButton;

    void Start()
    {
        //委託事件繫結
        PlayerMsgModel.Instance().OnLevelChange += SetLevel;
        //委託事件繫結
        PlayerMsgModel.Instance().OnExperienceChange += SetExperience;

        PlayerMsgModel.Instance().OnFullExperienceChange += SetFullExperience;
        PlayerMsgModel.Instance().OnGoldNumChange += SetGoldNum;
        //View繫結按鈕控制功能
        experienceUpButton.onClick.AddListener(
            PlayerMsgController._instance .OnExperienceUpButtonClick);
    }

    //修改UILevel值
    public void SetLevel(int level)
    {
        playerLevel.text = level.ToString();
    }

    //修改UI經驗值
    public void SetExperience(int experience)
    {
        //將字串以“/”拆開
        string[] str = playerExperience.text.Split(new char[] { '/' });
        //用新的經驗值重組
        playerExperience.text = experience + "/" + str[1];
    }

    public void SetFullExperience(int fullExiperience)
    {
        string[] str = playerExperience.text.Split(new char[] { '/' });

        playerExperience.text = str[0] + "/" + fullExiperience;
    }

    public void SetGoldNum(int goldn)
    {
        goldNum.text = goldn.ToString();
    }
}

 control

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

public class PlayerMsgController : MonoBehaviour {

    public static PlayerMsgController _instance;

    private int levelUpValue = 20;

    void Awake()
    {
        _instance  = this;
    }

    void Start()
    {
        PlayerMsgModel.Instance ().PlayerLevel = 1;
        PlayerMsgModel.Instance().PlayerExperience = 0;
        PlayerMsgModel.Instance().PlayerFullExperience = 100;
        PlayerMsgModel.Instance().GoldNum = 0;
    }

    /// <summary>
    /// 提升經驗按鈕點選事件
    /// </summary>
    public void OnExperienceUpButtonClick()
    {
            PlayerMsgModel.Instance().PlayerExperience += levelUpValue;
        if (PlayerMsgModel.Instance().PlayerExperience >= PlayerMsgModel.Instance().PlayerFullExperience)
        {
            PlayerMsgModel.Instance().PlayerLevel += 1;
            PlayerMsgModel.Instance().PlayerFullExperience += 200 * PlayerMsgModel.Instance().PlayerLevel;
            levelUpValue += 20;
            if (PlayerMsgModel.Instance().PlayerLevel % 3 == 0)
            {
                PlayerMsgModel.Instance().GoldNum += 100 * PlayerMsgModel.Instance().PlayerLevel;
            }
        }
    }
}