Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity 根据父对象的大小,按比例设置该对象的尺寸
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[AddComponentMenu("Layout/Content Size By Parent", 143)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[DisallowMultipleComponent]
public class ContentSizeByParent : UIBehaviour, ILayoutSelfController
{
public enum AspectMode { None, WidthControlsHeight, HeightControlsWidth, FitInParent, EnvelopeParent }
[SerializeField] private float m_scale = 1;
public float scale {
get { return m_scale; }
set {
if (m_scale != value)
{
m_scale = value;
SetDirty();
}
}
}
[System.NonSerialized]
private RectTransform m_Rect;
private RectTransform rectTransform
{
get
{
if (m_Rect == null)
m_Rect = GetComponent<RectTransform>();
return m_Rect;
}
}
private DrivenRectTransformTracker m_Tracker;
protected ContentSizeByParent() { }
protected override void OnEnable()
{
base.OnEnable();
SetDirty();
}
protected override void OnDisable()
{
m_Tracker.Clear();
LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
base.OnDisable();
}
protected override void OnRectTransformDimensionsChange()
{
SetDirty();
}
protected override void OnTransformParentChanged()
{
base.OnTransformParentChanged();
SetDirty();
}
private void UpdateRect(RectTransform.Axis axis)
{
if (!IsActive())
return;
m_Tracker.Clear();
{
m_Tracker.Add(this, rectTransform,
DrivenTransformProperties.Anchors |
DrivenTransformProperties.SizeDeltaX |
DrivenTransformProperties.SizeDeltaY);
rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = Vector2.one;
Vector2 sizeDelta = Vector2.zero;
Vector2 parentSize = GetParentSize();
// if ((parentSize.y * scale < parentSize.x) ^ (m_AspectMode == AspectMode.FitInParent))
{
//sizeDelta.y = GetSizeDeltaToProduceSize(parentSize.x / scale, 1);
}
//else
{
//sizeDelta.x = GetSizeDeltaToProduceSize(parentSize.y * scale, 0);
}
sizeDelta = parentSize * scale;
if (axis == RectTransform.Axis.Horizontal)
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, sizeDelta.x);
else
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, sizeDelta.y);
}
}
private float GetSizeDeltaToProduceSize(float size, int axis)
{
return size - GetParentSize()[axis] * (rectTransform.anchorMax[axis] - rectTransform.anchorMin[axis]);
}
private Vector2 GetParentSize()
{
RectTransform parent = rectTransform.parent as RectTransform;
if (!parent)
return Vector2.zero;
return parent.rect.size;
}
public virtual void SetLayoutHorizontal()
{
UpdateRect(RectTransform.Axis.Horizontal);
}
public virtual void SetLayoutVertical()
{
UpdateRect(RectTransform.Axis.Vertical);
}
protected void SetDirty()
{
if (!IsActive())
return;
LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
}
#if UNITY_EDITOR
protected override void OnValidate()
{
m_scale = Mathf.Clamp(m_scale, 0.001f, 1000f);
SetDirty();
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.