UNITY之MVC框架基础

=========================================================================>ViewBase

using UnityEngine;

using System.Collections;
public class MyViewPresenter : MonoBehaviour {
    protected virtual UIRect viewRootgetset;}
    void Awake(){
    
        viewRoot = viewRoot ?? GetComponent<UIRect> ();
    
        #if UNITY_EDITOR
        if(UnityEditor.EditorApplication.isPlaying)UpdateAlpha(0);
        #else
        viewRoot.alpha = 0f;
        #endif
    }

    // Use this for initialization
    void Start () {
    
    }
    // Update is called once per frame
    void Update () {
    
    }
    public virtual void Enable(){
    
        viewRoot.enabled = true;
    }
    public virtual void Disable(){
    
        viewRoot.enabled = false;
    }
    public virtual void Show(){
    
        UpdateAlpha (1f);
    }
    public virtual void Hide(){
    
        UpdateAlpha (0f);
    }
    public void UpdateAlpha(float v){
        viewRoot.alpha = v;
    }
    public static Transform GetChildRecursive(Transform trans,string name){
    
        Component[] transforms = trans.GetComponentsInChildren (typeof(Transform),true);
        foreach(Transform t in transforms){
            if(t.name==name){
                return t;
            }
        }
        return null;
    }

}

=====================================================================>LabelView

using UnityEngine;
using System.Collections;
public class MyLabelViewPresenter : MyViewPresenter {
    public UILabel TextLabel;
    public string Text{
        getreturn TextLabel.text;}
        setTextLabel.text = value;}
    }
    public UnityEngine.Color Color{
        getreturn TextLabel.color;}
        setTextLabel.color = value;}
    }
}

============================================================================>ButtonView

using UnityEngine;
using System.Collections;
using System;
public class MyButtonViewPresenter : MyViewPresenter {
    public event EventHandler Clicked;
    public UIButton Button;
    public UILabel ButtonLabel;
    // Use this for initialization
    void Start () {
        IsEnable = Button.isEnabled;
    }
    // Update is called once per frame
    void Update () {
    
    }
    public string Text{
        get{
            return ButtonLabel != null ? ButtonLabel.text : string.Empty;
        }
        set{
            if (ButtonLabel == null)
                return;
            ButtonLabel.text = value;
        }
    }
    public bool IsEnable {
        get;
        private set;
    }
    public override void Enable(){
        IsEnable = true;
    }
    public override void Disable(){
        IsEnable = false;
    }

    public virtual void OnButtonClicked(){
        if (IsEnable == false)
            return;
        if (Clicked != null) {
            Clicked (this,EventArgs.Empty);
        }
    }
}
=============================================================================>SliderView

using UnityEngine;
using System.Collections;
public class MySliderViewPresenter : MyViewPresenter {
    public UISlider slider;
    public UILabel TextLabel;
    public string Text{
        get{return TextLabel.text;}
        set{TextLabel.text = value; }
    }
    public float SliderValue{
        getreturn slider.value;}
        set{
            slider.value = value;
        }
    }
    public UnityEngine.Color SliderColor{
        get{return GetChildRecursive (this.transform"FG").GetComponent<UISprite> ().color; }
        setGetChildRecursive (this.transform"FG").GetComponent<UISprite> ().color = value;}
    }
}
==================================================================>MainViewManage

using UnityEngine;
using System.Collections;
public class MyMainGUIViewPresenter : MyViewPresenter {
    public MyLabelViewPresenter LevelLabel;
    public MyLabelViewPresenter HPLabel;
    public MyLabelViewPresenter XPLabel;
    public MySliderViewPresenter HPSlider;
    public MySliderViewPresenter XPSlider;
    public MyButtonViewPresenter AddXpCtrBtn;
    public MyButtonViewPresenter MinusHpCtrBtn;
    public override void Show(){
        LevelLabel.Show ();
        HPLabel.Show ();
        XPLabel.Show ();
        HPSlider.Show ();
        XPSlider.Show ();
        AddXpCtrBtn.Show ();
        MinusHpCtrBtn.Show ();
        base.Show ();
    }
    public override void Hide(){
        LevelLabel.Hide ();
        HPLabel.Hide();
        XPLabel.Hide ();
        HPSlider.Hide ();
        XPSlider.Hide ();
        AddXpCtrBtn.Hide ();
        MinusHpCtrBtn.Hide ();
        base.Hide ();
    }
}
==========================================================================>Model

using UnityEngine;
using System.Collections;
using System;
public class MyPlayerModel {
    public event EventHandler XPGained;
    public event EventHandler LevelUp;
    public event EventHandler DemageTaken;
    public event EventHandler Died;

