250422 TIL

박소희·2025년 4월 22일

Unity_7기

목록 보기
74/94

타임라인 내 블럭 삭제, 좌측 정렬

   public void DestroyBlock(Block block)
   {
       if (block.IsActive)
       {
           int index = PlacedBlocks.IndexOf(block);
           if (index >= 0)
           {
               block.IsActive = false;
               RemoveAndShiftLeft(index);
               
           }
       }
   }
   public void RemoveAndShiftLeft(int removeIndex)
{
    if (removeIndex < 0 || removeIndex >= PlacedBlocks.Count) return;

    // 먼저 삭제할 GameObject를 파괴한다
    if (slots[removeIndex].currentItem != null)
    {
        Destroy(slots[removeIndex].currentItem.gameObject);
        slots[removeIndex].currentItem = null;
    }
    PlacedBlocks.RemoveAt(removeIndex);

    for(int i = removeIndex; i < slots.Count -1; i++)
    {
        slots[i].currentItem = slots[i + 1].currentItem;
        if (slots[i].currentItem != null)
        {
            slots[i].currentItem.transform.SetParent(slots[i].transform);
            slots[i].currentItem.transform.localPosition = Vector3.zero;
        }
    }

    if(slots.Count > 0)
    {
        int lastIndex = PlacedBlocks.Count;
        if (lastIndex < slots.Count) slots[lastIndex].currentItem = null;
    }
    index = PlacedBlocks.Count;
}

고스트 생성 시 유니티가 멈추는 문제가 생겼다.

0개의 댓글