zoukankan      html  css  js  c++  java
  • unity 使用MVC模式

    这两天看了下老大的项目,他基本都是用MVC模式,写的很好,在此把我理解的记录下来

    Model:实体对象(对应数据库记录的类)

    View:视图

    presenter(controller):业务处理

    view中有present对象,present中有model和view对象

    view中UI的交互会调用present相应处理的方法(该方法处理完后会调用view显示处理后的视图(将改变后的model对象传递过去),view显示就会改变显示的内容)

     结构是下面这样子的:

    IPresenter:

    namespace NginAR
    {
        public interface IPresenter<T> where T : class
        {
            void Init();
        }
    }

    IView:

    namespace NginAR
    {
        public interface IView<T> where T : class
        {
            /**
     * 设置Presenter
     * @param presenter
     */
            void setPresenter(T presenter);
        }
    }

    IGlassView:

    namespace NginAR
    {
        public interface IGlassView : IView<IGlassPresenter>
        {
            // 设置当前步骤内容,用于显示
            void setStepContent(string content);
            // 设置当前步骤
            void setCurrentStep(int currentStep); 
    }
        public interface IGlassPresenter : IPresenter<IGlassView>
        {
            // 当前步
            bool StepCurrent();
            // 下一步
            bool StepNext();
            // 上一步
            bool StepLast();
            void onStepChange(int currentTaskId, int currentStepId);
        }
    }

    Step:

    namespace NginAR
    {
        public class Step
        {
            public int id { get; set; }
            public string content { get; set; }
            public string type { get; set; }
        }
    }

    FileUtil:

    namespace NginAR
    {
        // 读取文件工具
        public class FileUtil
        {   
            // 加载脚本文件,用于保存步骤内容
            public static void LoadModel(TextAsset asset,List<Step> steps) {
                string[] lines = asset.text.Split("
    "[0]);
                for (int i = 0; i < lines.Length; i++)
                {
                    string[] cols = lines[i].Split(" "[0]);
                    Step step = new Step();
                    step.id = int.Parse(cols[0]); 
                    step.content = cols[1];
                    step.type = cols[2];
              
                    steps.Add(step);
                }
                LogUtil.i("大小"+ steps.Count);
            }
        }
    
    
    }

    LogUtil:

    public class LogUtil
    { 
        private static bool LOGI = true;
        private static bool LOGW = true;
        private static bool LOGE = true;
    
        public static void i(string mess)
        {
            if (LOGI)
            {
                Debug.Log(mess);
            }
        }
    
        public static void w(string mess)
        {
            if (LOGW)
            {
                Debug.LogWarning(mess);
            }
        }
    
        public static void e(string mess)
        {
            if (LOGE)
            {
                Debug.LogError(mess);
            }
        }
    }

    MainView(继承IGlassView):

    UI事件调用present中对应的处理方法:

       // 新建逻辑管理对象 mvp-p
                prenseter = new MainPresenter(asset, this);
                // 为刷新当前步骤按钮设置监听回调
                btn_text.onClick.AddListener(delegate () {
                    prenseter.StepCurrent();
                });
                // 为上一步按钮设置监听回调
                btn_last.onClick.AddListener(delegate () {
                    prenseter.StepLast();
                });
                // 为下一步按钮设置监听回调
                btn_next.onClick.AddListener(delegate () {
                    prenseter.StepNext();
                });

    IGlassPresenter(继承IGlassPresenter):

    present中UI对应处理方法改变model,处理完调用view中方法显示处理后的视图展示:

     public bool StepLast()
            {
                currentStep--;
                if (currentStep < 0)
                {
                    currentStep = 0;
                    return false;
                }
                onStepChange(0, currentStep);
                return true;
            }
            // 下一步逻辑
            public bool StepNext()
            {
               
                if (steps.Count <= 0)
                {
                    return false;
                }
                currentStep++;
                if (currentStep >= steps.Count)
                {
                    currentStep = steps.Count-1;
                    return false;
    
                }
                onStepChange(0,currentStep);
                return true;
            }
            // 步骤改变调用
            public void onStepChange(int currentTaskId, int currentStepId)
            {
                this.currentStep = currentStepId;
                LogUtil.i("currentStepId"+currentStepId);
                currentStepContent =steps[currentStep].content;
                view.setStepContent(currentStepContent);
                view.setCurrentStep(currentStep);
            }

    同时present中还有加载文件的方法:

    文件格式是这样子的:1 1.风险可能导致的后果:倒塌、高处坠落、公路中断运行、跨越架封网脱落等 image1

    上面的FileUtil能解析出来并赋值给step(model)对象

    view中显示方法(参数为model中的信息):

    除了显示model信息之外还会加载其他的UI或者模型

     public void setStepContent(string content)
            {
                Debug.Log("content:"+content);
                // 按钮文本设置为步骤内容
                text.text = content;
            }
      public void setCurrentStep(int currentStep)
            {
                currentStep = currentStep + 1;
                if (Application.loadedLevelName.Equals("KYJ"))
                {
                    if (currentModel != null)
                        Destroy(currentModel);
                    Play("kyj_"+ currentStep);
                    LoadPrefab("Prefabs/Kyj_Step/kyj_" + currentStep, objectTarget.transform);
                   if (currentStep == 11)
                        endStep.SetActive(true);
                    else
                        endStep.SetActive(false);
                }
    }
  • 相关阅读:
    WRF WPS预处理
    CVS安装
    Linux窗口最小化消失,任务栏上无法找到的解决方法
    NCARG安装配置出现error while loading shared libraries: libg2c.so.0问题额解决办法
    Netcdf安装
    Embedding R-generated Interactive HTML pages in MS PowerPoint(转)
    The leaflet package for online mapping in R(转)
    Some 3D Graphics (rgl) for Classification with Splines and Logistic Regression (from The Elements of Statistical Learning)(转)
    What does a Bayes factor feel like?(转)
    Weka算法介绍
  • 原文地址:https://www.cnblogs.com/dsh20134584/p/7807916.html
Copyright © 2011-2022 走看看