=========================================================================>ViewBase
using UnityEngine;
using System.Collections;public class MyViewPresenter : MonoBehaviour {
protected virtual UIRect viewRoot{ get; set;}
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{
get{ return TextLabel.text;}
set{ TextLabel.text = value;}
}
public UnityEngine.Color Color{
get{ return TextLabel.color;}
set{ TextLabel.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{
get{ return slider.value;}
set{
slider.value = value;
}
}
public UnityEngine.Color SliderColor{
get{return GetChildRecursive (this.transform, "FG").GetComponent<UISprite> ().color; }
set{ GetChildRecursive (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{
get{ return level * 150;}
}
public int MAXHP{
get{ return level * 150;}
}
public int Level {
get{ return level;}
private set{
level = value;
}
}
public int XP{
get{ return xp;}
private set{ xp = value;}
}
public int HP{
get{ return 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 (this, EventArgs.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 (this, EventArgs.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 player{ get; set;}
MyMainGUIViewPresenter MainGUI{ get; set;}
public MyGUICtr(){
player = new MyPlayerModel ();
MainGUI = GameObject.Find ("MainUI").GetComponent<MyMainGUIViewPresenter> ();
MainGUI.AddXpCtrBtn.Clicked += (s,e)=>player.AddXp (25);
MainGUI.MinusHpCtrBtn.Clicked += (s, e) => 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 () {
}
}