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<UIClickEffect>();
        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<UIEffectSort>().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<Renderer>(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;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 1、介绍两大UI插件NGUI和UGUI2、unity渲染顺序控制方式3、NGUI的控制4、UGUI的控制5、...
    小飞不会飞_阅读 11,973评论 4 13
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,794评论 1 32
  • 前言:最近在做一个美术馆的场景,对于灯光的要求非常高,经过几天的调试和在网上查阅一些资料,总结一下。全局光照,简称...
    Tang7阅读 7,102评论 0 9
  • 1987年村里有一名退伍老兵回来了,5年前他带着全村人的希望踏上了当兵这条路,5年后他却用残疾的身躯出现在村人面...
    书声玥语阅读 1,722评论 2 2
  • 开学写在书本上的励志句 好好吃饭 好好睡觉 好好让自己变优秀 心里的垃圾定期倒一倒 什么事都会过去的 加...
    筏i阅读 5,080评论 0 1