[iOS] 유니티 숏치러 갑니다 - 2

유인호·2일 전
1

iOS

목록 보기
81/81

0. 서론

아이폰을 미러링한 상태에서 프로젝트를 키면 Unity as a library에서 화면을 미러링 중인 화면을 잡고 그 화면을 기준으로 앱을 실행하는 현상이 있다.

이때 앱을 키자마자 램을 2-3기가씩 먹는 현상이 있었고, 화면의 크기, 화질에 따라 아마 램을 더 잡아먹을 수 있을 터, 솔직히 나는 이거 해결 못하는 문제인줄 알았음 ㅇㅇ

어쩔 수 없이 옵젝씨로 이루어진 Unity가 만든 사이버 배설물을 까볼 수 밖에 없었다.

1. 문제의 코드

코드를 뒤적거리다 문제의 코드를 발견할 수 있었다.

- (UIWindowScene*)pickStartupWindowScene:(NSSet<UIScene*>*)scenes
{
    // if we have scene with UISceneActivationStateForegroundActive - pick it
    // otherwise UISceneActivationStateForegroundInactive will work
    //   it will be the scene going into active state
    // if there were no active/inactive scenes (only background) we should allow background scene
    //   this might happen in some cases with native plugins doing "things"
    UIWindowScene *foregroundScene = nil, *backgroundScene = nil;
    for (UIScene* scene in scenes)
    {
        if (![scene isKindOfClass: [UIWindowScene class]])
            continue;
        UIWindowScene* windowScene = (UIWindowScene*)scene;

        if (scene.activationState == UISceneActivationStateForegroundActive)
            return windowScene;
        if (scene.activationState == UISceneActivationStateForegroundInactive)
            foregroundScene = windowScene;
        else if (scene.activationState == UISceneActivationStateBackground)
            backgroundScene = windowScene;
    }

    return foregroundScene ? foregroundScene : backgroundScene;
}

이 녀석의 문제는 나는 알고있다. [iOS] 대표님.. 정말 잘 되었다니까요... (feat. Airplay & SceneDelegate)이 게시물에 해답이 적혀 있는데, 간략하게 설명하자면,

미러링을 하면 UIWindowScene이 두개 만들어진다. 그리고 그것을 담는 자료형은 Set이다.

즉, for문으로 scenes를 돌린다면, 어떤 화면이 먼저 나올지 랜덤이다.

UISceneActivationStateForegroundActive의 WindowScene은 두개기 때문에, 아이폰화면이 될지, 미러링 중인 화면이 될지 모른다.

2. 해결

UnityFramework는 로컬 프레임워크기 때문에 내가 수정이 가능하다. 즉,

- (UIWindowScene*)pickStartupWindowScene:(NSSet<UIScene*>*)scenes {
    // if we have scene with UISceneActivationStateForegroundActive - pick it
    // otherwise UISceneActivationStateForegroundInactive will work
    // if no active/inactive scenes (only background) - pick the background scene

    UIWindowScene *foregroundScene = nil, *backgroundScene = nil;

    for (UIScene* scene in scenes) {
        if (![scene isKindOfClass: [UIWindowScene class]])
            continue;

        UIWindowScene* windowScene = (UIWindowScene*)scene;

		// 추가
        if (windowScene.session.role != UIWindowSceneSessionRoleApplication) {
            continue;
        }

        if (scene.activationState == UISceneActivationStateForegroundActive)
            return windowScene;

        if (scene.activationState == UISceneActivationStateForegroundInactive)
            foregroundScene = windowScene;

        else if (scene.activationState == UISceneActivationStateBackground)
            backgroundScene = windowScene;
    }

    return foregroundScene ? foregroundScene : backgroundScene;
}

요롬코론 만들게 되면,

UIWindowSceneSessionRoleApplication는 단 하나밖에 없기 때문에 Set로 조져진 UIWindowScene에도 문제가 없어질 터.

잘 해결 된 걸 볼 수 있었다.

솔직히 나나 다른 iOS개발자나 미러링하면 UIWindowScene이 2개 되는거 모르는사람이 더 많을듯 ㄹㅇㅋㅋ

profile
🍎Apple Developer Academy @ POSTECH 2nd, 🌱SeSAC iOS 4th

1개의 댓글

이제 알았따

답글 달기

관련 채용 정보