Unity游戏中的全局点击特效

管理类UIEffectManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIEffectManager 
{

    private static UIEffectManager _instance;
    public static UIEffectManager Instance()
    {
        if (_instance == null)
        {
            _instance = new UIEffectManager();
        }
        return _instance;
    }

    private Transform effectParent;

    public void Init()
    {
        effectParent = new GameObject().transform;
        effectParent.gameObject.layer = LayerMask.NameToLayer("UI");
        effectParent.gameObject.AddComponent();
        effectParent.name = "UI_Effects";
        effectParent.SetParent(UINodesManager.TopUIRoot.transform);
        effectParent.localScale = Vector3.one;
        effectParent.localRotation = Quaternion.identity;
        effectParent.localPosition = Vector3.zero;
    }

    private const string ClickEffectName = "dianji";

    private GameObject GetEffectFromPool()
    {
        GameObject gg = null;
        int childCount = effectParent.childCount;
        for(int i =0;i< childCount;i++)
        {
            Transform tf = effectParent.GetChild(i);
            if (tf.name.Equals(ClickEffectName) && !tf.gameObject.activeInHierarchy)
            {
                return tf.gameObject;
            }
        }
        return gg;
    }

    public void PlayUIClickEffect(Vector3 v3)
    {
        GameObject eGo = null;
        eGo = GetEffectFromPool();
        if(eGo != null)
        {
            eGo.transform.position = v3;
            eGo.SetActive(true);
        }
        else
        {
            DataLite.LoadOne(string.Format("{0}{1}{2}{3}", PathManager.ABFilePath(), PathManager.AB_UI_Effect, ClickEffectName, ".assetbundle"), delegate (ResLoadInfo res, object param) {
                if (res.content.success && res.content.preLoadObjectArr.Length > 0)
                {
                    eGo = res.content.preLoadObjectArr[0] as GameObject;
                    if (eGo != null)
                    {
#if UNITY_EDITOR
                        ToolKit.SetTrueShaderToGameObject(eGo);
#endif
                        eGo = Object.Instantiate(eGo);
                        eGo.AddComponent().ChangeSort(3100);
                        eGo.name = ClickEffectName;
                        eGo.layer = LayerMask.NameToLayer("UI");
                        eGo.transform.SetParent(effectParent);
                        eGo.transform.position = v3;
                        eGo.transform.localScale = Vector3.one;
                    }
                }
                res.Unload();
            }, null, "", true);
        }
    }
}

UIClickEffect 点击特效类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIClickEffect : MonoBehaviour
{
    //300秒内未点击屏幕
    private const int FreeTime = 300;

    private float lastClickTime;
    private static bool isLevelState = false;


#if !UNITY_EDITOR
    bool isTouched = false;
#endif
    // Update is called once per frame
    void Update()
    {
#if UNITY_EDITOR
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            PlayEffect(Input.mousePosition);
            lastClickTime = Time.realtimeSinceStartup;
        }
#else
        if (Input.touchCount > 0 && !isTouched)
        {
            lastClickTime = Time.realtimeSinceStartup;
            PlayEffect(Input.GetTouch(0).position);
            isTouched = true;
        }
        else if(Input.touchCount == 0)  isTouched = false;
#endif
        if (lastClickTime > 0 && Time.realtimeSinceStartup - lastClickTime >= FreeTime)
        {
            if (!isLevelState)
            {
                //发送心跳包正在点击屏幕
            }
            isLevelState = true;
        }
        else if (isLevelState)
        {
            //发送心跳包没有在点击屏幕
            isLevelState = false;
        }
    }

    //对应安卓
    private void PlayEffect(Vector3 pos)
    {
        Vector3 v3 = UINodesManager.UICamera.ScreenToWorldPoint(pos);
        UIEffectManager.Instance().PlayUIClickEffect(v3);
    }

    //对应iOS
    Vector3 npos = Vector3.zero;
    private void PlayEffect(Vector2 pos)
    {
        npos.x = pos.x;
        npos.y = pos.y;
        Vector3 v3 = UINodesManager.UICamera.ScreenToWorldPoint(npos);

        UIEffectManager.Instance().PlayUIClickEffect(v3);
    }
}

管理层级的类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIEffectSort : MonoBehaviour
{
    public int sortingOrder = 1000;
    public bool sortUpdate = false;

    private Renderer[] m_EffectRend;
    void Awake()
    {
        //获取脚本下所有Renderer
        m_EffectRend = GetComponentsInChildren(true);

        Sort();
    }

    private void Sort()
    {
        //遍历Renderer 
        for (int i = 0; i < m_EffectRend.Length; i++)
        {
            m_EffectRend[i].sharedMaterial.renderQueue = sortingOrder;
            m_EffectRend[i].sortingOrder = sortingOrder; //设置层级
        }
    }

    public void ChangeSort(int order)
    {
        sortingOrder = order;
        Sort();
    }

    private void Update()
    {
        if (!open) return;
        if (sortUpdate)
        {
            Sort();
        }
    }

    private void OnDestroy()
    {
        sortUpdate = false;
    }
    private bool open = false;
    private void OnEnable()
    {
        open = true;
    }
    private void OnDisable()
    {
        open = false;
    }
}

你可能感兴趣的:(Unity游戏中的全局点击特效)