Skip to content

Instantly share code, notes, and snippets.

@robertwahler
Last active July 16, 2024 10:01
Show Gist options
  • Save robertwahler/016a820af52521a5cb0d to your computer and use it in GitHub Desktop.
Save robertwahler/016a820af52521a5cb0d to your computer and use it in GitHub Desktop.
Unity 3D layout group switches orientation automatically between landscape and portrait layouts so it either acts like a VerticalLayoutGroup or a HorizontalLayoutGroup.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
namespace SDD.UI {
/// <summary>
/// Layout group switches orientation automatically between landscape and
/// portrait layouts so it either acts like a VerticalLayoutGroup or a
/// HorizontalLayoutGroup.
/// </summary>
[AddComponentMenu("Layout/Dynamic Layout Group", 150)]
public class DynamicLayoutGroup : HorizontalOrVerticalLayoutGroup {
/// <summary>
/// When is the layout vertical? In portrait or landscape.
/// </summary>
[SerializeField]
public ScreenOrientation verticalWhen = ScreenOrientation.Portrait;
public bool IsVertical { get { return GetIsVertical(); }}
private bool GetIsVertical() {
bool isVertical;
if (UnityEngine.Screen.width > UnityEngine.Screen.height) {
//orientation = ScreenOrientation.Landscape;
isVertical = (verticalWhen == ScreenOrientation.Landscape) ? true : false;
}
else {
//orientation = ScreenOrientation.Portrait;
isVertical = (verticalWhen == ScreenOrientation.Portrait) ? true : false;
}
//Log.Debug(string.Format("DynamicLayoutGroup.OnRectTransformDimensionsChange() isVertical={0}a, ID={1}", isVertical, GetInstanceID()));
return isVertical;
}
public override void CalculateLayoutInputHorizontal() {
//Log.Debug(string.Format("DynamicLayoutGroup.CalculateLayoutInputHorizontal() IsVertical={0}, ID={1}", IsVertical, GetInstanceID()));
base.CalculateLayoutInputHorizontal();
CalcAlongAxis(0, isVertical: IsVertical);
}
public override void CalculateLayoutInputVertical() {
//Log.Debug(string.Format("DynamicLayoutGroup.CalculateLayoutInputVertical() IsVertical={0}, ID={1}", IsVertical, GetInstanceID()));
CalcAlongAxis(1, isVertical: IsVertical);
}
public override void SetLayoutHorizontal() {
//Log.Debug(string.Format("DynamicLayoutGroup.SetLayoutHorizontal() IsVertical={0}, ID={1}", IsVertical, GetInstanceID()));
SetChildrenAlongAxis(0, isVertical: IsVertical);
}
public override void SetLayoutVertical() {
//Log.Debug(string.Format("DynamicLayoutGroup.SetLayoutVertical() IsVertical={0}, ID={1}", IsVertical, GetInstanceID()));
SetChildrenAlongAxis(1, isVertical: IsVertical);
}
}
}
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace SDD.UI {
/// <summary>
/// Override the Unity UI custom editor for HorizontalOrVerticalLayoutGroup
/// and do exactly nothing. The only purpose of this class is to expose the
/// public ```VerticalWhen```. The inherited editor prevents editing new
/// publics in descendant classes without doing a full widget layout here
/// (too much work!).
/// </summary>
/// <remarks>
/// Place in ```Editor``` folder
/// </remarks>
[CustomEditor(typeof(SDD.UI.DynamicLayoutGroup), true)]
[CanEditMultipleObjects]
public class DynamicLayoutGroupEditor : UnityEditor.Editor {
}
}
namespace SDD {
/// <summary>
/// Orientation of the screen regardless of which way is up
/// </summary>
public enum ScreenOrientation {
Landscape,
Portrait
}
}
@Rizo0090
Copy link

How can I use it on runtime?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment