Unity开发框架 IFramework

MVVM UI模块

框架地址:https://github.com/OnClick9927/IFramework

在这里插入图片描述
实现的功能 点击按钮加1

1.在 Panel_1 添加 ScriptCreater 代码
在这里插入图片描述
2.分别在 Button 和 Text 上 添加 ScriptMask 代码
在这里插入图片描述
3.将 Button 和 Text 拖到 ScriptCreater 上的 Masks 上
在这里插入图片描述
4.将你要存放代码的文件夹拖到 CreatePath 上
在这里插入图片描述
5.点击 Build
在这里插入图片描述
然后在文件夹下会自动创建代码
在这里插入图片描述
6.参考 Panel01 代码构建自己的代码
在这里插入图片描述
7.打开 IFramework\RootWindow\IFramework.UIModule
在这里插入图片描述
点击CopyUIMapFromSource 创建UI_MVVM 脚本,名字随意。
在这里插入图片描述

在1选择你创建的Panel_1, 2也是一样,如果没有 点击 下 FreshPanel&&ModelTypes 。选择完毕后 点击 Gen 按钮创建代码
在这里插入图片描述
8.保存预制体并添加Panel_1代码,点击Bind会自动添加代码
在这里插入图片描述
9.参考Panel01系列的代码设置自己的代码

using IFramework;
using UnityEngine.UI;
using UnityEngine;
using IFramework.Modules.MVVM;
using IFramework.UI;

namespace IFramework_Demo
{
    
    
    public class Panel_1Model:IModel
    {
    
    
        public int count=100;
    }

    public class Panel_1 : UIPanel
	{
    
    
		public Text Text;
		public Button BTn_ADD;

	}
}
namespace IFramework_Demo
{
    
    

    public struct addOne : IUIEvent<int, addOne>//设置传入的消息结构
    {
    
    

		private int _type;
        public int type =>_type;

        public addOne SetType(int type)
        {
    
    
            _type=type;
			return this;
        }
    }
    public class Panel_1View : UIView<Panel_1ViewModel, Panel_1>
	{
    
    
		protected override void BindProperty()
		{
    
    
			base.BindProperty();
			handler.BindProperty(()=>{
    
    
				Tpanel.Text.text = Tcontext.count.ToString();//绑定数据
			});
			//ToDo
		}

		protected override void OnClear()
		{
    
    
		}

		protected override void OnLoad()
		{
    
    
			this.Tpanel.BTn_ADD.onClick.AddListener(()=>{
    
    
				this.message.Publish(this,new addOne().SetType(10));//设置传入的参数
			});
		}

		protected override void OnPop(UIEventArgs arg)
		{
    
    
		    Hide();
        }

		protected override void OnPress(UIEventArgs arg)
		{
    
    
            Hide();
		}

		protected override void OnTop(UIEventArgs arg)
		{
    
    
            Show();
		}

	}
}

namespace IFramework_Demo
{
    
    
	public class Panel_1ViewModel : UIViewModel<Panel_1Model>
	{
    
    
 		private Int32 _count;
		public Int32 count
		{
    
    
			get {
    
     return GetProperty(ref _count); }
			private set			{
    
    
				Tmodel.count = value;
				SetProperty(ref _count, value);
			}
		}


        protected override void SubscribeMessage()
        {
    
    
            base.SubscribeMessage();
			this.message.Subscribe<Panel_1View>(Listen);
        }

        private void Listen(IMessage message)
        {
    
    
			if (message.args.Is<addOne>())
			{
    
    
				count+= message. args.As<addOne>().type;
			}
           
        }

        protected override void SyncModelValue()
		{
    
    
 			this.count = Tmodel.count;

		}

        protected override void UnSubscribeMessage()
        {
    
    
            base.UnSubscribeMessage();
			this.message.UnSubscribe<Panel_1View>(Listen);
        }
    }
}

10.参考UIExample写加载代码

namespace IFramework_Demo
{
    
    
    public class UILoad : Game, IPanelLoader
    {
    
    
        IUIModule module;

        public UIPanel Load(Type type, string name)
        {
    
    
            GameObject go = Resources.Load<GameObject>("UI/Panel/"+name);
            return go.GetComponent<UIPanel>();
        }

        public override void Init()
        {
    
    
            module = modules.CreateModule<UIModule>();
        }

        public override void Startup()
        {
    
    
            module.AddLoader(this);
            module.SetGroups(new MvvmGroups(UI_MVVM.map));
            module.CreateCanvas();
			module.Get<Panel_1>("Panel_1");
        }
    }
}

将UILoad挂载在场景中,即可运行
在这里插入图片描述
Over。

猜你喜欢

转载自blog.csdn.net/LightHuiHui/article/details/115122492