void UpdateLayouts()
    {
        if (transform.parent == null)
        {
            Debug.LogWarning("AspectRatioLayouts requires a parent.");
            return;
        }

        if (anchors.Length < 2)
        {
            Debug.LogWarning("AspectRatioLayouts requires at least two reference anchors.");
        }

        RectTransform parent     = transform.parent as RectTransform;
        Vector2       parentSize = parent.rect.size;
        float         aspect     = parentSize.x / parentSize.y;

        AspectRatioLayout closest     = anchors[0];
        float             closestDist = Mathf.Abs(aspect - closest.aspect);

        for (int i = 1; i < anchors.Length; i++)
        {
            float dist = Mathf.Abs(anchors[i].aspect - aspect);
            if (dist < closestDist)
            {
                closest     = anchors[i];
                closestDist = dist;
            }
        }
        rectTransform.anchorMin        = closest.anchorMin;
        rectTransform.anchorMax        = closest.anchorMax;
        rectTransform.anchoredPosition = closest.anchoredPosition;
        rectTransform.pivot            = closest.pivot;
        rectTransform.sizeDelta        = closest.sizeDelta;
    }
Exemplo n.º 2
0
    public static void RestoreScene()
    {
        GameObject activeObject = Selection.activeObject as GameObject;

        if (null == activeObject)
        {
            UnityEngine.Debug.LogError("Please select Layouts GameObject!");
            return;
        }
        Layouts layouts = activeObject.GetComponent <Layouts>();

        if (null == layouts)
        {
            UnityEngine.Debug.LogError("Please select the GameObject whitch Layouts attached!");
            return;
        }

        AspectRatio ratio = Layouts.GetAspectRatio_();

        if (ratio == AspectRatio.Unknown)
        {
            UnityEngine.Debug.LogError("AspectRatio.Unknow: Not Update!");
            return;
        }

        if (layouts.modifyDesign)
        {
            CanvasScaler scaler = layouts.GetComponent <CanvasScaler>();
            if (null != scaler)
            {
                Layouts.SetDesignRes_(scaler, ratio);
            }
            else
            {
                Debug.LogError("CanvasScaler is Null!");
            }
        }

        for (int i = 0; i < layouts.layouts.Length; ++i)
        {
            Layout            layout       = layouts.layouts[i];
            RectTransform     trans        = layout.transform;
            AspectRatioLayout aspectLayout = layout.GetLayout(ratio);

            if (!aspectLayout.Equals(AspectRatioLayout.Null))
            {
                trans.localScale       = aspectLayout.scale;
                trans.anchoredPosition = aspectLayout.position;
            }
            else
            {
                Debug.LogError("AspectRatioLayout.Null");
            }
        }

        Debug.Log("Restore Scene Success!");
    }
Exemplo n.º 3
0
 public void SetLayout(AspectRatio aspectRatio, AspectRatioLayout layout)
 {
     if (null != layouts)
     {
         for (int i = 0; i < layouts.Length; ++i)
         {
             if (layouts[i].aspectRatio == aspectRatio)
             {
                 layouts[i] = layout;
                 return;
             }
         }
     }
 }
Exemplo n.º 4
0
 public void UpdateLayouts()
 {
     if (null != layouts)
     {
         AspectRatio ratio = GetAspectRatio();
         for (int i = 0; i < layouts.Length; ++i)
         {
             Layout layout = layouts[i];
             if (layout.HasLayout(ratio))
             {
                 AspectRatioLayout aspect = layout.GetLayout(ratio);
                 if (!aspect.Equals(AspectRatioLayout.Null))
                 {
                     layout.transform.localScale       = aspect.scale;
                     layout.transform.anchoredPosition = aspect.position;
                 }
             }
         }
     }
 }
Exemplo n.º 5
0
    public static void UpdateLayouts()
    {
        GameObject activeObject = Selection.activeObject as GameObject;

        if (null == activeObject)
        {
            UnityEngine.Debug.LogError("Please select Layouts GameObject!");
            return;
        }
        Layouts layouts = activeObject.GetComponent <Layouts>();

        if (null == layouts)
        {
            UnityEngine.Debug.LogError("Please select the GameObject whitch Layouts attached!");
            return;
        }

        AspectRatio ratio = Layouts.GetAspectRatio_();

        if (ratio == AspectRatio.Unknown)
        {
            UnityEngine.Debug.LogError("AspectRatio.Unknow: Not Update!");
            return;
        }

        for (int i = 0; i < layouts.layouts.Length; ++i)
        {
            Layout            layout       = layouts.layouts[i];
            RectTransform     trans        = layout.transform;
            AspectRatioLayout aspectLayout = layout.GetLayout(ratio);
            if (!aspectLayout.Equals(AspectRatioLayout.Null))
            {
                aspectLayout.scale    = trans.localScale;
                aspectLayout.position = trans.anchoredPosition;
                layout.SetLayout(ratio, aspectLayout);
            }
        }

        Debug.Log("Update Layouts Success!");
    }