using UnityEngine; namespace Axibug.Runtime { /// /// 辅助器创建器相关的实用函数。 /// public static class Helper { /// /// 创建辅助器。 /// /// 要创建的辅助器类型。 /// 要创建的辅助器类型名称。 /// 若要创建的辅助器类型为空时,使用的自定义辅助器类型。 /// 创建的辅助器。 public static T CreateHelper(string helperTypeName, T customHelper) where T : MonoBehaviour { return CreateHelper(helperTypeName, customHelper, 0); } /// /// 创建辅助器。 /// /// 要创建的辅助器类型。 /// 要创建的辅助器类型名称。 /// 若要创建的辅助器类型为空时,使用的自定义辅助器类型。 /// 要创建的辅助器索引。 /// 创建的辅助器。 public static T CreateHelper(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; } } }