Extensions.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using UnityEngine;
  3. using Object = UnityEngine.Object;
  4. namespace XGame.Framework.Asset
  5. {
  6. internal static class Extensions
  7. {
  8. /// <summary>
  9. /// object转泛型对象的扩展
  10. /// </summary>
  11. /// <typeparam name="TResult"></typeparam>
  12. /// <param name="value"></param>
  13. /// <param name="addressableName">用于转换错误时将名字打印出来</param>
  14. /// <returns></returns>
  15. public static TResult Convert<TResult>(this object value, string addressableName)
  16. {
  17. if (value != null)
  18. {
  19. Type type = typeof(TResult);
  20. if (type.IsInstanceOfType(value))
  21. {
  22. return (TResult)value;
  23. }
  24. if (type.IsClass)
  25. {
  26. if (type.IsSubclassOf(typeof(Component)))
  27. {
  28. var go = value as GameObject;
  29. if (go != null)
  30. {
  31. return go.GetComponent<TResult>();
  32. }
  33. var component = value as Component;
  34. if (component != null)
  35. {
  36. return component.gameObject.GetComponent<TResult>();
  37. }
  38. }
  39. else if (type == typeof(Sprite) && value is Texture2D texture)
  40. {
  41. object sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  42. return (TResult) sprite;
  43. }
  44. }
  45. try
  46. {
  47. TResult result = (TResult)value;
  48. return result;
  49. }
  50. catch (Exception ex)
  51. {
  52. AssetsLog.Error($"object conver type Error. source type:{value.GetType()} name:{value} target:{type} addressable:{addressableName}");
  53. AssetsLog.Exception(ex);
  54. }
  55. }
  56. return default;
  57. }
  58. /// <summary>
  59. /// 返回object的long格式的hashCode
  60. /// UnityEngine.Object返回的是instanceId << 32
  61. /// </summary>
  62. /// <param name="obj"></param>
  63. /// <returns></returns>
  64. public static long GetLongHashCode(this object obj)
  65. {
  66. if (obj == null) return 0;
  67. if (obj is Object uniObj)
  68. {
  69. long instanceId = uniObj.GetInstanceID();
  70. instanceId <<= 32;
  71. return instanceId;
  72. }
  73. return obj.GetHashCode();
  74. }
  75. /// <summary>
  76. /// 释放Object对象
  77. /// 独立资源才能使用Resources.UnloadAsset
  78. /// </summary>
  79. /// <param name="asset"></param>
  80. /// <returns></returns>
  81. public static bool Unload(this Object asset)
  82. {
  83. if (asset == null || asset is Component || asset is GameObject || asset is AssetBundle)
  84. {
  85. //非独立资源
  86. }
  87. else
  88. {
  89. //独立资源才能使用Resources.UnloadAsset
  90. Resources.UnloadAsset(asset);
  91. return true;
  92. }
  93. return false;
  94. }
  95. //public static string SaveToLower(this string msg)
  96. //{
  97. // if (string.IsNullOrEmpty(msg))
  98. // {
  99. // return msg;
  100. // }
  101. // return msg.ToLower();
  102. //}
  103. }
  104. }