Skip to content

Commit

Permalink
Add SafeAreaElement
Browse files Browse the repository at this point in the history
  • Loading branch information
kochounoyume committed Sep 17, 2024
1 parent 0e5af72 commit 4ada0d2
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 0 deletions.
File renamed without changes.
3 changes: 3 additions & 0 deletions Packages/MinimalUtility/Runtime/UIToolkit.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "MinimalUtility.UIToolkit",
"rootNamespace": "MinimalUtility.UIToolkit",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"ENABLE_UITOOLKIT"
],
"versionDefines": [
{
"name": "com.unity.modules.uielements",
"expression": "",
"define": "ENABLE_UITOOLKIT"
}
],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Packages/MinimalUtility/Runtime/UIToolkit/SafeAreaElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEngine;
using UnityEngine.UIElements;
#if UNITY_EDITOR
using Screen = UnityEngine.Device.Screen;
#else
using Screen = UnityEngine.Screen;
#endif

namespace MinimalUtility.UIToolkit
{
/// <summary>
/// セーフエリアを考慮した<see cref="VisualElement"/>.
/// </summary>
public class SafeAreaElement : VisualElement
{
/// <summary>
/// UIBuilderのLibraryに登録するためのUXML要素のファクトリクラス.
/// </summary>
public class SafeAreaFactory : UxmlFactory<SafeAreaElement, UxmlTraits>
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SafeAreaElement"/> class.
/// </summary>
public SafeAreaElement()
{
style.flexGrow = 1;
style.flexShrink = 1;

RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);

#pragma warning disable SA1313
void OnGeometryChanged(GeometryChangedEvent _)
#pragma warning restore SA1313
{
#if UNITY_EDITOR
if (panel.GetType().Name == "EditorPanel") return;
#endif
Rect safeArea = Screen.safeArea;
Vector2 leftTop
= RuntimePanelUtils.ScreenToPanel(panel, new (safeArea.xMin, Screen.height - safeArea.yMax));
Vector2 rightBottom
= RuntimePanelUtils.ScreenToPanel(panel, new (Screen.width - safeArea.xMax, safeArea.yMin));

style.marginLeft = leftTop.x;
style.marginTop = leftTop.y;
style.marginRight = rightBottom.x;
style.marginBottom = rightBottom.y;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ada0d2

Please sign in to comment.