88 lines
2.5 KiB
C#
88 lines
2.5 KiB
C#
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<GameObject> MapGoList = new List<GameObject>();
|
|
/// <summary>
|
|
/// ³öÉúµã
|
|
/// </summary>
|
|
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<GameObject>(MapName);
|
|
#endif
|
|
}
|
|
else
|
|
{
|
|
int id = MapName.GetHashCode();
|
|
asset = PrefabManager.LoadPrefab<UnityEngine.Object>(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;
|
|
}
|
|
}
|
|
}
|
|
|