Skip to content

Instantly share code, notes, and snippets.

@robertwahler
Last active July 16, 2024 10:01
Show Gist options
  • Select an option

  • Save robertwahler/016a820af52521a5cb0d to your computer and use it in GitHub Desktop.

Select an option

Save robertwahler/016a820af52521a5cb0d to your computer and use it in GitHub Desktop.

Revisions

  1. robertwahler revised this gist Dec 2, 2018. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions DynamicLayoutGroup.cs
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,6 @@
    using System.Collections;
    using System.Collections.Generic;

    using SDD.Extensions;
    using SDD.Events;

    namespace SDD.UI {

    /// <summary>
  2. robertwahler created this gist Oct 31, 2015.
    69 changes: 69 additions & 0 deletions DynamicLayoutGroup.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    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);
    }
    }
    }
    21 changes: 21 additions & 0 deletions DynamicLayoutGroupEditor.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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 {
    }
    }
    10 changes: 10 additions & 0 deletions ScreenOrientation.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    namespace SDD {

    /// <summary>
    /// Orientation of the screen regardless of which way is up
    /// </summary>
    public enum ScreenOrientation {
    Landscape,
    Portrait
    }
    }