public class AppsInTossStartupOptimizer : MonoBehaviour {
[Header("시작 최적화 설정")]
public bool enableFastStartup = true;
public bool preloadCriticalAssets = true;
public int maxConcurrentLoads = 3;
[Header("앱인토스 연동")]
public bool enableTossLogin = true;
public bool preloadTossServices = false;
[Header("메모리 최적화 설정")]
public bool enableStartupGC = true;
public bool unloadUnusedAssets = true;
public bool optimizeTextureMemory = true;
[Header("앱인토스 메모리 제한")]
public long maxMemoryUsageMB = 200; // 앱인토스 권장 제한 (iOS는 100)
void Awake() {
if (enableFastStartup) {
OptimizeStartupSettings();
}
if (enableStartupGC) {
StartCoroutine(OptimizeStartupMemory());
}
//// 메모리 사전 최적화
// 텍스처 스트리밍 설정
QualitySettings.streamingMipmapsActive = true;
QualitySettings.streamingMipmapsMemoryBudget = 128; // MB
// 오디오 설정 최적화
var config = AudioSettings.GetConfiguration();
config.sampleRate = 22050; // 모바일에서는 낮은 샘플레이트
config.speakerMode = AudioSpeakerMode.Stereo;
AudioSettings.Reset(config);
}
void OptimizeStartupSettings() {
// 프레임률 제한 설정 (초기 로딩 시)
Application.targetFrameRate = 30; // 배터리 절약
// Unity 서비스 지연 초기화
StartCoroutine(DelayedUnityServicesInit());
// 메모리 관리 최적화
System.GC.Collect();
Resources.UnloadUnusedAssets();
if (enableTossLogin) {
InitializeTossServices();
}
}
IEnumerator DelayedUnityServicesInit() {
// 첫 프레임 렌더링 후 Unity 서비스 초기화
yield return new WaitForEndOfFrame();
// Analytics, Cloud Build 등 지연 초기화
//InitializeUnityServices();
}
void InitializeTossServices() {
// 토스 로그인 서비스 사전 초기화
StartCoroutine(PreloadTossAuthentication());
}
IEnumerator PreloadTossAuthentication() {
// 토스페이 SDK 사전 로딩
yield return new WaitForSeconds(0.1f);
// AppsInToss 인증 토큰 검증
// AppsInToss.GetCurrentUserToken((token) => {
// if (!string.IsNullOrEmpty(token)) {
// Debug.Log("토스 인증 토큰 사전 로딩 완료");
// // 사용자 프로필 캐시 로딩
// StartCoroutine(CacheUserProfile(token));
// }
// });
}
IEnumerator CacheUserProfile(string token) {
// 사용자 프로필 정보 미리 캐시
// 게임 플레이 중 빠른 접근을 위함
yield return null; // 구현 필요
}
#if UNITY_EDITOR
// Build Settings 최적화
[MenuItem("AIT/Optimize Build Settings")]
static void OptimizeBuildSettings() {
// IL2CPP 설정 최적화
PlayerSettings.SetScriptingBackend(UnityEditor.Build.NamedBuildTarget.WebGL, ScriptingImplementation.IL2CPP);
// 코드 최적화 레벨
PlayerSettings.SetIl2CppCompilerConfiguration(UnityEditor.Build.NamedBuildTarget.WebGL, Il2CppCompilerConfiguration.Release);
// 불필요한 코드 제거
PlayerSettings.stripEngineCode = true;
PlayerSettings.SetManagedStrippingLevel(UnityEditor.Build.NamedBuildTarget.WebGL, ManagedStrippingLevel.High);
// 압축 설정
PlayerSettings.WebGL.compressionFormat = WebGLCompressionFormat.Brotli;
}
#endif
IEnumerator OptimizeStartupMemory() {
Debug.Log("시작 시 메모리 최적화 시작");
// 1. 가비지 컬렉션 실행
if (enableStartupGC) {
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
yield return new WaitForSeconds(0.1f);
}
// 2. 사용하지 않는 에셋 언로드
if (unloadUnusedAssets) {
var unloadOp = Resources.UnloadUnusedAssets();
yield return unloadOp;
}
// 3. 텍스처 메모리 최적화
if (optimizeTextureMemory) {
OptimizeTextureSettings();
}
// 4. 메모리 사용량 체크
CheckMemoryUsage();
Debug.Log("시작 시 메모리 최적화 완료");
}
void OptimizeTextureSettings() {
// 모바일 환경에 맞는 텍스처 설정 적용
QualitySettings.globalTextureMipmapLimit = 1; // 0:전체 해상도, 1:절반 해상도, 2:반의반해상도
QualitySettings.anisotropicFiltering = AnisotropicFiltering.Disable;
Debug.Log("텍스처 메모리 최적화 적용");
}
void CheckMemoryUsage() {
long currentMemoryMB = UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / (1024 * 1024);
Debug.Log($"현재 메모리 사용량: {currentMemoryMB}MB");
if (currentMemoryMB > maxMemoryUsageMB) {
Debug.LogWarning($"메모리 사용량이 권장 제한을 초과: {currentMemoryMB}MB > {maxMemoryUsageMB}MB");
// 앱인토스에 메모리 경고 리포트
//AppsInToss.ReportPerformanceIssue("high_memory_startup", currentMemoryMB);
// 추가 메모리 정리 시도
StartCoroutine(AdditionalMemoryCleanup());
}
}
IEnumerator AdditionalMemoryCleanup() {
// 더 적극적인 메모리 정리
Resources.UnloadUnusedAssets();
System.GC.Collect();
yield return new WaitForSeconds(0.5f);
// 정리 후 재측정
long newMemoryMB = UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / (1024 * 1024);
Debug.Log($"메모리 정리 후 사용량: {newMemoryMB}MB");
}
}
}