/// <summary> /// TEnemy是脚本类型(逻辑层),string是prefab名(表现层预制件) /// 调用一次生成一个敌人并添加到字典 /// </summary> /// <see cref ="m_name"/> /// <typeparam name="TEnemy"></typeparam> /// <param name="prefabName"></param> /// <returns></returns> public static TEnemy SpawnEnemy <TEnemy>(string prefabName) where TEnemy : BaseEnemy, new() { var prefab = AssetModule.LoadAsset <GameObject>($"Assets/AssetBundles_sai/Enemy/enemy_sword{prefabName}.prefab"); var obj = Object.Instantiate(prefab); TEnemy enemy = new TEnemy(); //初始化脚本component变量 //传入的参数name只是prefab名(一个兵种的名称),但会有多个同种兵(所以就用场景中生成的Gobj命名) enemy.Init(obj.name, obj); enemyDic.Add(obj.name, obj); return(enemy); }
/// <summary> /// 创建并获取一个面板实例(同步加载方法) /// </summary> /// <typeparam name="TPanel"></typeparam> /// <returns></returns> public static TPanel OpenPanel <TPanel>(System.Action <TPanel> onPanelOpened = null) where TPanel : BasePanel, new() { //panel脚本实例 var panel = new TPanel(); //panelGobj实例(先加载后创建) var prefab = AssetModule.LoadAsset <GameObject>(panel.Path); GameObject panelGobj = Object.Instantiate(prefab, canvasTransform); panelGobj.name = typeof(TPanel).Name; //初始化panel脚本字段,调用打开时的回调方法 panel.Init(panelGobj.name, panelGobj); panel.OnOpen(); return(panel); }