Replies: 4 comments 3 replies
-
|
小4 ( 程式 ) 答案public class Pool<T> : IPool<T>
{
protected const int _batchSpawnCount = 32;
// 產生物件的工廠
protected IFactory<T> _factory { get; }
// 儲存物件的空間
protected List<T> _storage { get; }
// 目前儲存的物件數
public int Count => _storage.Count;
public Pool(IFactory<T> factory)
{
_factory = factory;
_storage = new List<T>();
}
/// <summary>
/// 主動呼叫並建立物件
/// </summary>
/// <param name="count">建立的數量</param>
public void Ready(int count)
{
for (int i = 0; i < count; i++)
{
var item = _factory.Spawn();
_storage.Add(item);
}
}
public void Push(T item)
{
if (item == null)
{
throw new ArgumentNullException("item == null");
}
if (_storage.Contains(item))
{
return;
}
_storage.Add(item);
}
public T Pop()
{
if (_storage.Count < 1)
{
Ready(_batchSpawnCount);
}
var item = _storage[0];
_storage.RemoveAt(0);
return item;
}
} public class Bullet
{
public int Id { get; }
public Bullet(int id)
{
Id = id;
}
} public class BulletFactory : IFactory<Bullet>
{
private int uniqueID;
public BulletFactory()
{
uniqueID = 0;
}
public Bullet Spawn()
{
uniqueID++;
return new Bullet(uniqueID);
}
} public static class PoolExtension
{
public static T[] MultiPop<T>(this IPool<T> pool, int count)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (count < 0)
throw new ArgumentException("count must be greater than zero");
var items = new T[count];
for (int i = 0; i < items.Length; i++)
{
items[i] = pool.Pop();
}
return items;
}
public static void MultiPush<T>(this IPool<T> pool, params T[] items)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (items == null)
return;
for (int i = 0; i < items.Length; i++)
{
pool.Push(items[i]);
}
}
}反饋好難啊 我甚至全部丟給ChatGPT都沒得到答案 |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
JIA(程式新手) 我的答案using System;
using System.Collections.Generic;
using UnityEngine;
namespace PG0007.Questions
{
public class Question1 : MonoBehaviour
{
public class Pool<T> : IPool<T>
{
protected const int _batchSpawnCount = 32;
protected IFactory<T> _factory { get; }
protected List<T> _storage { get; }
public int Count { get { return _storage.Count; } }
public Pool(IFactory<T> factory)
{
_factory = factory;
_storage = new List<T>();
}
public void Ready(int count)
{
for (int i = 0; i < count; i++)
{
var item = _factory.Spawn();
_storage.Add(item);
}
}
public void Push(T item)
{
if (item == null) return;
if (_storage.Contains(item)) return;
_storage.Add(item);
}
public T Pop()
{
if (_storage.Count <= 0)
Ready(32);
int lastIndex = _storage.Count - 1;
var item = _storage[lastIndex];
_storage.RemoveAt(lastIndex);
return item;
}
}
public class Bullet
{
public int Id { get; }
public Bullet(int id)
{
Id = id;
}
}
public class BulletFactory : IFactory<Bullet>
{
private Pool<Bullet> bulletPool;
public BulletFactory()
{
bulletPool = new Pool<Bullet>(this);
}
public Bullet Spawn()
{
int id = bulletPool.Count + 1;
Bullet bullet = new Bullet(id);
bulletPool.Push(bullet);
return bullet;
}
}
}
public static class PoolExtension
{
public static T[] MultiPop<T>(this IPool<T> pool, int count)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (count < 0)
throw new ArgumentException("count must be greater than zero");
T[] items = new T[count];
for (int i = 0; i < count; i++)
items[i] = pool.Pop();
return items;
}
public static void MultiPush<T>(this IPool<T> pool, params T[] items)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (items == null)
return;
int itemCount = items.Length;
for (int i = 0; i < itemCount; i++)
{
pool.Push(items[i]);
}
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
老蕭OLDShaw(程式) 我的答案 public class Question1 : MonoBehaviour
{
public class Pool<T> : IPool<T>
{
protected const int _batchSpawnCount = 32;
/// <summary>
/// イ」・ヘェォ・コ、uシt
/// </summary>
protected IFactory<T> _factory { get; }
/// <summary>
/// タxヲsェォ・コェナカ。
/// </summary>
protected List<T> _storage { get; }
/// <summary>
/// ・リォeタxヲsェコェォ・ニ
/// </summary>
public int Count { get { return _storage.Count; } }
#region -- Constructors --
public Pool(IFactory<T> factory)
{
_factory = factory;
_storage = new List<T>();
}
#endregion
/// <summary>
/// ・DーハゥI・sィテォリ・゚ェォ・・
/// </summary>
/// <param name="count">ォリ・゚ェコシニカq</param>
public void Ready(int count)
{
for (int i = 0; i < count; i++)
{
_storage.Add(_factory.Spawn());
}
}
public void Push(T item)
{
if (_storage.Contains(item)) return;
_storage.Add(item);
}
public T Pop()
{
if( _storage.Count == 0)
{
Ready(_batchSpawnCount);
}
T item = _storage[_storage.Count - 1];
_storage.RemoveAt(_storage.Count - 1);
return item;
}
}
/// <summary>
/// A bullet object
/// </summary>
public class Bullet
{
public int Id { get; }
public Bullet(int id)
{
Id = id;
}
}
public class BulletFactory : IFactory<Bullet>
{
int _uniqueId;
#region -- Constructors --
public BulletFactory()
{
_uniqueId = 0;
}
#endregion
public Bullet Spawn()
{
return new Bullet(_uniqueId++);
}
}
}
public static class PoolExtension
{
public static T[] MultiPop<T>(this IPool<T> pool, int count)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (count < 0)
throw new ArgumentException("count must be greater than zero");
T[] result = new T[count];
for (int i = 0; i < count; i++)
{
result[i] = pool.Pop();
}
return result;
}
public static void MultiPush<T>(this IPool<T> pool, params T[] items)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (items == null)
return;
foreach (var item in items)
{
pool.Push(item);
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
Snoweve (程式) 我的答案using System;
using System.Collections.Generic;
using UnityEngine;
using static UnityEditor.Progress;
namespace PG0007.Questions
{
public class Question1 : MonoBehaviour
{
public class Pool<T> : IPool<T>
{
protected const int _batchSpawnCount = 32;
/// <summary>
/// 產生物件的工廠
/// </summary>
protected IFactory<T> _factory { get; }
/// <summary>
/// 儲存物件的空間
/// </summary>
protected List<T> _storage { get; }
/// <summary>
/// 目前儲存的物件數
/// </summary>
public int Count { get { return _storage.Count; } }
#region -- Constructors --
public Pool(IFactory<T> factory)
{
_factory = factory;
_storage = new List<T>();
}
#endregion
/// <summary>
/// 主動呼叫並建立物件
/// </summary>
/// <param name="count">建立的數量</param>
public void Ready(int count)
{
for (int i = 0; i < count; i++)
{
var item = _factory.Spawn();
_storage.Add(item);
}
}
public void Push(T item)
{
if (item == null)
return;
if (_storage.Contains(item))
return;
_storage.Add(item);
}
public T Pop()
{
if (_storage.Count < 1)
{
Ready(_batchSpawnCount);
}
T item = _storage[_storage.Count - 1];
_storage.RemoveAt(_storage.Count - 1);
return item;
}
}
/// <summary>
/// A bullet object
/// </summary>
public class Bullet
{
public int Id { get; }
public Bullet(int id)
{
Id = id;
}
}
public class BulletFactory : IFactory<Bullet>
{
private int _distinctId;
#region -- Constructors --
public BulletFactory()
{
_distinctId = 0;
}
#endregion
public Bullet Spawn()
{
return new Bullet(_distinctId++);
}
}
}
public static class PoolExtension
{
public static T[] MultiPop<T>(this IPool<T> pool, int count)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (count < 0)
throw new ArgumentException("count must be greater than zero");
var items = new T[count];
for (int i = 0; i < count; i++)
{
var item = pool.Pop();
items[i] = item;
}
return items;
}
public static void MultiPush<T>(this IPool<T> pool, params T[] items)
{
if (pool == null)
throw new ArgumentNullException("pool is null");
if (items == null)
return;
foreach (var item in items)
{
pool.Push(item);
}
}
}
}
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment




Uh oh!
There was an error while loading. Please reload this page.
-
Introduction
Background
川口哲也是一名遊戲設計師,他發想了一款彈幕射擊遊戲,原本程式的設計會讓子彈在每次發射時建立一個物件,但開發到一半時,他發現每次到子彈特別多的情況下,系統的幀數就會明顯的下降,在大量的建立、清除物件情況下,會消耗大量的系統資源,作為他的好同事,你提出了一個方案,利用物件池儲存應該被清除的子彈,當再次發射時,從物件池取出回收的子彈,可以減少物件大量建立、清除的情形...
Conditions
Answer
解答
Beta Was this translation helpful? Give feedback.
All reactions