UI交互操作

介绍

UNIHper主张通过脚本接口获取相关组件,尽量避免公开编辑器变量获取变量引用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using UNIHper;
using UNIHper.UI;
using DNHper;

[UIPage(Asset = "NewUI", Order = -1, Type = UIType.Normal)]
public class NewUI : UIBase
{
    // Start is called before the first frame update
    private void Start()
    {
        // 获取按钮点击事件
        this.Get<Button>("btn_startGame")
            .OnClickAsObservable()
            .Subscribe(_ =>
            {
                Debug.LogWarning("btn_startGame clicked");
                Managements.UI.Show<MainUI>();
            });
        // 获取指定路径下的组件,并为其赋值
        this.Get<Image>("userInfo/user_Avatar").sprite = Managements.Resource.Get<Sprite>("userAvatar");
    }

    // Update is called once per frame
    private void Update() { }

    // Called when this ui is loaded
    protected override void OnLoaded() { }

    // Called when this ui is shown
    protected override void OnShown() { }

    // Called when this ui is hidden
    protected override void OnHidden() { }
}

Last updated