MonoBehaviour的UI框架

相关的知识请点击:

http://www.blogjava.net/Jack2007/archive/2008/05/05/198566.html


原理什么的就不一一详谈了,直接上代码了:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public abstract class BaseView
{
    public GameObject myGameObject;
    public void Setup(Transform root)
    {
        myGameObject = root.gameObject;
        Init();
    }

    protected virtual void Init()
    { 
    }
}

public class ViewTest : BaseView
{
    public Text txt;
    public Button btn;
    public Text btnText;

    protected override void Init()
    {
        base.Init();
        Transform root = this.myGameObject.transform;
        txt = root.Find("Text").GetComponent();
        btn = root.Find("Button").GetComponent

using UnityEngine;
using System.Collections;

public class MonoViewController : MonoBehaviour where T : BaseView, new()
{
    private T _view;

    protected T View
    {
        get
        {
            if (_view == null)
            {
                _view = new T();
                _view.Setup(gameObject.transform);
            }
            return _view;
        }
    }
}


using UnityEngine;
using System.Collections;

public class TestController : MonoViewController {

    void Start()
    {

        View.btn.onClick.AddListener(OnClick);
    }

    public void OnClick()
    {
        View.txt.text = "欢迎关注本人博客";
        View.btnText.text = "关注吧!!!";
    }
}


效果图:

MonoBehaviour的UI框架_第1张图片                       MonoBehaviour的UI框架_第2张图片



你可能感兴趣的:(Unity,Unity架构,UGUI)