Skip to content

Instantly share code, notes, and snippets.

@CrandellWS
Forked from robertwahler/DynamicLayoutGroup.cs
Created February 15, 2018 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrandellWS/aec451f221a52da9e00c0c2cc94150b6 to your computer and use it in GitHub Desktop.
Save CrandellWS/aec451f221a52da9e00c0c2cc94150b6 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;
using SDD.Extensions;
using SDD.Events;
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment