NRPhoneScreen.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. namespace NRKernal
  2. {
  3. using UnityEngine;
  4. /// <summary> A nr phone screen. </summary>
  5. public class NRPhoneScreen
  6. {
  7. /// <summary> Width of the screen. </summary>
  8. private static float m_ScreenWidth = 0;
  9. /// <summary> Height of the screen. </summary>
  10. private static float m_ScreenHeight = 0;
  11. /// <summary> The default width. </summary>
  12. public const float DefaultWidth = 1080;
  13. /// <summary> The default height. </summary>
  14. public const float DefaultHeight = 2340;
  15. /// <summary> Gets or sets the resolution. </summary>
  16. /// <value> The resolution. </value>
  17. public static Vector2 Resolution
  18. {
  19. get
  20. {
  21. if (m_ScreenWidth < float.Epsilon || m_ScreenHeight < float.Epsilon)
  22. {
  23. #if UNITY_ANDROID && !UNITY_EDITOR
  24. AndroidJavaClass j = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  25. AndroidJavaObject currentActivity = j.GetStatic<AndroidJavaObject>("currentActivity");
  26. AndroidJavaObject displayManager = currentActivity.Call<AndroidJavaObject>("getSystemService", new AndroidJavaObject("java.lang.String", "display"));
  27. AndroidJavaObject display = displayManager.Call<AndroidJavaObject>("getDisplay", 0);
  28. AndroidJavaObject outSize = new AndroidJavaObject("android.graphics.Point");
  29. display.Call("getRealSize", outSize);
  30. m_ScreenWidth = outSize.Get<int>("x");
  31. m_ScreenHeight = outSize.Get<int>("y");
  32. #else
  33. m_ScreenWidth = DefaultWidth;
  34. m_ScreenHeight = DefaultHeight;
  35. #endif
  36. NRDebugger.Debug(string.Format("[NRPhoneScreen] width:{0} height:{1}", m_ScreenWidth, m_ScreenHeight));
  37. }
  38. return new Vector2(m_ScreenWidth, m_ScreenHeight);
  39. }
  40. set
  41. {
  42. m_ScreenWidth = value.x;
  43. m_ScreenHeight = value.y;
  44. }
  45. }
  46. }
  47. }