Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[ad_1]

I might prefer to refactor my pooling class to work for extra object varieties in my prime down Unity sport. Proper now its working superb for participant projectiles, however I need it to additionally work for enemy projectiles, enemies, particle results, mainly something that will spawn and be destroyed. I additionally really feel that its slightly clunky to assign the projectilePrefab straight within the supervisor, so I used to be additionally questioning if one thing like an Interface like IPoolable can be the way in which to go.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
utilizing UnityEngine.Pool;

public class PoolManager : MonoBehaviour
{
    #area Pooled Objects

    [SerializeField] personal Projectile _projectilePrefab;
    [SerializeField] personal Enemy _enemyPrefab;

    personal ObjectPool<Projectile> _projectilePool;
    personal ObjectPool<Enemy> _enemyPool;

    #endregion

    [SerializeField] personal int _projectilePoolAmount;
    [SerializeField] personal bool _usePool = true;

    public static PoolManager Occasion { get; personal set; }
    personal GameObject _poolContainer;

    void Awake()
    {
        Occasion = this;

        _poolContainer = new GameObject($"PoolContainer - {_projectilePrefab.identify}");
        _poolContainer.rework.SetParent(rework);

        InitializeProjectilePool();
    }

    personal void InitializeProjectilePool()
    {
        _projectilePool = new ObjectPool<Projectile>(
            CreateProjectile,
            projectile => {
                projectile.gameObject.SetActive(true);
            }, projectile => {
                projectile.gameObject.SetActive(false); 
            }, projectile => {
                Destroy(projectile.gameObject); 
            }, false, 10, 200);
    }

    personal Projectile CreateProjectile()
    {
        Projectile projectileInstance = Instantiate(_projectilePrefab);
        projectileInstance.rework.SetParent(_poolContainer.rework);
        projectileInstance.gameObject.SetActive(false);
        return projectileInstance;
    }
    personal void KillProjectile(Projectile projectile)
    {
        if (_usePool) { _projectilePool.Launch(projectile); }
        else { Destroy(projectile.gameObject); }
    }

    public Projectile GetProjectile()
    {
        var projectile = _usePool ? _projectilePool.Get() : CreateProjectile();
        projectile.Init(KillProjectile);
        return projectile;
    }
}

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *