Last active
January 2, 2023 21:41
-
-
Save IJEMIN/66a038ebb58f5e9f61c24e5244418289 to your computer and use it in GitHub Desktop.
유니티에서 안드로이드의 DP, iOS의 포인트 해상도를 사용하기
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 CanvasSizeToMobilePointSize : MonoBehaviour | |
{ | |
private CanvasScaler _canvasScaler; | |
private void Awake() | |
{ | |
_canvasScaler = GetComponent<CanvasScaler>(); | |
Resize(); | |
} | |
private void Resize() | |
{ | |
if (Application.isEditor) | |
{ | |
return; | |
} | |
_canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize; | |
#if UNITY_ANDROID | |
_canvasScaler.scaleFactor = Screen.dpi / 160; | |
#elif UNITY_IOS | |
_canvasScaler.scaleFactor = MyApplePlugin.GetNativeScaleFactor(); | |
#endif | |
} | |
} |
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 System.Runtime.InteropServices; | |
using UnityEngine; | |
public static class MyApplePlugin | |
{ | |
#if UNITY_IOS | |
[DllImport("__Internal")] | |
private static extern float _GetNativeScaleFactor(); | |
public static float GetNativeScaleFactor() | |
{ | |
return _GetNativeScaleFactor(); | |
} | |
#endif | |
} |
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
+(float)getNativeScaleFactor | |
{ | |
UIScreen* uiScreen = [UIScreen mainScreen]; | |
return uiScreen.nativeScale; | |
} | |
@end | |
extern "C" | |
{ | |
float _GetNativeScaleFactor() { | |
return [MyApplePlugin getNativeScaleFactor]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment