1. 程式人生 > >unity中UI的屏幕自適應代碼

unity中UI的屏幕自適應代碼

ans () adp scale idt screen ont 制作 void

public void ScreenUISelfAdptation(Transform scaleUI)
{
  float widthrate = UnityEngine.Screen.width / 1920.0f;
  float heightrate = UnityEngine.Screen.height / 1080.0f;
  float postion_x = scaleUI.GetComponent<RectTransform>().anchoredPosition.x * widthrate;
  float postion_y = scaleUI.GetComponent<RectTransform>().anchoredPosition.y * heightrate;

  scaleUI.localScale = new Vector3(widthrate, heightrate, 1);

  scaleUI.GetComponent<RectTransform>().anchoredPosition = new Vector2(postion_x, postion_y);
}

UnityEngine.Screen.widthUnityEngine.Screen.height是你要發布的屏幕分辨率大小。

1920.0f; x 1080.0f;是你制作UI時Gam e視圖中的分辨率大小。

unity中UI的屏幕自適應代碼