找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
游戏黄埔已经开课啦,大家速速报名赶快上车
查看: 1937|回复: 0

对象池演示素材Unity包

[复制链接]

162

主题

33

回帖

891

积分

管理员

积分
891
发表于 2025-2-1 21:11:25 | 显示全部楼层 |阅读模式

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;


  4. public class Coin : MonoBehaviour
  5. {
  6.     private void OnCollisionEnter(Collision collision)
  7.     {
  8.         if (collision.gameObject.layer==LayerMask.NameToLayer("ground"))
  9.         {
  10.             CoinPool.instance.coinPool.Release(gameObject);
  11.         }
  12.     }
  13. }
复制代码
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Pool;

  6. public class CoinPool : MonoBehaviour
  7. {
  8.     public GameObject coin;
  9.     public static CoinPool instance;
  10.     public ObjectPool<GameObject> coinPool;
  11.     private void Awake()
  12.     {
  13.         coinPool = new ObjectPool<GameObject>(CreateFunc,ActionOnGet,ActionOnRelease,ActionOnDestroy,true,10,1000);
  14.         instance = this;
  15.     }

  16.     private void ActionOnDestroy(GameObject obj)
  17.     {
  18.         Destroy(obj);
  19.     }

  20.     private void ActionOnRelease(GameObject obj)
  21.     {
  22.         obj.SetActive(false);
  23.     }

  24.     private void ActionOnGet(GameObject obj)
  25.     {
  26.         obj.SetActive(true);
  27.     }

  28.     private GameObject CreateFunc()
  29.     {
  30.         var obj=Instantiate(coin,transform);
  31.         return obj;
  32.     }
  33.    
  34. }


复制代码
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;

  5. public class Spawn : MonoBehaviour
  6. {
  7.     public Transform spawnTrans;
  8.     public float spawnInterval = 2;
  9.     public float spawnTimer;
  10.     public float minX = -10;
  11.     public float maxX = 10;
  12.     public Canvas _canvas;
  13.     private TMP_Text countActive;
  14.     private TMP_Text countAll;
  15.     private TMP_Text countInActive;




  16.     private void Start()
  17.     {
  18.         countActive = _canvas.transform.Find("t1/countActive").GetComponent<TMP_Text>();
  19.         if (countActive==null)
  20.         {
  21.             print("countActive为空了");
  22.         }
  23.         countAll = _canvas.transform.Find("t2/countAll").GetComponent<TMP_Text>();
  24.         countInActive = _canvas.transform.Find("t3/countInActive").GetComponent<TMP_Text>();
  25.     }
  26.     // Start is called before the first frame update
  27.     private void Update()
  28.     {
  29.         spawnTimer += Time.deltaTime;
  30.         if (spawnTimer >= spawnInterval)
  31.         {
  32.             spawnTimer -= spawnInterval;
  33.             SpawnCopin();
  34.         }
  35.         countActive.text = CoinPool.instance.coinPool.CountActive.ToString();
  36.         countAll.text= CoinPool.instance.coinPool.CountAll.ToString();
  37.         countInActive.text= CoinPool.instance.coinPool.CountInactive.ToString();
  38.     }
  39.     public void SpawnCopin()
  40.     {
  41.         spawnTrans.position = new Vector3(Random.Range(minX, maxX), spawnTrans.position.y, spawnTrans.position.z);
  42.         GameObject temp = CoinPool.instance.coinPool.Get();
  43.         temp.transform.position = spawnTrans.position;
  44.     }
  45. }
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|平顶山市图灵科技 ( 豫ICP备2024088136号-1| 豫公网安备41040202000275号 )

GMT+8, 2025-5-22 22:35 , Processed in 0.047299 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表