Maze / Library /PackageCache /com.unity.ugui@1.0.0 /Tests /Runtime /Canvas /CanvasScalerWithChildTextObjectDoesNotCrash.cs
| using UnityEngine; | |
| using UnityEngine.TestTools; | |
| using NUnit.Framework; | |
| using System.Collections; | |
| using UnityEngine.UI; | |
| [] | |
| [] | |
| [] | |
| public class CanvasScalerWithChildTextObjectDoesNotCrash | |
| { | |
| GameObject m_CanvasObject; | |
| [] | |
| public void TestSetup() | |
| { | |
| UnityEditor.EditorApplication.ExecuteMenuItem("Window/General/Game"); | |
| } | |
| [] | |
| public IEnumerator CanvasScalerWithChildTextObjectWithTextFontDoesNotCrash() | |
| { | |
| //This adds a Canvas component as well | |
| m_CanvasObject = new GameObject("Canvas"); | |
| var canvasScaler = m_CanvasObject.AddComponent<CanvasScaler>(); | |
| m_CanvasObject.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceCamera; | |
| //the crash only reproduces if the text component is a child of the game object | |
| //that has the CanvasScaler component and if it has an actual font and text set | |
| var textObject = new GameObject("Text").AddComponent<UnityEngine.UI.Text>(); | |
| textObject.font = Font.CreateDynamicFontFromOSFont("Arial", 14); | |
| textObject.text = "New Text"; | |
| textObject.transform.SetParent(m_CanvasObject.transform); | |
| canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; | |
| canvasScaler.referenceResolution = new Vector2(1080, 1020); | |
| //The crash happens when setting the referenceResolution to a small value | |
| canvasScaler.referenceResolution = new Vector2(9, 9); | |
| //We need to wait a few milliseconds for the crash to happen, otherwise Debug.Log will | |
| //get executed and the test will pass | |
| yield return new WaitForSeconds(0.1f); | |
| Assert.That(true); | |
| } | |
| [] | |
| public void TearDown() | |
| { | |
| GameObject.DestroyImmediate(m_CanvasObject); | |
| } | |
| } | |