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.完成。