weixudong
2020-11-14 868c163b39c3487b5fa6f69eb8ca3789ac65cb8e
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class ClickEffect : MonoBehaviour
{
    /// <summary>
    /// 屏幕特效原始资源
    /// </summary>
    public GameObject fxSample;
 
    /// <summary>
    /// 屏幕特效的生命时长,超过后会进行缓存
    /// </summary>
    public float fxLifeTime = 1.0f;
 
    /// <summary>
    /// 屏幕特效的容器(父对象)
    /// </summary>
    public RectTransform fxContainer;
 
    /// <summary>
    /// 屏幕特效渲染使用的相机
    /// </summary>
    public Camera fxRenderCamera;
 
    private Queue<GameObject> pool = new Queue<GameObject>(20);
    private List<GameObject> activatedFXList = new List<GameObject>();
 
    private void Awake()
    {
        if (fxSample == null)
        {
            Debug.LogErrorFormat("没有找到屏幕特效");
            this.enabled = false;
        }
        else
        {
            fxSample.SetActive(false);
        }
    }
 
    private void Update()
    {
        for (int i = activatedFXList.Count - 1; i >= 0; --i)
        {
            GameObject fx = activatedFXList[i];
            float fxTime = float.Parse(fx.name);
            if (Time.time - fxTime > fxLifeTime)
            {
                RecycleFX(fx);
                activatedFXList.RemoveAt(i);
            }
        }
 
        if (Application.isMobilePlatform)
        {
            for (int i = 0; i < Input.touchCount; ++i)
            {
                Touch touch = Input.GetTouch(i);
                if (touch.phase == TouchPhase.Began)
                {
                    PlayFX(touch.position);
                }
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(0))
            {
                PlayFX(Input.mousePosition);
            }
        }
    }
 
 
    private void PlayFX(Vector2 tapPos)
    {
        GameObject fx = CreateFX();
        fx.name = Time.time.ToString();
        activatedFXList.Add(fx);
 
        RectTransform fxRectTrans = fx.GetComponent<RectTransform>();
        Vector2 fxLocalPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(fxContainer, tapPos, fxRenderCamera, out fxLocalPos);
        fxRectTrans.SetParent(fxContainer);
        fxRectTrans.anchoredPosition3D = fxLocalPos;
        fx.SetActive(true);
    }
 
 
    private GameObject CreateFX()
    {
        GameObject newFX = null;
        if (pool.Count > 0)
        {
            newFX = pool.Dequeue();
        }
        else
        {
            newFX = Instantiate(fxSample);
        }
        return newFX;
    }
 
    private void RecycleFX(GameObject fx)
    {
        fx.SetActive(false);
        pool.Enqueue(fx);
    }
 
    //简单,无法显示在最上层
    // Vector3 point;
    // GameObject effectGo;
 
    // void Start()
    // {
    //     effectGo = Resources.Load<GameObject>("Prefabs/EffectClick");
    // }
 
    // void Update()
    // {
    //     if (Input.GetMouseButtonDown(0))
    //     {
    //         point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 4f);//获得鼠标点击点
    //         point = Camera.main.ScreenToWorldPoint(point);//从屏幕空间转换到世界空间
    //         GameObject go = Instantiate(effectGo);//生成特效
    //         go.transform.position = point;
    //         Destroy(go, 0.5f);
    //     }
    // }
}