ComponentExtensions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Harmony;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace XUnity.AutoTranslator.Plugin.Core.Extensions
  9. {
  10. public static class ComponentExtensions
  11. {
  12. private static readonly string TextPropertyName = "text";
  13. private static readonly string TexturePropertyName = "texture";
  14. private static readonly string MainTexturePropertyName = "mainTexture";
  15. private static readonly string MarkAsChangedMethodName = "MarkAsChanged";
  16. public static string GetText( this object ui )
  17. {
  18. if( ui == null ) return null;
  19. string text = null;
  20. var type = ui.GetType();
  21. if( ui is Text )
  22. {
  23. text = ( (Text)ui ).text;
  24. }
  25. else if( ui is GUIContent )
  26. {
  27. text = ( (GUIContent)ui ).text;
  28. }
  29. else
  30. {
  31. // fallback to reflective approach
  32. text = (string)ui.GetType()?.GetProperty( TextPropertyName )?.GetValue( ui, null );
  33. }
  34. return text ?? string.Empty;
  35. }
  36. public static void SetText( this object ui, string text )
  37. {
  38. if( ui == null ) return;
  39. var type = ui.GetType();
  40. if( ui is Text )
  41. {
  42. ( (Text)ui ).text = text;
  43. }
  44. else if( ui is GUIContent )
  45. {
  46. ( (GUIContent)ui ).text = text;
  47. }
  48. else
  49. {
  50. // fallback to reflective approach
  51. type.GetProperty( TextPropertyName )?.GetSetMethod()?.Invoke( ui, new[] { text } );
  52. }
  53. }
  54. public static string GetTextureName( this Texture texture )
  55. {
  56. if( !string.IsNullOrEmpty( texture.name ) ) return texture.name;
  57. return "Unnamed";
  58. }
  59. public static Texture2D GetTexture( this object ui )
  60. {
  61. if( ui is Image image )
  62. {
  63. return image.mainTexture as Texture2D;
  64. }
  65. else if( ui is RawImage rawImage )
  66. {
  67. return rawImage.mainTexture as Texture2D;
  68. }
  69. else
  70. {
  71. // lets attempt some reflection for several known types
  72. var type = ui.GetType();
  73. var texture = type.GetProperty( MainTexturePropertyName )?.GetValue( ui, null )
  74. ?? type.GetProperty( TexturePropertyName )?.GetValue( ui, null );
  75. return texture as Texture2D;
  76. }
  77. }
  78. public static void SetAllDirtyEx( this object ui )
  79. {
  80. if( ui == null ) return;
  81. if( ui is Graphic graphic )
  82. {
  83. graphic.SetAllDirty();
  84. }
  85. else
  86. {
  87. // lets attempt some reflection for several known types
  88. var type = ui.GetType();
  89. AccessTools.Method( type, MarkAsChangedMethodName )?.Invoke( ui, null );
  90. }
  91. }
  92. }
  93. public static class UILabelExtensions
  94. {
  95. private static readonly string SpacingYPropertyName = "spacingY";
  96. private static readonly string FloatSpacingYPropertyName = "floatSpacingY";
  97. private static readonly string UseFloatSpacingPropertyName = "useFloatSpacing";
  98. public static object GetSpacingY( this object uiLabel )
  99. {
  100. var type = uiLabel.GetType();
  101. var useFloatSpacing = (bool)type.GetProperty( UseFloatSpacingPropertyName )?.GetGetMethod()?.Invoke( uiLabel, null );
  102. if( useFloatSpacing )
  103. {
  104. return type.GetProperty( FloatSpacingYPropertyName )?.GetGetMethod()?.Invoke( uiLabel, null );
  105. }
  106. else
  107. {
  108. return type.GetProperty( SpacingYPropertyName )?.GetGetMethod()?.Invoke( uiLabel, null );
  109. }
  110. }
  111. public static void SetSpacingY( this object uiLabel, object spacing )
  112. {
  113. var type = uiLabel.GetType();
  114. if( spacing is float )
  115. {
  116. type.GetProperty( FloatSpacingYPropertyName )?.GetSetMethod()?.Invoke( uiLabel, new[] { spacing } );
  117. }
  118. else
  119. {
  120. type.GetProperty( SpacingYPropertyName )?.GetSetMethod()?.Invoke( uiLabel, new[] { spacing } );
  121. }
  122. }
  123. public static object Multiply( this object numeric, float scale )
  124. {
  125. if( numeric is float )
  126. {
  127. return (float)numeric * scale;
  128. }
  129. else
  130. {
  131. return (int)( (int)numeric * scale );
  132. }
  133. }
  134. }
  135. }