반응형
Unity에서 ContentSizeFitter가 하위 텍스트의 크기 변경 시 부모 오브젝트의 크기를 즉시 갱신하지 않는 문제를 해결하려면, 레이아웃을 강제로 새로 고치는 코드를 사용할 수 있습니다. 아래는 이를 구현하는 코드 예제입니다.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
public class UpgradeBtnContentSizeFitterRefresh : MonoBehaviour | |
{ | |
private static RectTransform rt; | |
void Start() | |
{ | |
var csf = GetComponent<ContentSizeFitter>(); | |
rt = GetComponent<RectTransform>(); | |
RefreshLayout(); | |
} | |
public static void RefreshLayout() | |
{ | |
LayoutRebuilder.ForceRebuildLayoutImmediate(rt); | |
} | |
} |


위와 같이 설정한 후, 값이 변경되는 곳에서 아래와 같이 호출하여 사용할 수 있습니다
UpgradeBtnContentSizeFitterRefresh.RefreshLayout();
만약 적용해야 할 대상이 한두 개 정도라면 각각 별도의 스크립트를 만들어 간단히 사용하는 것이 좋습니다. 그러나 여러 곳에서 반복적으로 사용해야 한다면, 매니저 클래스를 만들어 중앙에서 관리하는 방식이 효율적입니다.
반응형
'Unity' 카테고리의 다른 글
유니티 UI 오브젝트 위치 이동 시 부모가 다를 때 정확히 위치 맞추는 방법 (0) | 2025.02.04 |
---|---|
Unity에서 AdMob과 GPGS SDK 업데이트 시 빌드 오류 해결 방법 (0) | 2025.02.02 |
[유니티]단일 이미지를 타일링해서 스크롤링하고 싶을 때 (0) | 2024.12.14 |
유니티 안드로이드 빌드 후 스크롤뷰 등 기타 마스크 동작하지 않을 때 When Unity ScrollView or other masks do not work (4) | 2024.09.06 |
유니티 스프라이트 렌더러 마스크, 스프라이트 마스크 Unity Sprite Renderer Mask, Sprite Mask (0) | 2024.08.26 |