despawnAfterDelay() public static method

sticks the GameObject back into it's recycle bin after a delay. If the GameObject has no bin it is destroyed.
public static despawnAfterDelay ( GameObject go, float delayInSeconds ) : void
go GameObject Go.
delayInSeconds float
return void
Esempio n. 1
0
    /// <summary>
    /// fetches a new instance from the recycle bin. Returns null if we reached the hardLimit.
    /// </summary>
    public GameObject spawn()
    {
        var go = pop();

        if (go != null)
        {
            if (onSpawnedEvent != null)
            {
                onSpawnedEvent(go);
            }

            if (automaticallyRecycleParticleSystems)
            {
                var system = go.GetComponent <ParticleSystem>();
                if (system)
                {
                    // we add the startLifetime to the system's duration to avoid it getting recycled while emitting.
                    // note that curves can extend the startLifetime so this isn't perfect
                    TrashMan.despawnAfterDelay(go, system.duration + system.startLifetime);
                }
                else
                {
                    Debug.LogError("automaticallyRecycleParticleSystems is true but there is no ParticleSystem on this GameObject!");
                }
            }
        }

        return(go);
    }
Esempio n. 2
0
 // sets the lifespan of the item
 public virtual void OnEnable()
 {
     healthCurrent = healthMax;
     if (limitedTime)
     {
         TrashMan.despawnAfterDelay(gameObject, lifespan);
     }
 }
Esempio n. 3
0
    void Spawn()
    {
        var newObj = TrashMan.spawn(obj[Random.Range(0, obj.Length)], transform.position, Quaternion.Euler(0, 90, 0));

        TrashMan.despawnAfterDelay(newObj, 3);


        Invoke("Spawn", Random.Range(spawnMin, spawnMax));
    }
Esempio n. 4
0
    void OnGUI()
    {
        if (GUILayout.Button("Spawn Cube"))
        {
            var newObj = TrashMan.spawn(cubePrefab, Random.onUnitSphere * 5f, Random.rotation);
            TrashMan.despawnAfterDelay(newObj, Random.Range(1f, 2f));
        }


        if (GUILayout.Button("Spawn Sphere"))
        {
            var newObj = TrashMan.spawn(spherePrefab, Random.onUnitSphere * 3f);

            // spheres have a hardLimit set so we need to null check before using them
            if (newObj)
            {
                newObj.transform.parent = transform;
                TrashMan.despawnAfterDelay(newObj, Random.Range(5f, 8f));
            }
        }


        if (GUILayout.Button("Spawn Light from Scene"))
        {
            var newObj = TrashMan.spawn("light", Random.onUnitSphere * 10f);

            if (newObj)
            {
                newObj.transform.parent = transform;
                TrashMan.despawnAfterDelay(newObj, Random.Range(5f, 8f));
            }
        }


        if (GUILayout.Button("Spawn Particles by GameObject Name"))
        {
            TrashMan.spawn("Particles", Random.onUnitSphere * 3f);
        }


        if (GUILayout.Button("Create Recycle Bin at Runtime"))
        {
            _didCreateCapsuleRecycleBin = true;
            var recycleBin = new TrashManRecycleBin()
            {
                prefab = capsulePrefab
            };
            TrashMan.manageRecycleBin(recycleBin);
        }


        if (_didCreateCapsuleRecycleBin && GUILayout.Button("Spawn Capsule"))
        {
            var newObj = TrashMan.spawn(capsulePrefab, Random.onUnitSphere * 5f, Random.rotation);
            TrashMan.despawnAfterDelay(newObj, Random.Range(1f, 5f));
        }
    }
Esempio n. 5
0
    void Spawn()
    {
        //Instantiate(obj[Random.Range(0, obj.Length)], transform.position, Quaternion.identity);
        //Debug.Log("countFloor_3: " + countFloor_3);

        var newObj = TrashMan.spawn(obj[Random.Range(0, obj.Length)], transform.position, Quaternion.identity);

        TrashMan.despawnAfterDelay(newObj, 3);


        Invoke("Spawn", Random.Range(spawnMin, spawnMax));
    }
Esempio n. 6
0
        IEnumerator _testPool()
        {
            TrashManRecycleBin data   = new TrashManRecycleBin();
            GameObject         prefab = new GameObject();

            data.prefab = prefab;
            TrashMan.manageRecycleBin(data);
            for (int i = 0; i < 66; ++i)
            {
                var newObj = TrashMan.spawn(prefab);
                TrashMan.despawnAfterDelay(newObj, 2f);
                yield return(new WaitForSeconds(3f));
            }
        }
Esempio n. 7
0
    void OnGUI()
    {
        if (GUILayout.Button("Spawn Cube"))
        {
            var newObj = TrashMan.spawn(cubePrefab, Random.onUnitSphere * 5f, Random.rotation);
            TrashMan.despawnAfterDelay(newObj, Random.Range(1f, 2f));
        }


        if (GUILayout.Button("Spawn Sphere"))
        {
            var newObj = TrashMan.spawn(spherePrefab, Random.onUnitSphere * 3f);

            // spheres have a hardLimit set so we need to null check before using them
            if (newObj)
            {
                newObj.transform.parent = transform;
                TrashMan.despawnAfterDelay(newObj, Random.Range(5f, 8f));
            }
        }


        if (GUILayout.Button("Spawn Light from Scene"))
        {
            var newObj = TrashMan.spawn("light", Random.onUnitSphere * 10f);

            if (newObj)
            {
                newObj.transform.parent = transform;
                TrashMan.despawnAfterDelay(newObj, Random.Range(5f, 8f));
            }
        }


        if (GUILayout.Button("Spawn Particles by GameObject Name"))
        {
            TrashMan.spawn("Particles", Random.onUnitSphere * 3f);
        }

#if UNITY_4_6 || UNITY_5_0
        if (GUILayout.Button("Spawn UI element"))
        {
            CreateCanvas();
            var go = TrashMan.spawn(uiPrefab, Vector2.zero);
            go.transform.SetParent(canvasRoot.transform, true);
            var rt = go.transform as RectTransform;
            rt.anchoredPosition = new Vector2(Random.Range(-380, 380), Random.Range(-280, 280));
            TrashMan.despawnAfterDelay(go, Random.Range(1f, 5f));
        }
#endif
        if (GUILayout.Button("Create Recycle Bin at Runtime"))
        {
            _didCreateCapsuleRecycleBin = true;
            var recycleBin = new TrashManRecycleBin()
            {
                prefab = capsulePrefab
            };
            TrashMan.manageRecycleBin(recycleBin);
        }


        if (_didCreateCapsuleRecycleBin && GUILayout.Button("Spawn Capsule"))
        {
            var newObj = TrashMan.spawn(capsulePrefab, Random.onUnitSphere * 5f, Random.rotation);
            TrashMan.despawnAfterDelay(newObj, Random.Range(1f, 5f));
        }
    }
Esempio n. 8
0
 void OnEnable()
 {
     TrashMan.despawnAfterDelay(gameObject, timer);
 }
Esempio n. 9
0
	public void ParticleJetStep ()
	{
		var newObj = TrashMan.spawn( JetStep, transform.position, Quaternion.identity );
		TrashMan.despawnAfterDelay( newObj, 3 );
	}
Esempio n. 10
0
 public virtual void OnEnable()
 {
     TrashMan.despawnAfterDelay(gameObject, lifeSpan);
 }