diff options
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI')
7 files changed, 48 insertions, 29 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Graphics/Image.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Graphics/Image.cs index cae7667..4844c19 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Graphics/Image.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Graphics/Image.cs @@ -1092,7 +1092,7 @@ namespace UnityEngine.UI if (activeSprite == null) return true; - + Vector2 local; if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, eventCamera, out local)) return false; diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/HorizontalOrVerticalLayoutGroup.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/HorizontalOrVerticalLayoutGroup.cs index ebb74e2..3e1c923 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/HorizontalOrVerticalLayoutGroup.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/HorizontalOrVerticalLayoutGroup.cs @@ -1,5 +1,6 @@ namespace UnityEngine.UI { + // 布局算法的核心都在这个基类里面 public abstract class HorizontalOrVerticalLayoutGroup : LayoutGroup { [SerializeField] protected float m_Spacing = 0; @@ -17,21 +18,22 @@ namespace UnityEngine.UI [SerializeField] protected bool m_ChildControlHeight = true; public bool childControlHeight { get { return m_ChildControlHeight; } set { SetProperty(ref m_ChildControlHeight, value); } } + // 根据子节点计算得到自身的min, preferred, flexible protected void CalcAlongAxis(int axis, bool isVertical) { - float combinedPadding = (axis == 0 ? padding.horizontal : padding.vertical); + float combinedPadding = (axis == 0 ? padding.horizontal : padding.vertical); // padding是left+right, top+bottom bool controlSize = (axis == 0 ? m_ChildControlWidth : m_ChildControlHeight); bool childForceExpandSize = (axis == 0 ? childForceExpandWidth : childForceExpandHeight); - float totalMin = combinedPadding; - float totalPreferred = combinedPadding; - float totalFlexible = 0; + float totalMin = combinedPadding; // 所有子节点的min之和 + float totalPreferred = combinedPadding; // 所有子节点的preferred之和 + float totalFlexible = 0; // 是一个无单位数值,等于所有子节点的flexible值相加 bool alongOtherAxis = (isVertical ^ (axis == 1)); for (int i = 0; i < rectChildren.Count; i++) { RectTransform child = rectChildren[i]; - float min, preferred, flexible; + float min, preferred, flexible; // 子节点的三个属性值 GetChildSizes(child, axis, controlSize, childForceExpandSize, out min, out preferred, out flexible); if (alongOtherAxis) @@ -50,18 +52,21 @@ namespace UnityEngine.UI } } + // 减去最后一个子节点多加的spacing if (!alongOtherAxis && rectChildren.Count > 0) { totalMin -= spacing; totalPreferred -= spacing; } + totalPreferred = Mathf.Max(totalMin, totalPreferred); - SetLayoutInputForAxis(totalMin, totalPreferred, totalFlexible, axis); + // 保存到m_TotalMinSize, m_TotalPreferredSize, m_TotalFlexibleSize + SetLayoutInputForAxis(totalMin, totalPreferred, totalFlexible, axis); } protected void SetChildrenAlongAxis(int axis, bool isVertical) { - float size = rectTransform.rect.size[axis]; + float size = rectTransform.rect.size[axis]; // RectTransform大小 bool controlSize = (axis == 0 ? m_ChildControlWidth : m_ChildControlHeight); bool childForceExpandSize = (axis == 0 ? childForceExpandWidth : childForceExpandHeight); float alignmentOnAxis = GetAlignmentOnAxis(axis); @@ -69,7 +74,7 @@ namespace UnityEngine.UI bool alongOtherAxis = (isVertical ^ (axis == 1)); if (alongOtherAxis) { - float innerSize = size - (axis == 0 ? padding.horizontal : padding.vertical); + float innerSize = size - (axis == 0 ? padding.horizontal : padding.vertical); // 容纳子节点的空间 for (int i = 0; i < rectChildren.Count; i++) { RectTransform child = rectChildren[i]; @@ -95,11 +100,11 @@ namespace UnityEngine.UI if (GetTotalFlexibleSize(axis) == 0 && GetTotalPreferredSize(axis) < size) pos = GetStartOffset(axis, GetTotalPreferredSize(axis) - (axis == 0 ? padding.horizontal : padding.vertical)); - float minMaxLerp = 0; + float minMaxLerp = 0; // 塞进min之后的剩余空间/preferred-min的空间,是子节点从min向preferred扩大的依据 if (GetTotalMinSize(axis) != GetTotalPreferredSize(axis)) minMaxLerp = Mathf.Clamp01((size - GetTotalMinSize(axis)) / (GetTotalPreferredSize(axis) - GetTotalMinSize(axis))); - float itemFlexibleMultiplier = 0; + float itemFlexibleMultiplier = 0; // 塞进preferred之后,进一步塞满剩余空间的依据,即flexible if (size > GetTotalPreferredSize(axis)) { if (GetTotalFlexibleSize(axis) > 0) @@ -116,6 +121,7 @@ namespace UnityEngine.UI childSize += flexible * itemFlexibleMultiplier; if (controlSize) { + // 设置子节点的transform SetChildAlongAxis(child, axis, pos, childSize); } else @@ -128,12 +134,16 @@ namespace UnityEngine.UI } } + // 返回节点的min, prefered, flexible大小 + // axis 0是x轴,1是y轴 + // controlSize 是否勾选ChildControlSize + // childForceExpand 是否勾选ChildForceExpand private void GetChildSizes(RectTransform child, int axis, bool controlSize, bool childForceExpand, out float min, out float preferred, out float flexible) { - if (!controlSize) + if (!controlSize) // 如果没勾ChildControlSize,那么会忽略 { - min = child.sizeDelta[axis]; + min = child.sizeDelta[axis];//sizeDelta这里等价于size,因为动态布局系统里面anchor都是一起的 preferred = min; flexible = 0; } diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/ILayoutElement.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/ILayoutElement.cs index ae11e28..d0e1510 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/ILayoutElement.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/ILayoutElement.cs @@ -4,15 +4,17 @@ using System.Collections; namespace UnityEngine.UI { public interface ILayoutElement - { - // After this method is invoked, layout horizontal input properties should return up-to-date values. - // Children will already have up-to-date layout horizontal inputs when this methods is called. + {
+#region LayoutGroup的派生类才会实现,其他ILayoutElement比如Image,Text不会实现 + // After this method is invoked, layout horizontal input properties should return up-to-date values.
+ // Children will already have up-to-date layout horizontal inputs when this methods is called.
void CalculateLayoutInputHorizontal(); // After this method is invoked, layout vertical input properties should return up-to-date values. // Children will already have up-to-date layout vertical inputs when this methods is called. - void CalculateLayoutInputVertical(); - - // Layout horizontal inputs + void CalculateLayoutInputVertical();
+#endregion +
+ // Layout horizontal inputs
float minWidth { get; } float preferredWidth { get; } float flexibleWidth { get; } diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutGroup.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutGroup.cs index 95d9f48..59d432c 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutGroup.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutGroup.cs @@ -39,6 +39,7 @@ namespace UnityEngine.UI // ILayoutElement Interface public virtual void CalculateLayoutInputHorizontal() { + // 收集子节点中参与layout的节点 m_RectChildren.Clear(); var toIgnoreList = ListPool<Component>.Get(); for (int i = 0; i < rectTransform.childCount; i++) @@ -69,6 +70,7 @@ namespace UnityEngine.UI m_Tracker.Clear(); } + // 这个方法不收集是因为在CalculateLayoutInputHorizontal收集了,在LayoutRebuilder的Rebuild()方法中有注释 public abstract void CalculateLayoutInputVertical(); public virtual float minWidth { get { return GetTotalMinSize(0); } } public virtual float preferredWidth { get { return GetTotalPreferredSize(0); } } @@ -126,19 +128,19 @@ namespace UnityEngine.UI protected float GetStartOffset(int axis, float requiredSpaceWithoutPadding) { - float requiredSpace = requiredSpaceWithoutPadding + (axis == 0 ? padding.horizontal : padding.vertical); - float availableSpace = rectTransform.rect.size[axis]; - float surplusSpace = availableSpace - requiredSpace; - float alignmentOnAxis = GetAlignmentOnAxis(axis); + float requiredSpace = requiredSpaceWithoutPadding + (axis == 0 ? padding.horizontal : padding.vertical); // 算上padding后的大小 + float availableSpace = rectTransform.rect.size[axis]; // RectTransform大小 + float surplusSpace = availableSpace - requiredSpace; // 剩余空间 + float alignmentOnAxis = GetAlignmentOnAxis(axis); // 0\0.5\1 return (axis == 0 ? padding.left : padding.top) + surplusSpace * alignmentOnAxis; } protected float GetAlignmentOnAxis(int axis) { if (axis == 0) - return ((int)childAlignment % 3) * 0.5f; + return ((int)childAlignment % 3) * 0.5f; // 0, 0.5, 1 else - return ((int)childAlignment / 3) * 0.5f; + return ((int)childAlignment / 3) * 0.5f; // 0, 0.5, 1 } protected void SetLayoutInputForAxis(float totalMin, float totalPreferred, float totalFlexible, int axis) diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs index 2dc89a5..da201a7 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutRebuilder.cs @@ -5,7 +5,8 @@ namespace UnityEngine.UI { public class LayoutRebuilder : ICanvasElement { - private RectTransform m_ToRebuild; + // m_ToRebuild是一个ILayoutGroup + private RectTransform m_ToRebuild; //There are a few of reasons we need to cache the Hash fromt he transform: // - This is a ValueType (struct) and .Net calculates Hash from the Value Type fields. // - The key of a Dictionary should have a constant Hash value. @@ -50,6 +51,7 @@ namespace UnityEngine.UI components.RemoveAll(e => e is Behaviour && !((Behaviour)e).isActiveAndEnabled); } + // 立即重新布局一次,而不用等到帧末尾CanvasUpdateReigstry.PerformUpdate的时候 public static void ForceRebuildLayoutImmediate(RectTransform layoutRoot) { var rebuilder = s_Rebuilders.Get(); @@ -89,6 +91,7 @@ namespace UnityEngine.UI // since they will be their own roots. if (components.Count > 0) { + // // Layout control needs to executed top down with parents being done before their children, // because the children rely on the sizes of the parents. @@ -123,6 +126,8 @@ namespace UnityEngine.UI // since they will be their own roots. if (components.Count > 0 || rect.GetComponent(typeof(ILayoutGroup))) { + + // 先从子节点开始,最后到父节点,这样父节点可以得到子节点的信息 // Layout calculations needs to executed bottom up with children being done before their parents, // because the parent calculated sizes rely on the sizes of the children. @@ -192,6 +197,7 @@ namespace UnityEngine.UI var rebuilder = s_Rebuilders.Get(); rebuilder.Initialize(controller); + // 注册进去 if (!CanvasUpdateRegistry.TryRegisterCanvasElementForLayoutRebuild(rebuilder)) s_Rebuilders.Release(rebuilder); } diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutUtility.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutUtility.cs index 796f35e..c58abd5 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutUtility.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Layout/LayoutUtility.cs @@ -75,7 +75,7 @@ namespace UnityEngine.UI for (int i = 0; i < components.Count; i++) { - var layoutComp = components[i] as ILayoutElement; + var layoutComp = components[i] as ILayoutElement; // if (layoutComp is Behaviour && !((Behaviour)layoutComp).isActiveAndEnabled) continue; diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/Mask.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/Mask.cs index 19a5575..c67ec42 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/Mask.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/Mask/Mask.cs @@ -36,8 +36,7 @@ namespace UnityEngine.UI } } - [NonSerialized] - private Graphic m_Graphic; + [NonSerialized] private Graphic m_Graphic; public Graphic graphic { get { return m_Graphic ?? (m_Graphic = GetComponent<Graphic>()); } |