StarSystem CreateStarSystem(Vector2 position) { SystemType systemType = map.systemTypes.Find(GetSystemType).systemType; if (systemType == null) { systemType = map.systemTypes.Find((x) => x.systemType.useDefault).systemType; } GameObject systemObject = systemType.gameObjects.Find((x) => UnityEngine.Random.Range(0f, 1f) <= x.probability).gameObject; if (systemObject == null) { systemObject = systemType.gameObjects[0].gameObject; } systemObject = GameObject.Instantiate(systemObject); float size = UnityEngine.Random.Range(systemType.minSizeMultiplier, systemType.maxSizeMultiplier); systemObject.transform.GetChild(0).localScale = new Vector3(size, 1, size); string systemName = (starSystems.Count == 0 && systemType.names.Count > 0) ? systemType.names[0] : "System"; foreach (StarSystem system in starSystems) { systemName = systemType.names.Find((x) => (x != system.Name)); if (systemName == null) { systemName = "System"; //TODO: replace with default names } } List <Planet> planets = new List <Planet>(); for (int i = 0; i < UnityEngine.Random.Range(systemType.minPlanets, systemType.maxPlanets); i++) { PlanetType planet = systemType.planets.Find((x) => UnityEngine.Random.Range(0f, 1f) <= x.probability).planet; if (planet == null) { planet = systemType.planets[0].planet; } planets.Add(new Planet(planet.name)); } return(new StarSystem(systemName, systemObject, planets, position)); }
override public void OnInspectorGUI() { SystemType systemType = (SystemType)target; EditorUtility.SetDirty(systemType); EditorGUILayout.BeginVertical(); generalSettings = EditorGUILayout.Foldout(generalSettings, "System General Settings", true); if (generalSettings) { string storedSystemTypeName = systemType.name; systemType.name = EditorGUILayout.DelayedTextField("Type Name: ", systemType.name); if (systemType.name != storedSystemTypeName) { string assetPath = AssetDatabase.GetAssetPath(systemType.GetInstanceID()); AssetDatabase.RenameAsset(assetPath, systemType.name); AssetDatabase.SaveAssets(); } EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); GUILayout.Label("Size Multipliers: "); EditorGUIUtility.labelWidth = 40f; systemType.minSizeMultiplier = EditorGUILayout.FloatField("Min: ", systemType.minSizeMultiplier); systemType.maxSizeMultiplier = EditorGUILayout.FloatField("Max: ", systemType.maxSizeMultiplier); EditorGUIUtility.labelWidth = 120f; EditorGUILayout.EndHorizontal(); } namesSettings = EditorGUILayout.Foldout(namesSettings, "System Names List", true); if (namesSettings) { EditorGUI.indentLevel++; int namesCount = Mathf.Max(0, EditorGUILayout.DelayedIntField("Size: ", systemType.names.Count)); while (namesCount < systemType.names.Count) { systemType.names.RemoveAt(systemType.names.Count - 1); } while (namesCount > systemType.names.Count) { systemType.names.Add(null); } EditorGUILayout.BeginVertical(); EditorGUI.indentLevel++; for (int i = 0; i < systemType.names.Count; i++) { systemType.names[i] = (string)EditorGUILayout.TextField("Name " + i, systemType.names[i]); } EditorGUILayout.EndVertical(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); GUILayout.Label("Use default names list: "); systemType.useDefault = EditorGUILayout.Toggle(systemType.useDefault); EditorGUILayout.EndHorizontal(); EditorGUI.indentLevel--; EditorGUI.indentLevel--; } gameObjectsSettings = EditorGUILayout.Foldout(gameObjectsSettings, "System Type Objects", true); if (gameObjectsSettings) { EditorGUI.indentLevel++; int objectsCount = Mathf.Max(0, EditorGUILayout.DelayedIntField("Size: ", systemType.gameObjects.Count)); while (objectsCount < systemType.gameObjects.Count) { systemType.gameObjects.RemoveAt(systemType.gameObjects.Count - 1); } while (objectsCount > systemType.gameObjects.Count) { systemType.gameObjects.Add(new ObjectAndProbability()); } EditorGUILayout.BeginVertical(); EditorGUI.indentLevel++; if (objectsCount > 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); GUILayout.Label("GameObject"); GUILayout.Label("Probability"); EditorGUILayout.EndHorizontal(); } for (int i = 0; i < systemType.gameObjects.Count; i++) { ObjectAndProbability structure = systemType.gameObjects[i]; EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); structure.gameObject = (GameObject)EditorGUILayout.ObjectField(systemType.gameObjects[i].gameObject, typeof(GameObject), false); structure.probability = EditorGUILayout.Slider(systemType.gameObjects[i].probability, 0f, 1f); EditorGUILayout.EndHorizontal(); systemType.gameObjects[i] = structure; } EditorGUILayout.EndVertical(); EditorGUI.indentLevel--; EditorGUI.indentLevel--; } planetsSettings = EditorGUILayout.Foldout(planetsSettings, "Planet Types", true); if (planetsSettings) { EditorGUI.indentLevel++; EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); GUILayout.Label("Planet count: "); EditorGUIUtility.labelWidth = 40f; systemType.minPlanets = EditorGUILayout.IntField("Min: ", systemType.minPlanets); systemType.maxPlanets = EditorGUILayout.IntField("Max: ", systemType.maxPlanets); EditorGUIUtility.labelWidth = 120f; EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); int objectsCount = Mathf.Max(0, EditorGUILayout.DelayedIntField("Size: ", systemType.planets.Count)); while (objectsCount < systemType.planets.Count) { systemType.planets.RemoveAt(systemType.planets.Count - 1); } while (objectsCount > systemType.planets.Count) { systemType.planets.Add(new PlanetAndProbability()); } EditorGUILayout.BeginVertical(); EditorGUI.indentLevel++; if (objectsCount > 0) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); GUILayout.Label("Planet"); GUILayout.Label("Probability"); EditorGUILayout.EndHorizontal(); } for (int i = 0; i < systemType.planets.Count; i++) { PlanetAndProbability structure = systemType.planets[i]; EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); structure.planet = (PlanetType)EditorGUILayout.ObjectField(systemType.planets[i].planet, typeof(PlanetType), false); structure.probability = EditorGUILayout.Slider(systemType.planets[i].probability, 0f, 1f); EditorGUILayout.EndHorizontal(); systemType.planets[i] = structure; } EditorGUILayout.EndVertical(); } if (GUILayout.Button("Save Object")) { systemType.gameObjects.Sort((x, y) => y.probability.CompareTo(x.probability)); systemType.planets.Sort((x, y) => y.probability.CompareTo(x.probability)); string assetPath = AssetDatabase.GetAssetPath(systemType.GetInstanceID()); AssetDatabase.RenameAsset(assetPath, systemType.name); AssetDatabase.SaveAssets(); } EditorGUILayout.EndVertical(); }