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>
|
private Camera fxRenderCamera;
|
|
private Queue<RecycleEffectc> pool = new Queue<RecycleEffectc>(20);
|
|
private void Awake()
|
{
|
if (fxSample == null)
|
{
|
Debug.LogErrorFormat("没有找到屏幕特效");
|
this.enabled = false;
|
}
|
else
|
{
|
fxSample.SetActive(false);
|
}
|
|
fxRenderCamera = GameObject.Find("UICamera").GetComponent<Camera>();
|
}
|
|
private void Update()
|
{
|
if (Application.isMobilePlatform)
|
{
|
Touch touch = Input.GetTouch(0);
|
if (touch.phase == TouchPhase.Began)
|
{
|
PlayFX(touch.position);
|
}
|
// for (int i = 0; i < Input.touchCount; ++i)
|
// {
|
|
// }
|
}
|
else
|
{
|
if (Input.GetMouseButtonDown(0))
|
{
|
PlayFX(Input.mousePosition);
|
}
|
}
|
}
|
|
|
private void PlayFX(Vector2 tapPos)
|
{
|
if (Time.timeScale < 0.0001f) return;
|
|
RecycleEffectc fx = CreateFX();
|
|
RectTransform fxRectTrans = fx.gameObject.GetComponent<RectTransform>();
|
Vector2 fxLocalPos;
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(fxContainer, tapPos, fxRenderCamera, out fxLocalPos);
|
fxRectTrans.SetParent(fxContainer);
|
fxRectTrans.anchoredPosition3D = fxLocalPos;
|
//fxRectTrans.localScale = Vector3.one;
|
fx.gameObject.SetActive(true);
|
fx.StartPlay(fxLifeTime, RecycleFX);
|
}
|
|
|
private RecycleEffectc CreateFX()
|
{
|
RecycleEffectc newFX = null;
|
if (pool.Count > 0)
|
{
|
newFX = pool.Dequeue();
|
}
|
else
|
{
|
GameObject go = Instantiate(fxSample);
|
newFX = go.AddComponent<RecycleEffectc>();
|
}
|
return newFX;
|
}
|
|
private void RecycleFX(RecycleEffectc fx)
|
{
|
fx.gameObject.SetActive(false);
|
pool.Enqueue(fx);
|
}
|
|
}
|