Helper.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using UnityEngine;
  9. namespace UnityGameFramework.Runtime
  10. {
  11. /// <summary>
  12. /// 辅助器创建器相关的实用函数。
  13. /// </summary>
  14. public static class Helper
  15. {
  16. /// <summary>
  17. /// 创建辅助器。
  18. /// </summary>
  19. /// <typeparam name="T">要创建的辅助器类型。</typeparam>
  20. /// <param name="helperTypeName">要创建的辅助器类型名称。</param>
  21. /// <param name="customHelper">若要创建的辅助器类型为空时,使用的自定义辅助器类型。</param>
  22. /// <returns>创建的辅助器。</returns>
  23. public static T CreateHelper<T>(string helperTypeName, T customHelper) where T : MonoBehaviour
  24. {
  25. return CreateHelper(helperTypeName, customHelper, 0);
  26. }
  27. /// <summary>
  28. /// 创建辅助器。
  29. /// </summary>
  30. /// <typeparam name="T">要创建的辅助器类型。</typeparam>
  31. /// <param name="helperTypeName">要创建的辅助器类型名称。</param>
  32. /// <param name="customHelper">若要创建的辅助器类型为空时,使用的自定义辅助器类型。</param>
  33. /// <param name="index">要创建的辅助器索引。</param>
  34. /// <returns>创建的辅助器。</returns>
  35. public static T CreateHelper<T>(string helperTypeName, T customHelper, int index) where T : MonoBehaviour
  36. {
  37. T helper = null;
  38. if (!string.IsNullOrEmpty(helperTypeName))
  39. {
  40. System.Type helperType = Utility.Assembly.GetType(helperTypeName);
  41. if (helperType == null)
  42. {
  43. Log.Warning("Can not find helper type '{0}'.", helperTypeName);
  44. return null;
  45. }
  46. if (!typeof(T).IsAssignableFrom(helperType))
  47. {
  48. Log.Warning("Type '{0}' is not assignable from '{1}'.", typeof(T).FullName, helperType.FullName);
  49. return null;
  50. }
  51. helper = (T)new GameObject().AddComponent(helperType);
  52. }
  53. else if (customHelper == null)
  54. {
  55. Log.Warning("You must set custom helper with '{0}' type first.", typeof(T).FullName);
  56. return null;
  57. }
  58. else if (customHelper.gameObject.InScene())
  59. {
  60. helper = index > 0 ? Object.Instantiate(customHelper) : customHelper;
  61. }
  62. else
  63. {
  64. helper = Object.Instantiate(customHelper);
  65. }
  66. return helper;
  67. }
  68. }
  69. }