Skip to content

Instantly share code, notes, and snippets.

@IJEMIN
Last active January 2, 2023 21:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save IJEMIN/66a038ebb58f5e9f61c24e5244418289 to your computer and use it in GitHub Desktop.
Save IJEMIN/66a038ebb58f5e9f61c24e5244418289 to your computer and use it in GitHub Desktop.
유니티에서 안드로이드의 DP, iOS의 포인트 해상도를 사용하기
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
}
}
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
}
+(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