From d60e7d1909000303e7dfc4fcaa3663e8bcb0f670 Mon Sep 17 00:00:00 2001 From: Zenovore <agustinus.alexander17@gmail.com> Date: Sat, 9 Apr 2022 19:31:04 +0700 Subject: [PATCH] feat/object-pooling --- Survival Shooter/Assets/Prefabs/Bullet.prefab | 2 +- .../Assets/Prefabs/ObjectPooler.prefab | 49 +++++++++++++ .../Assets/Prefabs/ObjectPooler.prefab.meta | 7 ++ Survival Shooter/Assets/Scenes/WaveMode.unity | 57 ++++++++++++++++ Survival Shooter/Assets/Scenes/ZenMode.unity | 57 ++++++++++++++++ .../Assets/Scripts/Bullet/Bullet.cs | 15 ++-- .../Assets/Scripts/IPooledObject.cs | 6 ++ .../Assets/Scripts/IPooledObject.cs.meta | 11 +++ .../Assets/Scripts/Managers/PowerManager.cs | 2 +- .../Assets/Scripts/Managers/SpeedManager.cs | 2 +- .../Assets/Scripts/ObjectPooler.cs | 68 +++++++++++++++++++ .../Assets/Scripts/ObjectPooler.cs.meta | 11 +++ .../Assets/Scripts/Player/PlayerMovement.cs | 2 +- .../Assets/Scripts/Player/PlayerShooting.cs | 12 +++- 14 files changed, 289 insertions(+), 12 deletions(-) create mode 100644 Survival Shooter/Assets/Prefabs/ObjectPooler.prefab create mode 100644 Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta create mode 100644 Survival Shooter/Assets/Scripts/IPooledObject.cs create mode 100644 Survival Shooter/Assets/Scripts/IPooledObject.cs.meta create mode 100644 Survival Shooter/Assets/Scripts/ObjectPooler.cs create mode 100644 Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta diff --git a/Survival Shooter/Assets/Prefabs/Bullet.prefab b/Survival Shooter/Assets/Prefabs/Bullet.prefab index 5f3894b..61ea4d3 100644 --- a/Survival Shooter/Assets/Prefabs/Bullet.prefab +++ b/Survival Shooter/Assets/Prefabs/Bullet.prefab @@ -67,7 +67,7 @@ MonoBehaviour: isRotating: 1 isFloating: 0 isScaling: 0 - rotationAngle: {x: 0, y: 0, z: 10} + rotationAngle: {x: 10, y: 0, z: 10} rotationSpeed: 5 floatSpeed: 0 floatRate: 0 diff --git a/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab new file mode 100644 index 0000000..a4c0ed6 --- /dev/null +++ b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab @@ -0,0 +1,49 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &1663985618794767950 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1663985618794767948} + - component: {fileID: 1663985618794767951} + m_Layer: 0 + m_Name: ObjectPooler + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1663985618794767948 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1663985618794767950} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1663985618794767951 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1663985618794767950} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5cbfe0fa2edd3034d9f950788afa7c83, type: 3} + m_Name: + m_EditorClassIdentifier: + pools: + - tag: Bullet + prefab: {fileID: 8946891882775703378, guid: d6c526ea9d76cdd468b8425ad07a3ded, type: 3} + size: 35 diff --git a/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta new file mode 100644 index 0000000..0a90950 --- /dev/null +++ b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c19968325c6af3d44b67fc4746e2355e +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Survival Shooter/Assets/Scenes/WaveMode.unity b/Survival Shooter/Assets/Scenes/WaveMode.unity index 7d9371f..9d5ddd3 100644 --- a/Survival Shooter/Assets/Scenes/WaveMode.unity +++ b/Survival Shooter/Assets/Scenes/WaveMode.unity @@ -3204,6 +3204,63 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 533adfa42ba500c43a62a71ebc8f0c99, type: 3} +--- !u!1001 &1663985618332078390 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_RootOrder + value: 15 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767950, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_Name + value: ObjectPooler + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} --- !u!4 &2719544848253408655 stripped Transform: m_CorrespondingSourceObject: {fileID: 1293407273377250347, guid: 6e3d49b904179974ab53cbb0d368a260, type: 3} diff --git a/Survival Shooter/Assets/Scenes/ZenMode.unity b/Survival Shooter/Assets/Scenes/ZenMode.unity index 1c3d334..0eac9b7 100644 --- a/Survival Shooter/Assets/Scenes/ZenMode.unity +++ b/Survival Shooter/Assets/Scenes/ZenMode.unity @@ -604,6 +604,63 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: playerHealth: {fileID: 875225643} +--- !u!1001 &305081429 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_RootOrder + value: 15 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767950, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_Name + value: ObjectPooler + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} --- !u!1 &529888204 GameObject: m_ObjectHideFlags: 0 diff --git a/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs b/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs index f33c47a..2a7cb7a 100644 --- a/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs +++ b/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs @@ -2,7 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class Bullet : MonoBehaviour +public class Bullet : MonoBehaviour, IPooledObject { public float speed = 100.0f; public int damage = 20; @@ -26,7 +26,7 @@ public class Bullet : MonoBehaviour bool isCrit = false; // Start is called before the first frame update - void Start() + public void OnObjectSpawn() { player = GameObject.FindGameObjectWithTag ("Player"); playerShooting = player.GetComponentInChildren <PlayerShooting> (); @@ -36,6 +36,8 @@ public class Bullet : MonoBehaviour gunAudioArr = GetComponents<AudioSource>(); critAudio = gunAudioArr[0]; bulletCritChance = PlayerShooting.critChance; + hasHit = false; + timer = 0; } // Update is called once per frame @@ -50,7 +52,8 @@ public class Bullet : MonoBehaviour // Schedule for destruction if bullet never hits anything. if (timer >= life) { - Destroy(gameObject); + // Destroy(gameObject); + gameObject.SetActive(false); } velocity = transform.forward; @@ -117,10 +120,12 @@ public class Bullet : MonoBehaviour { envHit.ShowHit(hit.point); } - Destroy(gameObject); + // Destroy(gameObject); + gameObject.SetActive(false); } void DelayedDestroy() { - Destroy(gameObject, 0.2f); + // Destroy(gameObject, 0.2f); + gameObject.SetActive(false); } } diff --git a/Survival Shooter/Assets/Scripts/IPooledObject.cs b/Survival Shooter/Assets/Scripts/IPooledObject.cs new file mode 100644 index 0000000..bd8bdc2 --- /dev/null +++ b/Survival Shooter/Assets/Scripts/IPooledObject.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +public interface IPooledObject +{ + void OnObjectSpawn(); +} diff --git a/Survival Shooter/Assets/Scripts/IPooledObject.cs.meta b/Survival Shooter/Assets/Scripts/IPooledObject.cs.meta new file mode 100644 index 0000000..9377760 --- /dev/null +++ b/Survival Shooter/Assets/Scripts/IPooledObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 817e955d67ecbb144a2f776b2dc484ec +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs b/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs index 0b6efd2..7b42407 100644 --- a/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs +++ b/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs @@ -15,7 +15,7 @@ public class PowerManager : MonoBehaviour // player = GameObject.FindGameObjectWithTag("Player"); // playerShooting = player.GetComponent<PlayerShooting>(); text = GetComponent<Text>(); - power = 0; + power = 1; } // Update is called once per frame diff --git a/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs b/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs index f564168..54018e7 100644 --- a/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs +++ b/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs @@ -11,7 +11,7 @@ public class SpeedManager : MonoBehaviour void Start() { text = GetComponent<Text>(); - speed = 0f; + speed = 1f; } // Update is called once per frame diff --git a/Survival Shooter/Assets/Scripts/ObjectPooler.cs b/Survival Shooter/Assets/Scripts/ObjectPooler.cs new file mode 100644 index 0000000..e15a717 --- /dev/null +++ b/Survival Shooter/Assets/Scripts/ObjectPooler.cs @@ -0,0 +1,68 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ObjectPooler : MonoBehaviour +{ + [System.Serializable] + public class Pool{ + public string tag; + public GameObject prefab; + public int size; + } + #region Singleton + public static ObjectPooler Instance; + + private void Awake() + { + Instance = this; + } + #endregion + public List<Pool> pools; + public Dictionary<string, Queue<GameObject>> poolDictionary; + // Start is called before the first frame update + void Start() + { + poolDictionary = new Dictionary<string, Queue<GameObject>>(); + + foreach (Pool pool in pools) + { + Queue<GameObject> objectPool = new Queue<GameObject>(); + + for (int i = 0; i < pool.size; i++) + { + GameObject obj = Instantiate(pool.prefab); + obj.SetActive(false); + objectPool.Enqueue(obj); + } + + poolDictionary.Add(pool.tag, objectPool); + } + } + + public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation){ + + if (!poolDictionary.ContainsKey(tag)) + { + Debug.LogWarning("Pool with tag " + tag + " doesn't exist"); + return null; + } + + GameObject objectToSpawn = poolDictionary[tag].Dequeue(); + objectToSpawn.SetActive(true); + objectToSpawn.transform.position = position; + objectToSpawn.transform.rotation = rotation; + + IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>(); + + if (pooledObj != null) + { + pooledObj.OnObjectSpawn(); + } + + poolDictionary[tag].Enqueue(objectToSpawn); + + return objectToSpawn; + } + +} diff --git a/Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta b/Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta new file mode 100644 index 0000000..11523ae --- /dev/null +++ b/Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5cbfe0fa2edd3034d9f950788afa7c83 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs b/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs index 13c1f36..4d0f14c 100644 --- a/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs +++ b/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs @@ -55,7 +55,7 @@ public class PlayerMovement : MonoBehaviour public void speedOrb(){ if (speed < 10f){ speed += 0.5f; - SpeedManager.speed += 0.5f; + SpeedManager.speed += 1f; } } diff --git a/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs b/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs index 9f07e7b..3651098 100644 --- a/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs +++ b/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs @@ -7,9 +7,9 @@ public class PlayerShooting : MonoBehaviour public float range = 100f; public static int critChance = 0; - public static int bulletCount = 5; + public static int bulletCount = 1; - public GameObject bullets; + // public GameObject bullets; float timer; Ray shootRay; RaycastHit shootHit; @@ -20,7 +20,12 @@ public class PlayerShooting : MonoBehaviour AudioSource gunAudio; Light gunLight; float effectsDisplayTime = 0.2f; + ObjectPooler objectPooler; + + private void Start(){ + objectPooler = ObjectPooler.Instance; + } void Awake() { shootableMask = LayerMask.GetMask("Shootable"); @@ -79,7 +84,8 @@ public class PlayerShooting : MonoBehaviour int index = bullet==0 ? 0 : bullet*2; // gunLine.SetPosition(index, transform.position); - Instantiate(bullets, transform.position, q * transform.rotation); + ObjectPooler.Instance.SpawnFromPool("Bullet", transform.position, q * transform.rotation); + // Instantiate(bullets, transform.position, q * transform.rotation); // if (Physics.Raycast(shootRay.origin,shootRay.direction, out shootHit, range, shootableMask)) // { -- GitLab