/
HoverScaler.cs
61 lines (55 loc) · 1.77 KB
/
HoverScaler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 一个高度封装的 Hover 交互效果组件
/// </summary>
public class HoverScaler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
/**
* 是否在 hover 时把控件调整到最后渲染,使它显示在最上层
* 通过附加 Canvas 和 GraphicRaycaster 组件实现,动态调整 Canvas 的 sortingOrder 属性
*/
public bool topWhenHover;
// topWhenHover 为 true 时,使用此值设置 canvas 组件的 sortingOrder 属性
public int sortingOrder = 1;
private Animator animator;
// Start is called before the first frame update
void Start()
{
animator = gameObject.AddComponent<Animator>();
var controller = Resources.Load<RuntimeAnimatorController>("Animations/ScaleWhenHover");
animator.runtimeAnimatorController = Instantiate(controller);
if (topWhenHover)
{
if (GetComponent<Canvas>() == null)
{
gameObject.AddComponent<Canvas>();
}
if (GetComponent<GraphicRaycaster>() == null)
{
gameObject.AddComponent<GraphicRaycaster>();
}
}
}
private void OnHoverChanged(bool hover)
{
if (topWhenHover)
{
var canvas = GetComponent<Canvas>();
canvas.overrideSorting = hover;
canvas.sortingOrder = sortingOrder;
}
animator.SetBool("Hovering", hover);
}
public void OnPointerEnter(PointerEventData eventData)
{
OnHoverChanged(true);
}
public void OnPointerExit(PointerEventData eventData)
{
OnHoverChanged(false);
}
}