using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickEffect : MonoBehaviour
{
///
/// 屏幕特效原始资源
///
public GameObject fxSample;
///
/// 屏幕特效的生命时长,超过后会进行缓存
///
public float fxLifeTime = 1.0f;
///
/// 屏幕特效的容器(父对象)
///
public RectTransform fxContainer;
///
/// 屏幕特效渲染使用的相机
///
private Camera fxRenderCamera;
private Queue pool = new Queue(20);
private void Awake()
{
if (fxSample == null)
{
Debug.LogErrorFormat("没有找到屏幕特效");
this.enabled = false;
}
else
{
fxSample.SetActive(false);
}
fxRenderCamera = GameObject.Find("UICamera").GetComponent();
}
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();
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();
}
return newFX;
}
private void RecycleFX(RecycleEffectc fx)
{
fx.gameObject.SetActive(false);
pool.Enqueue(fx);
}
}