zoukankan      html  css  js  c++  java
  • U3D MVC 背包案例

    Model:【模型】

    根据物体的基本属性,在Model中所有物品都有一个共有的属性ID。在Model基类中可添加ID属性

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class BaseModel
    {
        public int ID {
            get;
            set;
        }
    }

    背包中存放物品,每个物品也都是一个模型,物品属性为ID,name【物品名字】,Path【物品对应图片的存放路径】

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GoodModel : BaseModel
    {
        /// <summary>
        /// 物品名称
        /// </summary>
        public string goodName;
        /// <summary>
        /// 物品图片对应图片
        /// </summary>
        public string GoodIconPath;
        public GoodModel ()
        {
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="id">Identifier.</param>
        /// <param name="name">Name.</param>
        /// <param name="path">Path.</param>
        public GoodModel (int id, string name, string path)
        {
            this.ID = id;
            this.goodName = name;
            this.GoodIconPath = path;
        }
    }

    背包的每一个背包栏的模型,属性ID【背包栏ID】,goodID【物品ID】,count【背包栏中的存放的物品数量】

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 对应背包中存放某一类物品的模型
    /// 包含这一类物品对应goodID,个数
    /// </summary>
    public class PackModel : BaseModel
    {
    
        //物品
        public GoodModel goodsModel;
        //物品个数
        public int Count;
        //物品ID
        public int GoodID;
    
        public PackModel ()
        {
    
        }
    
        //背包ID, 物品ID , 物品数量
        public PackModel (int id, int goodID, int count)
        {
            this.ID = id;
            this.GoodID = goodID;
            this.Count = count;
        }
    
        //没有物品时, 背包初始化使用
        public PackModel (int id)
        {
            this.ID = id;
            this.GoodID = -1;
            this.Count = 0;
        }
    }

    ProXy:【代理,用来管理模型集合】

    首先定义继承于BaseModel的泛型类,用来声明模型集合,想模型中添加Model对象,根据ID查找Model模型【代替Model来处理数据】

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    /// <summary>
    /// 用于存放和管理所有模型对应的数据,具体是哪个模型,取决于派生类
    /// 如果是goodProxy:当前游戏中所有可能出现在背包中的物品信息
    /// 如果是PackProxy:表示当前玩家已经获得到的或者买到的物品信息
    /// </summary>
    public class BaseProxy<T> where T:BaseModel
    {
    
        //泛型集合,T必须是继承于BaseModel集合的
        List<T> modelList;
    
        public BaseProxy ()
        {
            modelList new List<T> ();
        }
    
        //添加model对象
        public void AddModel (T model)
        {
            if (model == null) {
                Debug.Log ("model参数不能为空");
            }
            this.modelList.Add (model);
        }
    
        //根据ID获取model对象
        public T GetModelById (int id)
        {
            for (int i = 0; i < this.modelList.Count; i++) {
                if (this.modelList [i].ID == id) {
                    return this.modelList [i];
                }
            }
            return default(T);
        }
    
        //获取模型集合
        public List<T> GetModelList ()
        {
            return this.modelList;
        }
    }

    物品代理GoodsProxy,继承于GoodModel,用于创建模型列表,来告诉其他的类,有几种物品模型

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GoodsProxy : BaseProxy<GoodModel>
    {
        //调用父类无参构造方法
        public GoodsProxy () : base ()
        {
            this.AddModel (new GoodModel (1, "大夏龙雀", "goods1"));
            this.AddModel (new GoodModel (2, "屠龙刀", "goods2"));
            this.AddModel (new GoodModel (3, "割鹿刀", "goods0"));
        }
    //单例类
        private static GoodsProxy instance;
    
        public static GoodsProxy GetInstance ()
        {
            if (instance == null) {
                instance = new GoodsProxy ();
            }
            return instance;
        }
    }

    背包代理PackProxy,用来向集合中添加背包栏,并初始化背包栏中的物品个数及物品属性

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PackProxy : BaseProxy<PackModel>
    {
        public PackProxy () : base ()
        {
            this.AddModel (new PackModel (1, 1, 5));
            this.AddModel (new PackModel (2, 2, 6));
            this.AddModel (new PackModel (3));
            this.AddModel (new PackModel (4));
            this.AddModel (new PackModel (5, 3, 10));
        }
    //单例类
        private static PackProxy instance;
    
        public static PackProxy GetInstance ()
        {
            if (instance == null) {
                instance = new PackProxy ();
            }
            return instance;
        }
    }

    View

        这里的View分为两类,一是所有的背包栏的集体展示,二是每一个背包栏中物品的展示

    1、PackViewScript

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PackViewScript : MonoBehaviour
    {
        //用来展示装备的预制体
        GameObject packItemPrefab;
    
        void Awake ()
        {
            //加载预制体
            packItemPrefab = Resources.Load<GameObject> ("pack");
        }
    
        public void RenderModelToView (List<PackModel> packmodelList)
        {
            for (int i = 0; i < packmodelList.Count; i++) {
                //创建的装备
                GameObject packItem = Instantiate (packItemPrefab);
                packItem.transform.SetParent (transform);
                //将对应的背包栏中物品信息展示早View上
                packItem.GetComponent<PackItemScript> ().ShowGoods (packmodelList [i]);
            }
        }
    }

    2、PackItemScript每一个物品栏中的效果展示

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class PackItemScript : MonoBehaviour
    {
    
        //展示信息的控件
        Image goodImg;
        Text goodsCount;
    
        void Awake ()
        {
            goodImg = transform.Find ("Image").GetComponent<Image> ();
            goodsCount = transform.Find ("Text").GetComponent<Text> ();
        }
    
        //展示物品信息
        public void ShowGoods (PackModel packModel)
        {
            if (packModel.Count == 0) {
                //当前背包栏中没有物品,禁用掉展示物品的图片
                goodImg.gameObject.SetActive (false);
                goodsCount.text = string.Empty;
            } else {
                goodImg.gameObject.SetActive (true);
                goodImg.sprite = Resources.Load <Sprite> (packModel.goodsModel.GoodIconPath);
                goodsCount.text = packModel.Count.ToString ();
            }
        }
    }

    Controller:控制器

      控制器可作为Model和View交互的桥梁,在本案例中主要是与Proxy和View进行交互

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class PackControllerScript : MonoBehaviour
    {
    
        //创建背包栏委托对象
        PackProxy packProxy;
        //物品委托对象
        GoodsProxy goodsProxy;
        //获取要展示的View对象
        public PackViewScript packView;
    
        void Awake ()
        {
            packProxy = PackProxy.GetInstance ();
            goodsProxy = GoodsProxy.GetInstance ();
        }
        //展示背包
        void ShowPack ()
        {
            //获取要展示信息的集合
            List<PackModel> packModelList = packProxy.GetModelList ();
            //将集合中的信息展示出来(遍历)
            for (int i = 0; i < packModelList.Count; i++) {
                PackModel packModel = packModelList [i];
                //根据对应的物品ID去获取对应的物品model
                packModel.goodsModel = goodsProxy.GetModelById (packModel.GoodID);
            }
            //将数据传递给packView
            packView.RenderModelToView (packModelList);
        }
    
        void Update ()
        {
            if (Input.GetMouseButtonDown (0)) {
                ShowPack ();
            }
        }
    
    }
  • 相关阅读:
    Emmet 语法
    GitHub常用命令
    ProgressBar.js – 漂亮的响应式 SVG 进度条
    99个漂亮的注册和登录页设计(附PSD)
    android Acitivity之间的几种传值方式(^_^)
    Android 动态生成 EditTest
    Android 小笔记
    winfrom获取用户控件里的控件对象
    MVC+Easeyui dialog的小问题
    bootStrap
  • 原文地址:https://www.cnblogs.com/zpy1993-09/p/13083685.html
Copyright © 2011-2022 走看看