forked from MinaPecheux/UnityTutorials-RTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapMetadataExtractor.cs
More file actions
59 lines (51 loc) · 1.95 KB
/
Copy pathMapMetadataExtractor.cs
File metadata and controls
59 lines (51 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
public static class MapMetadataExtractor
{
private static readonly string _mapDataFolder = "Resources/ScriptableObjects/Maps";
public static void Extract(Scene s)
{
// check the map metadata Scriptable Objects folder exists,
// or create it
if (!System.IO.Directory.Exists(Application.dataPath + $"/{_mapDataFolder}"))
System.IO.Directory.CreateDirectory(Application.dataPath + $"/{_mapDataFolder}");
// get the scene name + set it as active
string sceneName = s.name;
EditorSceneManager.SetActiveScene(s);
// try to get the "Terrain" object
GameObject[] objs = s.GetRootGameObjects();
Terrain terrain = null;
foreach (GameObject g in objs)
{
terrain = g.GetComponent<Terrain>();
if (terrain != null)
break;
}
if (terrain == null)
{
Debug.LogWarning("There is no 'Terrain' component in this scene!");
return;
}
// try and get the map metadata Scriptable Object,
// or create and save it
string assetPath = $"Assets/{_mapDataFolder}/{sceneName}.asset";
MapData data = AssetDatabase.LoadAssetAtPath<MapData>(assetPath);
if (data == null)
{
data = ScriptableObject.CreateInstance<MapData>();
AssetDatabase.CreateAsset(data, assetPath);
data.mapName = sceneName;
data.sceneName = sceneName;
}
// get the map size
Bounds bounds = terrain.terrainData.bounds;
data.mapSize = bounds.size.x;
// get the max number of players = number of spawnpoints
data.maxPlayers = GameObject.Find("Spawnpoints").transform.childCount;
// update the Scriptable Object
EditorUtility.SetDirty(data);
AssetDatabase.SaveAssets();
}
}