Sharing my codes

Hi!

I’ve created this post to share some of the codes I wrote in past projects or even in some prototypes I was working on.

All the codes that I will post here was written by myself, and everyone can use them as and when you want. I’m doing this because this is the way I can do something (I hope) for people who are learning and also to receive feedback and improvements about my codes too!

In short lines:

All the codes here are free to use! Use them as you wish, and if you have any suggestion, please, post here!

I’ll try to upload as many assets as I can.

1 Like

The first one is my Pool script, don’t know if this is the best way to do a pool, but it works pretty good I think!

To use it:

  • Create an empty GameObject, and name his tag as “PoolsManager”.
  • Add a PoolManager.cs script in it.

Now you have a pool manager. The PoolManager store all the pools in your game and is pretty easy to access them.

  • Create another empty GameObject
  • Add the SpawnPool script in it.
  • Name the pool and add a prefab instance.

And this is how you create a pool! Now, to spawn an object just call:

GameObject go = PoolManager.GetInstance().GetPool("MY_NAME" ).Spawn(transform.position, transform.rotation);

Or to get an specific component in the gameobject, use:

YourComponent comp = PoolManager.GetInstance().GetPool(poolName).Spawn<YourComponent>(transform.position, transform.rotation);

That’s it!

In this Pool script you can:

  • Dynamically create a pool.
  • Create a Particle Pool (ParticlePoolObject.cs, does not leave particles behind!).
  • Improve this script!

1845706–118385–Pools.unitypackage (12.4 KB)

1 Like

Just throwing it in here, but yhou might want to make a “public” project on github, or an other source sharing site that supports it well.

A forum post is great, but not always the best ways to keep up with it.

Just a personal advice though, feel free to ignore, or even to demand it’s removal (I’ll remove the post if you ask).

1 Like

Thanks for the advice!
I’ll create a repository at github as soon as possible, thanks again!

Another script…this one is simpler, the ideia is to cache some of the Unity components (Transform, GameObject…) a little bit easier.

Advantages:

  • You don’t need to declare again and again your variables.
  • It’s easier to other scripts to access your components.

To do this, instead of extending or inherit from MonoBehaviour, you will inherit from BaseMonoBehaviour script.
It’s also important

Example:

public class Player: [B]BaseMonoBehaviour [/B]
{
    public override void Awake ()
    {
        //IMPORTANT TO CALL THIS!!
        base.Awake ();
    }
}

1847024–118440–BaseMonoBehaviour.cs (679 Bytes)