    private int level;
    private int xp;
    private int hp;
    public int MaxXP{
        getreturn level * 150;}
    }
    public int MAXHP{
        getreturn level * 150;}
    }
    public int Level {
    
        getreturn level;}
        private set{
            level = value;
        }
    }
    public int XP{
        getreturn xp;}
        private setxp = value;}
    }
    public int HP{
        getreturn hp;}
        set{
            var oldValue = HP;
            hp = value;
            if(hp<0){
                hp = 0;
            }

            if(oldValue>xp){
                if(DemageTaken!=null){
                    DemageTaken (this,EventArgs.Empty);
                }
            }
            if(IsDead){
                if (Died != null)
                    Died (thisEventArgs.Empty);
            }
        }
    }
    public MyPlayerModel(){
        level = 1;
        xp = 0;
        hp = MAXHP;
    }


    public bool IsDead{
        get{
            return HP <= 0;
        }
    }

    public void TakeDemage(int hpDemage){
        if (IsDead)
            return;
        HP -= hpDemage;
    }

    public void AddXp(int amount){
        if (IsDead)
            return;
        XP += amount;
        if(XP>=MaxXP){
            XP = 0;
            OnPlayerLevelUp ();
        }
        OnPlayerXPGained ();
    }
    public virtual void OnPlayerXPGained(){
    
        if (XPGained != null)
            XPGained (this,EventArgs.Empty);
    }
    public virtual void OnPlayerLevelUp(){
        level++;
        HP = MAXHP;
        if (LevelUp != null)
            LevelUp (thisEventArgs.Empty);
    }
    public bool HasLowHP{
        get{
            if(HP*1.0<MAXHP*0.25){
                return true;
            }else{
                return false;
            }
        }
    }
}
=======================================================================>MyController

using UnityEngine;
using System.Collections;
using System;
public class MyGUICtr {

    MyPlayerModel playergetset;}
    MyMainGUIViewPresenter MainGUIgetset;}
    public MyGUICtr(){
        player = new MyPlayerModel ();
        MainGUI = GameObject.Find ("MainUI").GetComponent<MyMainGUIViewPresenter> ();

        MainGUI.AddXpCtrBtn.Clicked += (s,e)=>player.AddXp (25);
        MainGUI.MinusHpCtrBtn.Clicked += (se) => player.TakeDemage (75);

        player.XPGained += OnPlayerGainedXP;
        player.DemageTaken += OnDemageTaken;
        player.Died +=(s,e)=> UpdateNavDeadShow();
        player.LevelUp += OnPlayerLevelUp;

        UpdateNavShow ();

    }
    void UpdateHPUI(){
        MainGUI.HPSlider.SliderValue = (float)player.HP / (float)player.MAXHP;
        MainGUI.HPSlider.Text="HP:" + player.HP + "/" + player.MAXHP;
        if (player.HasLowHP) {
            MainGUI.HPSlider.SliderColor = UnityEngine.Color.yellow;
        } else {
            MainGUI.HPSlider.SliderColor = UnityEngine.Color.green;
        }
    }
    void UpdateNavShow(){
        MainGUI.HPSlider.SliderValue = player.HP / (float)player.MAXHP;
        Debug.Log (player.HP);
        Debug.Log (player.MAXHP);
        MainGUI.XPSlider.SliderValue = (float)player.XP / (float)player.MaxXP;
        MainGUI.HPLabel.Text = "HP:" + player.HP + "/" + player.MAXHP;
        MainGUI.XPSlider.Text = "XP:" + player.XP + "/" + player.MaxXP;
        MainGUI.LevelLabel.Text = "LV:" + player.Level;
    }
    void UpdateXPUI(){
        MainGUI.XPSlider.SliderValue = (float)player.XP / (float)player.MaxXP;
        MainGUI.XPLabel.Text = "XP:" + player.XP + "/" + player.MaxXP;
    }
    void UpdateNavDeadShow(){
        //MainGUI.HPLabel.Text = "Dead";
        MainGUI.HPLabel.Color = UnityEngine.Color.red;
    }
    public void ShowView(){
        MainGUI.Show ();
    }
    public void HideView(){
        MainGUI.Hide ();
    }

    public void OnPlayerGainedXP(object sender,EventArgs e){
        UpdateXPUI ();
    }
    public void OnDemageTaken(object sender,EventArgs e){
    
        UpdateHPUI ();
    }

    public void OnPlayerLevelUp(object sender,EventArgs e){
        UpdateNavShow ();
    }
}
===========================================================================>InitController

using UnityEngine;
using System.Collections;

public class MyInitCtr : MonoBehaviour {

    MyGUICtr myGuiCtr;

    // Use this for initialization
    void Start () {
    
        myGuiCtr = new MyGUICtr ();
        myGuiCtr.ShowView ();
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值