zoukankan      html  css  js  c++  java
  • unity3d mvvm c#

    using UnityEngine;
    using System.Collections;
    
    public interface IState {
        void BeforEnter();
        void BeforLeave();
    }
    
    public interface ISceneState: IState {
    
    }
    
    public class GenFSM  {
        IState _state;
        public GenFSM(IState state){
            this._state = state;
            this._state.BeforEnter();
        }
    
        public void ChangeState(IState state){
            this._state.BeforLeave();
            this._state = state;
            this._state.BeforEnter();
        }
    }
    
    namespace Scene {
        public class Login: ISceneState {
            // ui vm
            public void BeforEnter(){
                // uivm.Oncommand( show ui)
            }
    
            public void BeforLeave(){
    
            }
    
            public void OnLogin(){
                //SceneViewModel.changeState(SceneViewModel.Game);
            }
        }
    
        public class Game: ISceneState {
            // package vm
    
            HeroViewModel heroViewModel;
    
            public void BeforEnter(){
                Application.LoadLevel(1);
                this.heroViewModel  = new HeroViewModel();
            }
            
            public void BeforLeave(){
                
            }
        }
    }
    
    public interface HeroCmd {
        void UseSkill();
        void AddBlood();
        void Say();
    }
    
    public class HeroViewModel: HeroCmd {
        public HeroViewModel(){
            var prefab = GameObject.Find ("hero") as GameObject;
            prefab.AddComponent("HeroBehaviour");
            HeroBehaviour inst = prefab.GetComponent<HeroBehaviour>();
            inst.vm = this;
        }
    
        public void OnCommand(string cmd){
            Debug.Log("on command: " +  cmd);
        }
    
        public void UseSkill(){}
        public void AddBlood(){}
        public void Say(){}
    }
    
    
    
    public class HeroBehaviour: MonoBehaviour {
    
        public HeroCmd vm;
    
        void Update(){
            if(Input.anyKeyDown){
                // animation
                vm.UseSkill();
            }
        }
    }
    
    
    public class UILoginViewModel {
        Scene.Login _l;
        public UILoginViewModel(Scene.Login L){
            this._l = L;
            var prefab = GameObject.Find ("login_ui") as GameObject;
            prefab.AddComponent("UIClickBehaviour");
        }
    
        void Login(){
            this._l.OnLogin();
        }
    }
    
    
    public class SceneViewModel {
        private Scene.Login _stateLogin;
        private Scene.Login _stateGame;
    
        GenFSM scenefsm;
    
        public SceneViewModel(){
            this._stateLogin = new Scene.Login();
            this.scenefsm = new GenFSM(this._stateLogin);
        }
    
        //command
        public void ChangeState(ISceneState state){
            scenefsm.ChangeState(state);
        }
    }
  • 相关阅读:
    strcpy实现 处理地址重叠,返回值问题
    lua的table的删除操作
    防御式编程
    软技能:代码之外的生存指南
    adb命令行输出logcat日志
    SyntaxError: Non-ASCII character 'xe5' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
    cocos2dx lua 图片去色shader
    cocos studio
    35 个 Java 代码性能优化总结
    Java必备技能:clone浅克隆与深克隆
  • 原文地址:https://www.cnblogs.com/lightlfyan/p/4207348.html
Copyright © 2011-2022 走看看