123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using GameFramework;
- using UnityEngine;
- namespace UnityGameFramework.Runtime
- {
- /// <summary>
- /// 辅助器创建器相关的实用函数。
- /// </summary>
- public static class Helper
- {
- /// <summary>
- /// 创建辅助器。
- /// </summary>
- /// <typeparam name="T">要创建的辅助器类型。</typeparam>
- /// <param name="helperTypeName">要创建的辅助器类型名称。</param>
- /// <param name="customHelper">若要创建的辅助器类型为空时,使用的自定义辅助器类型。</param>
- /// <returns>创建的辅助器。</returns>
- public static T CreateHelper<T>(string helperTypeName, T customHelper) where T : MonoBehaviour
- {
- return CreateHelper(helperTypeName, customHelper, 0);
- }
- /// <summary>
- /// 创建辅助器。
- /// </summary>
- /// <typeparam name="T">要创建的辅助器类型。</typeparam>
- /// <param name="helperTypeName">要创建的辅助器类型名称。</param>
- /// <param name="customHelper">若要创建的辅助器类型为空时,使用的自定义辅助器类型。</param>
- /// <param name="index">要创建的辅助器索引。</param>
- /// <returns>创建的辅助器。</returns>
- public static T CreateHelper<T>(string helperTypeName, T customHelper, int index) where T : MonoBehaviour
- {
- T helper = null;
- if (!string.IsNullOrEmpty(helperTypeName))
- {
- System.Type helperType = Utility.Assembly.GetType(helperTypeName);
- if (helperType == null)
- {
- Log.Warning("Can not find helper type '{0}'.", helperTypeName);
- return null;
- }
- if (!typeof(T).IsAssignableFrom(helperType))
- {
- Log.Warning("Type '{0}' is not assignable from '{1}'.", typeof(T).FullName, helperType.FullName);
- return null;
- }
- helper = (T)new GameObject().AddComponent(helperType);
- }
- else if (customHelper == null)
- {
- Log.Warning("You must set custom helper with '{0}' type first.", typeof(T).FullName);
- return null;
- }
- else if (customHelper.gameObject.InScene())
- {
- helper = index > 0 ? Object.Instantiate(customHelper) : customHelper;
- }
- else
- {
- helper = Object.Instantiate(customHelper);
- }
- return helper;
- }
- }
- }
|