Narrow Prefab Selection through attached Scripts

Hi!


I just edited my ObjectPool Script that takes in Scripts derived from the abstract PoolItem Class. (or however you would call that)

Sadly when I want to assign the Prefabs I used before, I can’t even find a single Object to take in, although I have at least 2 Prefabs that have a PoolItem derived Script attached.

Isn’t there a Way to make the Editor look for GameObjects that only have a required Script attached?

Or would that at least be something people need? Wha do you think about that?

Guess I have to rollback and assign GameObjects from the huge cluster of Prefabs in the Selection Window, and then log something in the OnValidate method. But my approach feels …better… to me^^


Someone out there able to help?


Have a nice time coding :wink:

In your own code you can just check that gameObject.GetComponent< particularScriptClass >() does not return null.

Regarding the editor component; it looks to me, like this function DOES allow you specify a particular type for the selected object, the second parameter. Have you tried this?

particularScriptClassObject=EditorGUILayout.ObjectField(particularScriptClassObject, typeof( particularScriptClass), false);

Well, all I did, was replacing GameObjects that are taken in with the PoolItem Script. I eventaully had to figure, that Unity doesn’t check the Asset GameObjects for their Scripts, which kind of was a pain in the ass… I kinda do relaize i need something like github now^^

I think its a bit more complex than you think :wink:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class ObjectPool
{

    public GameObject           prefab;

    private string              identifier = "";

    public bool                 grows = true,
                                found;

    public int                  curItem     = 0,
                                lastObject  = 0,
                                max         = 0;



    [ReadOnly] public List<GameObject>         pool            = new List<GameObject>();
    [ReadOnly] public LinkedList<PoolItem>     recyclingPool   = new LinkedList<PoolItem>();



    public ObjectPool(GameObject prefab, int amount)
    {
        ValidateObject(prefab);
        this.prefab = prefab;
        AddObjects(amount);
    }

    private void ValidateObject(GameObject prefab)
    {
        if (!prefab.GetComponent<PoolItem>())
        {
            Debug.Log(prefab.name + ": Attaching PoolItem");
            prefab.AddComponent<PoolItem>();
        }
    }

    public void AddObjects(int amount)
    {
        for (int i = 0; i < amount; i++)
        {
            GameObject obj = CreatePrefab();
            obj.name += identifier + " " + i.ToString();
            pool.Add(obj);
        }
    }

    private GameObject CreatePrefab()
    {
        GameObject obj = MonoBehaviour.Instantiate(prefab);
        obj.SetActive(false);
        return obj;
    }

    public GameObject GetNext(out bool needsRecycling)
    {
        GameObject ret = null;
        found = false;
        needsRecycling = false;
        while (!found)
        {
            if (recyclingPool.Count > 0)
            {
                PoolItem item =  recyclingPool.First.Value;

                item.CancelInvoke("Disable");
                needsRecycling = true;

                ret = item.gameObject;
                recyclingPool.RemoveFirst();
            }
            else if (!pool[curItem].gameObject.activeInHierarchy)
            {
                ret = pool[curItem];
                lastObject = curItem;

                IncrementNext();
                found = true;
            }
            else
            {
                IncrementNext();
                if (lastObject == curItem)
                {
                    if (grows)
                    {
                        ret = CreatePrefab();
                        pool.Add(ret);

                        IncrementNext();
                        lastObject = curItem;
                        found = true;

                    }
                    else
                        break;
                }
            }
        }
        return ret;
    }

    private void IncrementNext()
    {
        curItem++;
        if (curItem >= pool.Count)
        {
            curItem = 0;
        }
    }

    public void RefreshPrefabs(GameObject newPrefab = null)
    {
        if (newPrefab)
        {
            prefab = newPrefab;
        }
        for (int i = 0; i < pool.Count; i++)
        {
            GameObject oldPrefab = pool*;*

pool = CreatePrefab();
MonoBehaviour.Destroy(oldPrefab);
}
}
}
I wanted to avoid the enabling/disabling of Gameobjects, because they turn out to be very cost-intensive when enabling/disabling hundreds in just a few seconds (bullethell). So I decided to give the used gameobjects a time span, where they can be recycled, without disabling and enabling them. I figured it would also make much more sense to have one big pool manager that pools as many objects of the same class as possible, to maximize the recycling rate.
Actually, I’m still on it, and it would have been easier to give all the scripts that are pooling the PoolItem Component of the going-to-be-pooled objects.
Thanks for your help anyways. Am very grateful for the community feeling :wink: