์๋
ํ์ธ์
์ ๋ํฐ Jobsystem , Burst ์ด์ฉํด์ ์์ฒด ์ถฉ๋์ฒด ๋ง๋๋๊ฑธ ๋ชฉํ๋ก ๊ธ์ ํ๋ฒ ์จ๋ณผ๋ ค๊ณ ํฉ๋๋ค.
์ด๋ฒ์ ๊ฒ์ ๋ง๋ค๋ฉด์ ์์ฒด ์ถฉ๋์ฒด๊ฐ ํ์ํ์ต๋๋ค. ใ
ใ
๋๊ตฐ๊ฐ์๊ฒ ๋์์ด ๋๊ธธ ๋ฐ๋๋๋ค. ์ ๋ ์ค๋ ฅ์ด ๋ถ์กฑํ์ง๋ผ.. ๋ง ์ฝ๋ ์ฑ๋ฅ์ด ์์ฒญ ์ข๋ค ์ด๋ฐ๊ฑด ์ฅ๋ด๋ชปํฉ๋๋ค.. ใ
ใ
์ผ๋จ ์ฑ๊ธํค์ผ๋ก ์คํฌ๋ฆฝํธ ํ๋ ๋ง๋ค์ด ์ค์๋ค ์ ๋ CollisionSystemV2 ์ผ๋ก ์ด๋ฆ์ ์ ํ์ต๋๋ค.
public class CollisionSystemV2 : MonoBehaviour
{
public static CollisionSystemV2 ins#tance; #์ ์ง์ฐ๊ณ ์ฐ์ธ์ ๊ธ์ง์ด๋๋ฌธ์
private void Awake()
{
if(ins#tance != null)
{
if(ins#tance != this)
{
Destroy(ins#tance);
}
}
ins#tance = this;
}
}
๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ์ ์ถฉ๋์ฒด๊ฐ ๋ ํด๋์ค ํ๋ ๋ ๋ง๋ค์ด์ค๊ฒ์
public class NativeCollider
{
public int id; //์ถฉ๋์ฒด์ ID
public object entity; //์ถฉ๋์ฒด์ ๋ฌผ์ฒด
public bool isCircle; //์ถฉ๋์ฒด๊ฐ ์์ธ๊ฐ?
public bool isDynamic; //๊ณ์ ์์ง์ด๋ ์ถฉ๋์ฒด์ธ๊ฐ
public Vector2 position; //์ถฉ๋์ฒด์ ์์น
public Vector2 size; //์ถฉ๋์ฒด์ ์ฌ์ด์ฆ
}
์ด๋ฐ์์ผ๋ก ๊ตฌ์กฐ๋ฅผ ์ง์คฌ์ต๋๋ค.
์ด์ ์ด ์ถฉ๋์ฒด๋ค์ ๋ด์๋์ผ, ์ฐ์ฐ์ ํ๋ ๋ญ๋ ํ ํ
๋
์ฐ๋ฆฌ ์๊น ๋ง๋ ์ฑ๊ธํค ํด๋์ค์๋ค๊ฐ ๋ด์ ๋ณ์๋ฅผ ๋ง๋ค์ด์ค์๋ค.
public List<NativeCollider> entityList;
์ด์ ์ฌ๊ธฐ๋ค๊ฐ ์ถฉ๋์ฒด๋ค์ ๋ด์๋์๊ฑฐ์์. ์ ๋ ์ถฉ๋์ฒด ID๊ธฐ์ค์ List์ ๋ค์ด๊ฐ Index๋ก ์ก๊ณ ์ถ์ต๋๋ค.
๊ทธ๋ฌ๊ธฐ์ํด์ FreeList์ ์ด์ฉํ ๊ฒ๋๋ค. (์ด๊ฒ ๋ช
์นญ์ด ๋ง๋์ง๋ ๋ชจ๋ฅด๊ฒ ์ด์ , ์ธ๊ตญ์ฌ์ดํธ์์ ์ฐพ์๊ฑฐ๋ผ์ ใ
ใ
)
์ ๋ FreeList๋ผ๊ณ ๋ถ๋ฅผ๊ฒ์
FreeList๋
C# List์ฌ์ฉ์ ์์๋ฅผ ์ ๊ฑฐํ๋ฉด ๊ธธ์ด๊ฐ ๋ฌ๋ผ์ง์์์.
์์๋ฅผ ์ ๊ฑฐํ์ง์๊ณ ๊ทธ ์์ Index๋ถ๋ถ๋ง null๋ก ๋ฐ๊ฟ์ index์์น๊ฐ๋ง ๋ค๋ฅธ๊ณณ์ ์ ์ฅํด๋๊ณ ๋ค์์ ๋ค๋ฅธ ์์๊ฐ ์ ๊ทผํ ๋ ์ ์ฅ๋ index์ ์ฌ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
์ด๋ ๊ฒํ๋ฉด id๋ถ๋ถ์ด ๋ณด์ฅ์ด๋ฉ๋๋ค. ์ ๊ฐ ์ค๋ช
์ ์๋ชปํด์ ใ
ใ
์ดํด๋ชปํ์
จ์ผ๋ฉด ์ฝ๋๋ก ๋ด๋ฐ์
private void AddEntityList(NativeCollider entity)
{
int index = 0;
if(entityFreeList.Count > 0)
{
index = entityFreeList[entityFreeList.Count - 1];
entityFreeList.RemoveAtSwapBack(entityFreeList.Count - 1);
entityList[index] = entity;
}
else
{
entityList.Add(entity);
index = entityList.Count - 1;
}
entity.id = index;
}
private void RemoveEntityList(NativeCollider entity)
{
entityList[entity.id] = null;
entityFreeList.Add(entity.id);
}
์ด๋ฐ์์ผ๋ก ์ฑ๊ธํคํด๋์ค์ ํจ์๋ฅผ ๋ง๋ค์ด์คฌ์ต๋๋ค.
public List<int> entityFreeList; ๋ณ์๋ ์ถ๊ฐํด์คฌ์ต๋๋ค.
์ฌ๊ธฐ์ ์ถ๊ฐ๋ entityFreeList๋ entityList ๋ณ์์์ ๋์จ ๋น index์ ์ ์ฅํ๋๊ณณ ์
๋๋ค.
์ฝ๋๋ฅผ ๋ณด์๋ฉด ๋์ถฉ ์ดํด๊ฐ ๋๋์??
์ด์ ๋ถํฐ๋ ์๋ก์ด ์ถฉ๋์ฒด๋ฅผ ์ถ๊ฐํ ๋๋ ๋ฐ๋ก ๋ฆฌ์คํธ์๋ค๊ฐ ์ถ๊ฐํ์ง๋ง๊ณ ์์ ์ฝ๋์ฒ๋ผ ์ถ๊ฐ,์ญ์ ํจ์๋ฅผ ์ด์ฉํด์ ํด์ผํฉ๋๋ค.
์ ์ด์ด์ ์ด์ JobSystem์ ์ด์ฉํ๊ธฐ ์ํด์ ๊ตฌ์กฐ์ฒด๋ฅผ ํ๋ ๋ง๋ค์ด์ค์๋ค.
์คํ์ ์ ์ฅ๋๋ ํ์์ผ๋ก ํ๊ธฐ ์ํด์ ์
๋๋ค.
(์ก์์คํ
์ ์ด์ฉํ ๋ ค๋ฉด ๊ฐํ์
์ด ํ์ํฉ๋๋ค!)
public struct ColliderBody
{
public int id;
public bool isCircle;
public float2 position;
public float2 size;
public ColliderBody(int id, bool isCircle,float2 position, float2 size)
{
this.id = id;
this.isCircle = isCircle;
this.position = position;
this.size = size;
}
}
์ ์ด์ ์ด ๊ตฌ์กฐ์ฒด๋ ์ฐ๋ฆฌ ์๊น ๋ง๋ ์ถฉ๋์ฒด ํด๋์ค์ ์ฐ์ฐ์ ์ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๊ตฌ์กฐ์ฒด์
๋๋ค.
์ด์ ์ฑ๊ธํค ํด๋์ค์ ์ด ๊ตฌ์กฐ์ฒด๋ฅผ ๋ด์ ๋ณ์๋ฅผ ํ๋ ๋ง๋ค์ด์ค์๋ค.
public NativeAr#ray<ColliderBody> bodys;
NativeAr#ray๋ Persistent๋ก ์ ์ธํด์คํด์ผํด์!! ๊ณ์ ์ธ๊ฑฐ์
๋๋ค.
ํน์ ๋ชจ๋ฅด๋๊น ์คํฌ๋ฆฝํธ ๊บผ์ง๋ Dispose๋ ํด์ฃผ์ธ์
์ด์ ์ถฉ๋์ฒด ํด๋์ค์ ColliderBody์ ํธํ๊ฒ ๊ฐ์ ธ์ค๊ธฐ ์ํ ํจ์๋ฅผ ํ๋ ๋ง๋ค์ด์ค์๋ค.
public ColliderBody GetBody()
{
return new ColliderBody(id, isCircle, position, size);
}
๊ทธ๋ฆฌ๊ณ ์๊น ์ฑ๊ธํคํด๋์ค์์ ๋ง๋ ์ถฉ๋์ฒด ์ถ๊ฐ, ์ ๊ฑฐ ํจ์๋ถ๋ถ์ ์์ ํด์ ColliderBody ๋ด๊ณ ์๋ bodys๋ณ์ ์๋ค๊ฐ
์ถ๊ฐ๋๊ฒ๋ ๋ง๋ค์ด์ค์๋ค.
bodys[index] = entity.GetBody();
private void AddEntityList(NativeCollider entity)
{
int index = 0;
if(entityFreeList.Count > 0)
{
index = entityFreeList[entityFreeList.Count - 1];
entityFreeList.RemoveAtSwapBack(entityFreeList.Count - 1);
entityList[index] = entity;
}
else
{
entityList.Add(entity);
index = entityList.Count - 1;
}
entity.id = index;
bodys[index] = entity.GetBody();
}
์ด๋ ๊ฒ ํ๋ฉด ์ด์ ๊ธฐ๋ณธ๊ตฌ์กฐ๋ ๋์
๋๋ค.
๋ค์๊ธ์์ ์ด์ ์ด ๊ตฌ์กฐ๋ฅผ ์ด์ฉํด์ ๋ณ๋ ฌ ์ถฉ๋ ์ฐ์ฐ ํด๋ด
์๋ค! ๊ทธ ์ดํ์ ๋ ์ด์ด๋ง์คํฌ, OnEnter,OnExit,OnStay ์ด๋ฒคํธ๋ฐ๋ ๊น์ง ๊ตฌํํด๋ณผ๊ฒ์.
๊ฐ์ฌํฉ๋๋ค.
์ผ์ง๋ ๊ฐ์ถ
3์ฃผ ํ์ ์์ ์ ๋ฏฟ๋๋ค๋ฉด ๋ชจ๋ฅด๊ฒ ์ง๋ง ์ถ๊ฐ,์ ๊ฑฐ ํจ์๊น์ง ๋ง๋ค์ด ๋์๊ฑฐ๋ผ๋ฉด ๋ฆฌ์คํธ๋ private์ผ๋ก ์จ๊ฒจ๋๊ณ ์ฝ๊ธฐ์ ์ฉ์ผ๋ก ์ ๊ทผํ๊ฒ get ํ๋กํผํฐ๋ readonly ๋ฆฌ์คํธ๋ฅผ ๋ฆฌํดํ๋ get ํจ์๋ฅผ ๋ฐ๋ก ๋ง๋๋๊ฒ ์์ ํ ๊ฑฐ์. ์ฑ๊ธํด์ด๋ผ๋ฉด ๋์ฑ๋ ๋ด์ฉ์ ๊ฐ์ถ
Burst ๋ List ๋ชป์ฐ์ง์๋์?
๊ทธ๋์ NativeAr1ray์๋ ์ ์ฅํด์ค์