using Axibug.Runtime; using UnityEngine; using UnityEditor; using Axibug.Resources; using System.Collections.Generic; using System.Diagnostics.SymbolStore; using System.Runtime.Remoting.Metadata.W3cXsd2001; namespace Game { public class MapComponent : GameComponent { public bool bLoadFinish { get;private set; } = false; public int mMapId { get; private set; } = 0; List MapGoList = new List(); /// /// 出生点 /// public Vector3 SpawnPos { get; private set; } // Start is called before the first frame update void Start() { } public void LoadMap(int MapId) { bLoadFinish = false; if (mMapId != MapId) RemoveMapGameObjs(); mMapId = MapId; //TODO 异步 GameObject mapgo = CloneMap(MapId); SpawnPos = mapgo.transform.Find("SpawnPos").transform.position; MapGoList.Add(mapgo); bLoadFinish = true; } public void RemoveMapGameObjs() { mMapId = 0; for (int i = 0; i < MapGoList.Count; i++) { Destroy(MapGoList[i]); } MapGoList.Clear(); } private GameObject CloneMap(int MapId) { string rootPath = "Assets/GameAssets"; string MapName = $"Assets/GameAssets/Map/{MapId}/{MapId}.prefab"; string tmp = MapName.Remove(0, rootPath.Length + 1); int idx = tmp.LastIndexOf('/'); string bundleName = tmp.Substring(0, idx); UnityEngine.Object asset = null; if (AppEntry.Base.EditorResourceMode) { #if UNITY_EDITOR asset = AssetDatabase.LoadAssetAtPath(MapName); #endif } else { int id = MapName.GetHashCode(); asset = PrefabManager.LoadPrefab(bundleName.ToLower(), MapName, id, this.transform); } if (asset == null) { Debug.LogError($"asset加载失败,path={MapName}"); return null; } GameObject go = Instantiate(asset, this.transform) as GameObject; if (go == null) { Debug.LogError("LoadPrefabByEditor2. go == null. asset:" + asset); } return go; } } }