• unity3d-对象池的用法


    对象池🐘🐘🐘

    提示:以下是本篇文章正文内容

    • 什么时候使用对象池?
    1. 具有重复、频繁出现然后消失的物体、

    以下有残影效果制作的code代码部分

    请添加图片描述

    • 对象池设计模式概念(已残影生成为例)

    请添加图片描述
    💡理解:对象池(Object Pool)但从字面理解就是一池子的物体,在我们需要使用的时候就拿出来一个,然后包装成我想用的样子。用完之后清空放回到池子里。

    • 为什么要有对象池呢?
    1. 对象池用于减少内存开销,其原理就是把可能用到到的对象,先存在一个地方(池),要用的时候就调出来,不用就放回去。
    2. 如果我们频繁使用 Instantiate 和 Destroy 来生成后销毁,那么会占用大量的系统内存,甚至导致系统崩溃。
    3. 我们可以利用对象池的设计模式概念,在游戏的一开始就制作若干个数量的物体,将他们设置 SetActive(false) 当我们需要它们的时候,只要把它们设置成 SetActive(true) 就好了,用完了再次设置为false。以此方法来循环使用,以达到优化系统的作用。
    4. 如果不使用对象池,我们不经浪费了很多时间在寻找合适的内存上,更让内存产生了很多的内存碎片。

    以冲锋残影的案例来说的话,我们只需要制作每一个残影的 Prefab 让它可以获得玩家的坐标位置和 Sprite 图像资源,然后随着时间推移降低不透明度,到达指定时间就返回对象池当中就可以了:
    请添加图片描述

    • 代码部分享,很有用哦!~所以赶紧收藏起来吧🔗🔗🔗~

    Player

    using UnityEngine;
    
    public class Player : MonoBehaviour
    {
        private Animator animator;
        private Rigidbody2D rigidbody2d;
        private Collider2D collider2d;
    
        public float speed, jumpForce;
        public Transform layerCheck;
        public  LayerMask layerMask;
    
        public bool isJump, isLayer,isDash;
    
        private bool jumpPressed;
        private  int jumpCount;
        private float hor;
    
        [Header("Dash")]
        public float dashTime;//跳跃时长
        private float dashTimeLeft;//剩余时间
        private float lastDashTime=-10; //上一次dash时间点
        public float dashCoolDown;
        public float dashSpeed;
    
        private void Awake()
        {
            animator = GetComponent();
            rigidbody2d = GetComponent();
            collider2d = GetComponent();
        }
    
        private void Update()
        {
            if (Input.GetButtonDown("Jump")&&jumpCount>0)
            {
                jumpPressed = true;
            }
            //冲刺
            if (Input.GetKeyDown(KeyCode.J))
            {
                if (Time.time >= (dashCoolDown + lastDashTime))
                {
                    //可以执行dash
                    ReadyToDash();
                }
            }
        }
    
        private void FixedUpdate()
        {
            isLayer = Physics2D.OverlapCircle(layerCheck.transform.position,0.1f,layerMask);
            Movenemt();
            Jump();
            SwitchAnimation();
            //跳跃
            Dash();
        }
    
        private void Movenemt()
        {
            hor = Input.GetAxisRaw("Horizontal");
            rigidbody2d.velocity = new Vector2(hor*speed, rigidbody2d.velocity.y);
    
            if (hor != 0)
            {
                transform.localScale = new Vector3(hor, 1, 1);
            }
        }
    
        private void Jump()
        {
            if (isLayer)
            {
                isJump = false;
                jumpCount = 2;
            }
            if (jumpPressed && isLayer)
            {
                isJump = true;
                rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, jumpForce);
                jumpCount--;
                jumpPressed = false;
            }else if (jumpPressed && isJump && jumpCount>0)
            {
                rigidbody2d.velocity = new Vector2(rigidbody2d.velocity.x, jumpForce);
                jumpCount--;
                jumpPressed = false;
            }
        }
    
        private void SwitchAnimation()
        {
    
            animator.SetFloat("isRun", Mathf.Abs(rigidbody2d.velocity.x));
    
            if (isLayer)
            {
                animator.SetBool("luo", false);
                animator.SetBool("isIdel", true);
            }
            else if (!isLayer && rigidbody2d.velocity.y > 0)
            {
                animator.SetBool("isIdel", false);
                animator.SetBool("isJump", true);
            }
            else if( rigidbody2d.velocity.y < 0)
            {
                animator.SetBool("isJump", false);
                animator.SetBool("luo", true);
            }
        }
    
        private void ReadyToDash()
        {
    
            isDash = true;
    
            dashTimeLeft = dashTime;
    
            lastDashTime = Time.time;
    
        }
    
        private void Dash()
        {
            if (isDash)
            {
    
                if (dashTimeLeft > 0)
                {
    
                    if (rigidbody2d.velocity.y > 0&& isLayer)
                    {
                        rigidbody2d.velocity = new Vector2(dashSpeed * hor, jumpForce);
                    }
                    rigidbody2d.velocity = new Vector2(dashSpeed * hor, rigidbody2d.velocity.y);
    
                    dashTimeLeft -= Time.deltaTime;
    
                    ShadowPool.Instance.DequeueS();
                }
                if (dashTimeLeft <= 0)
                {
                    isDash = false;
                    if (!isLayer)
                    {
                        //目的为了在空中结束 Dash 的时候可以接一个小跳跃。根据自己需要随意删减调整
                        rigidbody2d.velocity = new Vector2(dashSpeed * hor, jumpForce);
                    }
                }
            }
    
        }
    }
    
    
    • 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
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156

    在这里插入图片描述

    ShadowPool

    using System.Collections;
    using UnityEngine;
    
    public class ShadowPool : MonoBehaviour
    {
        public static ShadowPool Instance;
        //队列
        private Queue queueS = new Queue();
    
        public GameObject shadowPrefab;
    
        public int createpPrefabCount;
    
        private void Awake()
        {
            Instance = this;
            FillPool();
        }
        
        private void FillPool()
        {
            for (int i = 0; i < createpPrefabCount; i++)
            {
                var player = Instantiate(shadowPrefab);
                player.transform.SetParent(transform);
    
                //取消启动 返回对象池
                EnqueueS(player);
            }
        }
    
        public void EnqueueS(GameObject gameObject)
        {
            gameObject.SetActive(false);
    
            queueS.Enqueue(gameObject);
        }
    
        public GameObject DequeueS()
        {
            if (queueS.Count == 0)
            {
                FillPool();
            }
    
            var showShadow = (GameObject)queueS.Dequeue();
    
            showShadow.SetActive(true);
    
            return showShadow;
        }
    }
    
    • 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

    在这里插入图片描述

    ShadowPrefab

    
    using UnityEngine;
    
    public class ShadowPrefab : MonoBehaviour
    {
        private Transform player;
    
        private SpriteRenderer plyerSprite;
        private SpriteRenderer thisSprite;
    
        [Header("颜射")]
        private Color thisColor;
    
        [Header("事件")]
        private float startTime;
        public float time;
    
        [Header("对象的透明度")]
        private float lucency;
        public float startLucencyValue;
        public  float lucencyRide;
    
       
    
        private void OnEnable()
        {
            player = GameObject.FindGameObjectWithTag("Player").transform;
            plyerSprite = player.GetComponent();
            thisSprite = GetComponent();
            thisSprite.sprite = plyerSprite.sprite;
    
    
            lucency=startLucencyValue;
    
            transform.position = player.transform.position;
            transform.rotation = player.rotation;
            transform.localScale = player.localScale;
    
            startTime = Time.time;
    
        }
    
        private void Update()
        {
            lucency *= lucencyRide;
    
            thisColor = new Color(0.5f, 0.5f, 1, lucency);
    
            thisSprite.color = thisColor;
    
            if (Time.time >= startTime + time)
            {
                //返回对象池
                ShadowPool.Instance.EnqueueS(this.gameObject);
            }
        }
    }
    
    
    • 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

    在这里插入图片描述

    GitHub项目分享地址👉👉👉:https://github.com/Gitxiaomanong/unity3d-ObjectPool
    unity版本2020.3

  • 相关阅读:
    Spring MVC中@InitBinder注解是如何应用的?
    kafka如何保证消息不丢?
    CAS机制
    初创公司用“豆包”大模型,日均tokens两个月内增长357倍
    如何防止html转义函数
    前端请求后台接口失败处理逻辑
    Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步
    前后端分离--前置路由守卫(登录过滤)和整合shiro安全框架
    springboot actuator:开放全部(部分)端点、端点映射、端点保护
    前端笔记(html、css、vue、vue3、react、jquery)
  • 原文地址:https://blog.csdn.net/m0_61490399/article/details/125956288