实现右键菜单栏 unity protips插件

1.鼠标右键【triggerevent】设置

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class Test : MonoBehaviour, IPointerClickHandler
{
    public UnityEvent leftClick;
    public UnityEvent middleClick;
    public UnityEvent rightClick;
    private void Start()
    {
        leftClick.AddListener(new UnityAction(ButtonLeftClick));
        rightClick.AddListener(new UnityAction(ButtonRightClick));
        middleClick.AddListener(new UnityAction(ButtonMiddleClick));
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
            leftClick.Invoke();
        else if (eventData.button == PointerEventData.InputButton.Right)
            rightClick.Invoke();
        else if (eventData.button == PointerEventData.InputButton.Middle)
            middleClick.Invoke();
    }

    private void ButtonLeftClick()
    {

    }
    private void ButtonRightClick()
    {

    }
    private void ButtonMiddleClick()
    {

    }
}

2. 挂在button按钮下面,设置好事件

3. 事件,用protips 插件,制作对应预制体,使用远程创建物体功能

 

using System;
using UnityEngine;

namespace ModelShark
{
    /// <summary>
    /// This is an example of how to pop up a tooltip on any object from an event. This tooltip will stay open and block all other tooltips
    /// until the use clicks the Close button. It illustrates how you could make a system of tutorial tooltips that teach a player how to
    /// play your game, and also shows how you can have interactable tooltips.
    /// </summary>
    public class PopupTooltipRemotely : MonoBehaviour
    {
        /// <summary>Triggers a tooltip immediately on the game object specified.</summary>
        /// <param name="onObject">The game object to pop a tooltip over.</param>
        public void PopupTooltip(GameObject onObject, string bodyText, string buttonText)
        {
            // Add the TooltipTrigger component to the object we want to pop a tooltip up for.
            TooltipTrigger tooltipTrigger = onObject.GetComponent<TooltipTrigger>();

            if (tooltipTrigger == null)
                tooltipTrigger = onObject.AddComponent<TooltipTrigger>();

            TooltipStyle tooltipStyle = Resources.Load<TooltipStyle>("CleanSimpleCloseButtonMy");
            tooltipTrigger.tooltipStyle = tooltipStyle;
            tooltipStyle.gameObject.GetComponent<ItemTest>().tooltiptrigger = onObject;
            // Set the tooltip text and properties.
            //tooltipTrigger.SetText("BodyText", bodyText);
            tooltipTrigger.SetText("ButtonText", String.IsNullOrEmpty(buttonText) ? "Continue" : buttonText);
            tooltipTrigger.tipPosition = TipPosition.MouseRightMiddle;
            tooltipTrigger.maxTextWidth = 300;
            tooltipTrigger.staysOpen = false; // make this a tooltip that stays open...
            //tooltipTrigger.isBlocking = false; // ...and is blocking (no other tooltips allowed while this one is active).
            tooltipTrigger.isRemotelyActivated = true;
            // Popup the tooltip and give it the object that triggered it (the Canvas in this case).
            tooltipTrigger.Popup(8f, gameObject);
        }

        public void PopupTooltip(GameObject onObject)
        {
            PopupTooltip(onObject, "This tooltip was triggered from an event. A tooltip like this is useful for in-game tutorials and NPC \"barks.\"", "Continue");
        }
    }
}

4.为了实现鼠标移出界面后,自动关闭界面的功能,在tooltips预制体上挂载脚本,并判断是否移出,移出后让其关闭:

 

using ModelShark;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ItemTest :   MonoBehaviour,IPointerExitHandler
{
    public GameObject tooltiptrigger;
    //当鼠标进入UI
    public void OnPointerEnter(PointerEventData eventData)
    {
        print("进入UI");
    }

    //当鼠标离开UI
    public void OnPointerExit(PointerEventData eventData)
    {
        print("离开UI");
        tooltiptrigger.GetComponent<TooltipTrigger>().ForceHideTooltip();


    }


}

5.完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值