Example #1
0
        public void TestSetup()
        {
            m_PrefabRoot        = Object.Instantiate(Resources.Load("AspectRatioFitterTests")) as GameObject;
            m_AspectRatioFitter = m_PrefabRoot.GetComponentInChildren <AspectRatioFitter>();

            m_AspectRatioFitter.enabled = true;

            m_RectTransform           = m_AspectRatioFitter.GetComponent <RectTransform>();
            m_RectTransform.sizeDelta = new Vector2(50, 50);
        }
Example #2
0
        public void TestFitInParent()
        {
            m_PrefabRoot        = Object.Instantiate(Resources.Load("AspectRatioFitterTestsFitInParent")) as GameObject;
            m_AspectRatioFitter = m_PrefabRoot.GetComponentInChildren <AspectRatioFitter>();

            m_AspectRatioFitter.enabled = true;

            m_RectTransform = m_AspectRatioFitter.GetComponent <RectTransform>();
            Assert.AreEqual(200, m_RectTransform.rect.width);
        }
    private static void FitImageAspect(UnityEditor.MenuCommand menuCommand)
    {
        AspectRatioFitter f = menuCommand.context as AspectRatioFitter;

        if (f != null)
        {
            Image img = f.GetComponent <Image>();
            if (img == null && f.transform.parent != null)
            {
                img = f.transform.parent.GetComponent <Image>();
            }
            if (img != null && img.sprite != null)
            {
                UnityEditor.Undo.RecordObject(f, "FitImageAspect");

                f.aspectRatio = (float)img.sprite.texture.width / img.sprite.texture.height;
                UnityEditor.EditorUtility.SetDirty(f);
            }
        }
    }