using System.IO; using NUnit.Framework; using TMPro; using UnityEditor; using UnityEngine; using XGame.EditorTools; public sealed class JsonUIPrefabBuilderInputFieldTests { private const string JsonPath = "../Temp/UIGenTests/UI_TMPInputTest.json"; private const string PrefabPath = "Assets/Temp/UIGenTests/UI_TMPInputTest.prefab"; [TearDown] public void TearDown() { AssetDatabase.DeleteAsset(PrefabPath); AssetDatabase.DeleteAsset("Assets/Temp/UIGenTests"); string fullJsonPath = Path.GetFullPath(Path.Combine(Application.dataPath, JsonPath)); if (File.Exists(fullJsonPath)) { File.Delete(fullJsonPath); } } [Test] public void BuildFromJsonFile_CreatesAndBindsTmpInputField() { string fullJsonPath = WriteJson(); string outputPath = JsonUIPrefabBuilder.BuildFromJsonFile(fullJsonPath); Assert.That(outputPath, Is.EqualTo(PrefabPath)); GameObject prefab = AssetDatabase.LoadAssetAtPath(PrefabPath); Assert.That(prefab, Is.Not.Null); Transform inputTransform = prefab.transform.Find("InputHitArea"); Assert.That(inputTransform, Is.Not.Null); TMP_InputField input = inputTransform.GetComponent(); Assert.That(input, Is.Not.Null); Assert.That(inputTransform.gameObject.layer, Is.EqualTo(LayerMask.NameToLayer("UI"))); Assert.That(input.textComponent, Is.Not.Null); Assert.That(input.textComponent.name, Is.EqualTo("Txt_InputValue")); Assert.That(input.placeholder, Is.Not.Null); Assert.That(input.placeholder.name, Is.EqualTo("Txt_InputPlaceholder")); Assert.That(input.characterLimit, Is.EqualTo(140)); Assert.That(input.lineType, Is.EqualTo(TMP_InputField.LineType.SingleLine)); } private static string WriteJson() { string fullJsonPath = Path.GetFullPath(Path.Combine(Application.dataPath, JsonPath)); Directory.CreateDirectory(Path.GetDirectoryName(fullJsonPath)); File.WriteAllText(fullJsonPath, @"{ ""schemaVersion"": 1, ""prefabName"": ""UI_TMPInputTest"", ""prefabPath"": ""Assets/Temp/UIGenTests/UI_TMPInputTest.prefab"", ""description"": ""TMP input field test."", ""canvas"": { ""canvasScaler"": { ""referenceResolution"": [2048, 1024], ""matchWidthOrHeight"": 0.5 } }, ""assets"": { ""sprites"": {} }, ""nodes"": [ { ""name"": ""UI_TMPInputTest"", ""parent"": null, ""active"": true, ""rect"": { ""anchorMin"": [0, 0], ""anchorMax"": [1, 1], ""offsetMin"": [0, 0], ""offsetMax"": [0, 0] }, ""components"": [] }, { ""name"": ""InputHitArea"", ""parent"": ""UI_TMPInputTest"", ""active"": true, ""rect"": { ""anchorMin"": [0.5, 0.5], ""anchorMax"": [0.5, 0.5], ""pivot"": [0.5, 0.5], ""anchoredPosition"": [0, 0], ""sizeDelta"": [520, 80] }, ""components"": [ { ""type"": ""Image"", ""imageType"": ""Sliced"", ""raycastTarget"": true, ""color"": ""#FFFFFFFF"" }, { ""type"": ""TMP_InputField"", ""textComponent"": ""Txt_InputValue"", ""placeholderComponent"": ""Txt_InputPlaceholder"", ""placeholder"": ""输入内容..."", ""characterLimit"": 140, ""lineType"": ""SingleLine"", ""contentType"": ""Standard"" } ] }, { ""name"": ""Txt_InputPlaceholder"", ""parent"": ""InputHitArea"", ""active"": true, ""rect"": { ""anchorMin"": [0, 0], ""anchorMax"": [1, 1], ""offsetMin"": [24, 0], ""offsetMax"": [-24, 0] }, ""components"": [ { ""type"": ""TextMeshProUGUI"", ""text"": ""输入内容..."", ""fontSize"": 28, ""alignment"": ""Left"", ""color"": ""#7A8DA1FF"", ""raycastTarget"": false } ] }, { ""name"": ""Txt_InputValue"", ""parent"": ""InputHitArea"", ""active"": true, ""rect"": { ""anchorMin"": [0, 0], ""anchorMax"": [1, 1], ""offsetMin"": [24, 0], ""offsetMax"": [-24, 0] }, ""components"": [ { ""type"": ""TextMeshProUGUI"", ""text"": """", ""fontSize"": 28, ""alignment"": ""Left"", ""color"": ""#23384CFF"", ""raycastTarget"": false } ] } ], ""bindings"": [], ""events"": [] }"); return fullJsonPath; } }