暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputFieldTests.cs 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.TestTools;
  7. using System.Collections;
  8. using System.IO;
  9. using UnityEditor;
  10. using UnityEngine.UI;
  11. using System.Reflection;
  12. namespace InputfieldTests
  13. {
  14. public class BaseInputFieldTests
  15. {
  16. protected GameObject m_PrefabRoot;
  17. IEnumerator Waiting()
  18. {
  19. #if UNITY_EDITOR
  20. // WaitForEndOfFrame doesn't work in batch mode
  21. int startFrame = Time.frameCount;
  22. return new WaitUntil(() => Time.frameCount - startFrame >= 1);
  23. #else
  24. yield return new WaitForEndOfFrame();
  25. #endif
  26. }
  27. protected IEnumerator WaitForCondition(string name, Func<bool> condition, float timeOutInSeconds, Func<string> additionalErrorMessage = null)
  28. {
  29. var start = Time.realtimeSinceStartup;
  30. while (condition() == false)
  31. {
  32. yield return Waiting();
  33. if (Time.realtimeSinceStartup - start > timeOutInSeconds)
  34. {
  35. var msg = $"TimeOut ({timeOutInSeconds} seconds) while waiting for '{name}'";
  36. if (additionalErrorMessage != null)
  37. msg += Environment.NewLine + additionalErrorMessage.Invoke();
  38. throw new Exception(msg);
  39. }
  40. }
  41. }
  42. public void CreateInputFieldAsset(string prefabPath)
  43. {
  44. #if UNITY_EDITOR
  45. var rootGO = new GameObject("rootGo");
  46. var canvasGO = new GameObject("Canvas", typeof(Canvas));
  47. canvasGO.transform.SetParent(rootGO.transform);
  48. var canvas = canvasGO.GetComponent<Canvas>();
  49. canvas.referencePixelsPerUnit = 100;
  50. GameObject inputFieldGO = new GameObject("InputField", typeof(RectTransform), typeof(InputField));
  51. inputFieldGO.transform.SetParent(canvasGO.transform);
  52. GameObject textGO = new GameObject("Text", typeof(RectTransform), typeof(Text));
  53. textGO.transform.SetParent(inputFieldGO.transform);
  54. GameObject eventSystemGO = new GameObject("EventSystem", typeof(EventSystem), typeof(FakeInputModule));
  55. eventSystemGO.transform.SetParent(rootGO.transform);
  56. InputField inputField = inputFieldGO.GetComponent<InputField>();
  57. inputField.interactable = true;
  58. inputField.enabled = true;
  59. inputField.textComponent = textGO.GetComponent<Text>();
  60. inputField.textComponent.fontSize = 12;
  61. inputField.textComponent.supportRichText = false;
  62. if (!Directory.Exists("Assets/Resources/"))
  63. Directory.CreateDirectory("Assets/Resources/");
  64. PrefabUtility.SaveAsPrefabAsset(rootGO, prefabPath);
  65. GameObject.DestroyImmediate(rootGO);
  66. #endif
  67. }
  68. }
  69. }