mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
[=]SafeTop ImageBackGround
[=]SafeTop ImageBackGround
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// 背景图片等比拉伸。
|
||||
/// </summary>
|
||||
public class ImageBackGroundStretch : MonoBehaviour
|
||||
{
|
||||
public float standardAspectValue = 9 / 16f;
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
DoImageStretch(standardAspectValue);
|
||||
}
|
||||
|
||||
private void DoImageStretch(float standardAspect)
|
||||
{
|
||||
float deviceAspect = Screen.width / (float)Screen.height;
|
||||
if (standardAspect > deviceAspect)
|
||||
{
|
||||
float scale = standardAspect / deviceAspect;
|
||||
transform.localScale = new Vector3(scale, scale, 1f);
|
||||
}
|
||||
else if (standardAspect < deviceAspect)
|
||||
{
|
||||
float scale = deviceAspect / standardAspect;
|
||||
transform.localScale = new Vector3(scale, scale, 1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4e6e63472f81d39499f4bf79b27eaf44
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using TEngine;
|
||||
|
||||
namespace TEngine
|
||||
{
|
||||
public class SafeTop : MonoBehaviour
|
||||
{
|
||||
void Start()
|
||||
{
|
||||
var topRect = gameObject.transform as RectTransform;
|
||||
CheckNotch(true);
|
||||
if (topRect != null)
|
||||
{
|
||||
var anchoredPosition = topRect.anchoredPosition;
|
||||
anchoredPosition = new Vector2(anchoredPosition.x, anchoredPosition.y - _notchHeight);
|
||||
topRect.anchoredPosition = anchoredPosition;
|
||||
}
|
||||
}
|
||||
|
||||
private static float _notchHeight;
|
||||
|
||||
public static void CheckNotch(bool applyEditorNotch = false)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
_notchHeight = applyEditorNotch ? Screen.safeArea.y > 0f ? Screen.safeArea.y : Screen.currentResolution.height - Screen.safeArea.height : 0f;
|
||||
#else
|
||||
_notchHeight = Screen.safeArea.y > 0f ? Screen.safeArea.y : Screen.currentResolution.height - Screen.currentResolution.height;
|
||||
#endif
|
||||
Debug.Log($"CheckNotch :{_notchHeight}");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0a563c3e931db74f94f9991270a2dee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user