一般在游戏中点击了某个道具,会在其周围弹出显示介绍框之类。
导出资源
代码实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
/*
* Author:W
* 1.弹出菜单 popupMenu.Show
* 2.弹出面板 GRoot.inst.ShowPopup
*/
public class GPopupTest : MonoBehaviour {
private GComponent root;
/// <summary>
/// 弹出菜单栏触发按钮
/// </summary>
private GButton popMenuBtn;
/// <summary>
/// 弹出面板触发按钮
/// </summary>
private GButton popPanelBtn;
/// <summary>
/// 弹出菜单
/// </summary>
private PopupMenu popupMenu;
/// <summary>
/// 弹出面板组件
/// </summary>
private GComponent popupPanel;
void Awake()
{
UIPackage.AddPackage("UI/Basics");
}
// Use this for initialization
void Start () {
root = this.GetComponent<UIPanel>().ui;
//菜单栏弹出框初始化
if (popupMenu == null)
{
popupMenu = new PopupMenu("ui://9leh0eyfm8lv8c");
for (int i = 1; i < 5; i++)
{
GButton btn1 = popupMenu.AddItem("选项"+i, OnMenuItemClick);
GTextField btnName = btn1.GetChild("n3").asTextField;
btnName.text = "选项" + i;
}
}
//弹出面板初始化
if (popupPanel == null)
{
popupPanel = UIPackage.CreateObject("Basics", "Component12").asCom;
popupPanel.Center();
}
popMenuBtn = root.GetChild("n0").asButton;
popMenuBtn.onClick.Add((EventContext context) => {
if (popupMenu != null)
popupMenu.Show((GObject)context.sender, PopupDirection.Down);
});
popPanelBtn = root.GetChild("n1").asButton;
popPanelBtn.onClick.Add(()=> {
GRoot.inst.ShowPopup(popupPanel);
});
}
private void OnMenuItemClick(EventContext context)
{
GObject itemObject = (GObject)context.data;
Debug.Log("菜单项 =" + itemObject.text+" 被选中");
}
// Update is called once per frame
void Update () {
}
}
运行结果如下