velog-client icon indicating copy to clipboard operation
velog-client copied to clipboard

자동 비공개 됩니다... 어디를 고쳐야 하나요?

Open FixerSan opened this issue 1 year ago • 1 comments

클래스 설명

Class PoolManager

  • Pool을 동적으로 추가, 삭제등 관리하기 편하기 위함이다.

먼저 Managers 스크립트에 PoolManager를 추가한다. (추가하는 방법은 Link :: System - Managers 페이지 참조)






로직설명

사용할 Pool

public class Pool
{
    private GameObject prefab;
    private Queue<GameObject> queue_PoolObject;
    private Transform transform_Pool;
    private string poolName;

    public Pool(GameObject _prefab, string _poolName)
    {
        prefab = _prefab;
        poolName = _poolName;
        queue_PoolObject = new Queue<GameObject>();
        Init();
    }

    private void Init()
    {
        GameObject go = GameObject.Find("@Pool");
        if(go == null)
        {
            go = new GameObject{ name = "@Pool" };
        }
        GameObject _transform_Pool = new GameObject { name = poolName };
        _transform_Pool.transform.SetParent(go.transform);
        transform_Pool = _transform_Pool.transform;
    }

    public GameObject Get()
    {
        GameObject poolObject;
        if (queue_PoolObject.TryDequeue(out GameObject _poolObject))
        {
            poolObject = _poolObject;
        }

        else
        {
            poolObject = GameObject.Instantiate(prefab);
            poolObject.name = prefab.name;
        }

        poolObject.SetActive(true);
        return poolObject;
    }

    public void Push(GameObject _poolObject)
    {
        _poolObject.transform.SetParent(transform_Pool);
        _poolObject.SetActive(false);
        queue_PoolObject.Enqueue(_poolObject);
    }

    public void Clear()
    {
        queue_PoolObject.Clear();
        Managers.Resource.Destroy(transform_Pool.gameObject);
    }
}






Pool 생성

풀 생성 코드 :: Managers.Pool.CreatePool(string, GameOject)
- string 자리의 pool의 이름이 될 값을, GameObject 자리에는 pool이 인스턴스 해줄 프리팹의 값을 넣어준다.
public void CreatePool(GameObject _prefab, System.Action _callback = null)
{
    string key = _prefab.name;
    if (dictionary_Pool.ContainsKey(key))
        return;
    Pool pool = new Pool(_prefab, $"{key} Pool");
    dictionary_Pool.Add(key, pool);
    _callback?.Invoke();
}
  • CreatePool 함수 호출

  • Dictionary에 이미 같은 이름의 Pool이 있는지 체크 후 있다면 리턴

  • new Pool 생성

  • Pool에 이름을 프리팹의 Name, 프리팹을 GameObject로 초기화

  • Pool을 PoolManager Dictionary의 Add

  • 생성 콜백






Pool 삭제

풀 삭제 코드 :: Managers.Pool.DeletePool(string)
- string 자리의 pool의 이름을 넣어준다.
public void DeletePool(string _key)
{
    if(dictionary_Pool.ContainsKey(_key))
    {
        dictionary_Pool[_key].Clear();
        dictionary_Pool.Remove(_key);
    }
}
  • Delete 함수 호출

  • Dictionary에 Pool이 있는지 체크

  • 있다면 Pool의 Clear 함수 호출

  • Clear 종료 후 dictionary 삭제






Pooling

풀링 코드 :: Managers.Pool.Get(string)
- string 자리의 오브젝트를 풀링시킬 pool의 이름값을 적어준다.
public GameObject Get(string _key)
{
    if (dictionary_Pool.TryGetValue(_key, out Pool pool))
    {
        return pool.Get();
    }
    return null;
}
  • Get 함수 호출

  • dictionary에 이미 같은 이름의 Pool이 있는지 체크

  • 있으면 그 Pool의 Get함수 실행 후 리턴 된 GameObject 값 반환






Push

푸쉬 코드 :: Managers.Pool.Push(GameObject)
public bool Push(GameObject _go)
{
    if(dictionary_Pool.ContainsKey(_go.name))
    {
        dictionary_Pool[_go.name].Push(_go);
        return true;
    }
    return false;
}
  • Push 함수 호출

  • dictionary에 Pool이 있는지 체크

  • 있다면 그 Pool에 오브젝트를 넣어주고 True 반환

  • 없다면 false 반환






시도

Create를 하지 않고 Get이나 Push를 하면 자동으로 Create 함수를 호출

  • Create에 필요한 프리팹 오브젝트를 어드레서블로 로드하려니 비동기 로드다 보니 프리팹이 로드 되기 전 프리팹 변수가 Create로 들어가져서 실패

  • 동기로 호출하자니 도중에 게임이 멈추는 현상이 일어날 것 같아서 포기

FixerSan avatar Aug 03 '23 03:08 FixerSan

불편을 드려서 죄송합니다.

/@{username} 아이디를 알려주시고 해당 글의 title을 알려주시면 신속하게 처리해드리겠습니다.

감사합니다 :)

winverse avatar Feb 04 '24 22:02 winverse