From 15c2849090392613063115167f685e739e163831 Mon Sep 17 00:00:00 2001 From: Eicy Date: Wed, 10 Jul 2024 16:39:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E8=B1=A1=E6=B1=A0?= =?UTF-8?q?=E5=B9=B6=E6=9B=B4=E6=96=B0KeySettingManager=E7=9A=84README?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KeyboardInput/KeySettingManager.cs | 2 +- KeyboardInput/README.md | 21 ++-- .../SingletonObjectPoolerInitializer.cs | 29 +++++ Pool/README.md | 42 +++++++ Pool/SingletonObjectPooler.cs | 103 ++++++++++++++++++ 5 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 Pool/Editor/SingletonObjectPoolerInitializer.cs create mode 100644 Pool/README.md create mode 100644 Pool/SingletonObjectPooler.cs diff --git a/KeyboardInput/KeySettingManager.cs b/KeyboardInput/KeySettingManager.cs index ae27ff0..2ac43b1 100644 --- a/KeyboardInput/KeySettingManager.cs +++ b/KeyboardInput/KeySettingManager.cs @@ -20,7 +20,7 @@ namespace AnnoyingUtils.KeyboardInput if (_instance) return _instance; _instance = FindObjectOfType(); if (_instance) return _instance; - var go = new GameObject("KeySettingManager"); + var go = new GameObject(nameof(KeySettingManager)); _instance = go.AddComponent(); return _instance; diff --git a/KeyboardInput/README.md b/KeyboardInput/README.md index 270c272..6bbb373 100644 --- a/KeyboardInput/README.md +++ b/KeyboardInput/README.md @@ -2,17 +2,20 @@ ```csharp public class KeyMapping - { - public string actionName; - public KeyCode keyCode; +{ + public string actionName; + public KeyCode keyCode; - public KeyMapping(string actionName, KeyCode keyCode) - { - this.actionName = actionName; - this.keyCode = keyCode; - } + public KeyMapping(string actionName, KeyCode keyCode) + { + this.actionName = actionName; + this.keyCode = keyCode; } +} ``` KeySettingManager为单例类 -//todo \ No newline at end of file +在LoadKeySettings()中填写自己需要的键位对即可。 +调用GetKey(string actionName)获取行为对应的KeyCode + +调用SetKey(string actionName, KeyCode newKeyCode)设置行为对应KeyCode \ No newline at end of file diff --git a/Pool/Editor/SingletonObjectPoolerInitializer.cs b/Pool/Editor/SingletonObjectPoolerInitializer.cs new file mode 100644 index 0000000..2a3b905 --- /dev/null +++ b/Pool/Editor/SingletonObjectPoolerInitializer.cs @@ -0,0 +1,29 @@ +using UnityEditor; +using UnityEngine; + +namespace AnnoyingUtils.Pool.Editor +{ + [InitializeOnLoad] + public static class SingletonObjectPoolInitializer + { + static SingletonObjectPoolInitializer() + { + CreateSingletonInstances(); + } + + private static void CreateSingletonInstances() + { + var types = typeof(SingletonObjectPool<>).Assembly.GetTypes(); + foreach (var type in types) + { + if (!type.IsSubclassOf(typeof(MonoBehaviour)) || !type.BaseType.IsGenericType || + type.BaseType.GetGenericTypeDefinition() != typeof(SingletonObjectPool<>)) continue; + var existingInstances = UnityEngine.Object.FindObjectsOfType(type); + if (existingInstances.Length != 0) continue; + var obj = new GameObject(type.Name); + obj.AddComponent(type); + Debug.Log(type.Name + " 单例实力已创建"); + } + } + } +} \ No newline at end of file diff --git a/Pool/README.md b/Pool/README.md new file mode 100644 index 0000000..48ad20e --- /dev/null +++ b/Pool/README.md @@ -0,0 +1,42 @@ +新建一个对象池类继承SingletonObjectPool后,在UnityEditor中会自动创建以类名为名称的单例实例。 +```csharp +public class EnemyPool : SingletonObjectPool +{ +} +``` +用法示例如下方刷怪圈 +```csharp +public class EnemySpawner : MonoBehaviour +{ + public GameObject enemyPrefab; + public int initialPoolSize = 5; + public int maxPoolSize = 10; + public float spawnInterval = 3f; + public int spawnCount = 1; + public float spawnRadius = 5f; + + private void Start() + { + SingletonObjectPool.Instance.Init(enemyPrefab.GetComponent(), initialPoolSize, maxPoolSize); + StartCoroutine(SpawnEnemiesCoroutine()); + } + + private IEnumerator SpawnEnemiesCoroutine() + { + while (true) + { + yield return new WaitForSeconds(spawnInterval); + SpawnEnemies(); + } + } + + private void SpawnEnemies() + { + for (var i = 0; i < spawnCount; i++) + { + var spawnPosition = transform.position + (Vector3)Random.insideUnitCircle * spawnRadius; + SingletonObjectPool.Instance.Spawn(transform).transform.position = spawnPosition; + } + } +} +``` \ No newline at end of file diff --git a/Pool/SingletonObjectPooler.cs b/Pool/SingletonObjectPooler.cs new file mode 100644 index 0000000..d1a8453 --- /dev/null +++ b/Pool/SingletonObjectPooler.cs @@ -0,0 +1,103 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Pool; + +namespace AnnoyingUtils.Pool +{ + public class SingletonObjectPool : MonoBehaviour where T : MonoBehaviour + { + #region 单例 + + private static SingletonObjectPool _instance; + + public static SingletonObjectPool Instance + { + get + { + if (_instance) return _instance; + _instance = FindObjectOfType>(); + if (_instance) return _instance; + var obj = new GameObject(typeof(SingletonObjectPool).Name); + _instance = obj.AddComponent>(); + return _instance; + } + } + + #endregion + + + private ObjectPool _pool; + private T _prefab; + private Queue _activeObjects; + private int _maxActiveObjects; + + public void Init(T prefab, int defaultCapacity = 1, int maxSize = 10) + { + if (prefab is null) + { + Debug.LogError("预制体为空"); + return; + } + + _prefab = prefab; + _maxActiveObjects = maxSize; + _pool = new ObjectPool(CreateFunc, ActionOnGet, ActionOnRelease, ActionOnDestroy, true, defaultCapacity, + maxSize); + _activeObjects = new Queue(); + } + + private T CreateFunc() + { + return Instantiate(_prefab); + } + + private void ActionOnGet(T obj) + { + obj.gameObject.SetActive(true); + _activeObjects.Enqueue(obj); + } + + private void ActionOnRelease(T obj) + { + obj.gameObject.SetActive(false); + } + + private void ActionOnDestroy(T obj) + { + Destroy(obj.gameObject); + } + + //供滴人死亡、子弹命中等调用 + public void Release(T obj) + { + if (_pool is null) + { + Debug.LogWarning(typeof(T).Name + " 对象池未实例化"); + return; + } + + _activeObjects.Dequeue(); + _pool.Release(obj); + } + + public T Spawn(Transform parent) + { + if (_pool is null) + { + Debug.LogWarning(typeof(T).Name + " 对象池未实例化"); + return null; + } + + //检查是否超出容量 + if (_activeObjects.Count >= _maxActiveObjects) + { + var oldestObject = _activeObjects.Peek(); + Release(oldestObject); + } + + var temp = _pool.Get(); + temp.transform.position = parent.position; + return temp; + } + } +} \ No newline at end of file