AkiraPixelWind/Assets/Scripts/Main/CustomsComponent/MapComponent.cs

69 lines
1.9 KiB
C#

using Axibug.Runtime;
using UnityEngine;
using UnityEditor;
using Axibug.Resources;
namespace Game
{
public class MapComponent : GameComponent
{
public bool bLoadFinish { get;private set; } = false;
/// <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;
//TODO Òì²½
GameObject mapgo = CloneMap(MapId);
SpawnPos = mapgo.transform.Find("SpawnPos").transform.position;
bLoadFinish = true;
}
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;
}
}
}