How to reset build settings from script and set new scenes from file in a certain order for android build?
Hello everyone :)
I'm new to Unity, so please excuse me, if the answer is too obvious..
I have a script, which I start with a batch file. I call it with:
"D:\Unity3D\Editor\Unity.exe" -batchmode -nographics -quit -projectPath %~dp0 -executedMethod BuildUnityPlayer.PerformBuild
pause
Pause, because I want to see, if there are errors, but in this case there aren't any.
The BuildUnityPlayer.PerformBuild file looks like this:
using UnityEngine;
using System;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.IO;
public class BuildUnityPlayer : MonoBehaviour
{
private static string levelsFile = "LevelsToLoad.csv";
public static void PerformBuild()
{
Debug.Log("BuildUnityPlayer active.");
string path = GetPath();
Debug.Log(path);
string[] listOfLevelPaths = GetLevels(path); //1
BuildPlayerOptions resetBuildPlayerOptions = new BuildPlayerOptions()
{
scenes = {}, //2
locationPathName = Application.dataPath,
target = BuildTarget.Android,
options = BuildOptions.None
};
BuildPipeline.BuildPlayer(resetBuildPlayerOptions);
EditorBuildSettingsScene[] original = new EditorBuildSettingsScene[0]; //3
EditorBuildSettings.scenes = original;
foreach (var scenePath in listOfLevelPaths)
{
original = EditorBuildSettings.scenes;
EditorBuildSettingsScene[] newSettings = new EditorBuildSettingsScene[original.Length + 1];
//Array.Copy(original, newSettings, original.Length);
var sceneToAdd = new EditorBuildSettingsScene(scenePath, true);
newSettings[newSettings.Length - 1] = sceneToAdd;
EditorBuildSettings.scenes = newSettings;
}
Debug.Log("starting Customized Build...");
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
{
scenes = listOfLevelPaths, //4
locationPathName = Application.dataPath,
target = BuildTarget.Android,
options = BuildOptions.None
};
BuildPipeline.BuildPlayer(buildPlayerOptions);
Debug.Log("Customized Build performed.");
}
private static string GetPath()
{
return Path.GetFullPath(Application.dataPath);
}
private static string[] GetLevels(string path)
{
if (Directory.Exists(path))
{
string levelsInText = File.ReadAllText(path + "/" + levelsFile);
if (File.Exists("Assets/" + levelsFile))
{
string[] levelsInArray = levelsInText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
return levelsInArray;
}
else
{
Debug.LogError("Could not get file for levels. Check if file 'LevelsToLoad.csv' exists.");
return null;
}
}
else
{
Debug.LogError("Could not get path for levels. Check path for file 'LevelsToLoad.csv'.");
return null;
}
}
}
So there is this file, which contains all paths to the right scenes, which have to be included in the build. The main reason why I want to do it so is because I want this program to be editable from people who have no idea of coding. Their main task will be copy and pasting folders and replace images and sounds as they wish. So I want the scenes to be loaded in a certain order from the file "LevelsToLoad.csv".
The content in excel looks like this:
Assets/donetedit_scenes/Main.unity
Assets/levels/sl_M/Sl.unity
Assets/levels/dr_M/Dr.unity
Assets/levels/dr_N/Dr.unity
1: This means, when "GetLevels(path)" is called, I got a list of these entries.
2: Now I want them in exactly this order. So I thought I could reset the Buildplayeroptions by emptying the scenes.
3: Then by adding to a new list the levels as they are read from the file.
4: And at last build the project with BuildPlayerOptions but now with the new ordered scenes.
The problem now is that the scenes are added, if I delete them from the build settings in Unity Editor first; BUT if I let them there, they are not changed when I modify the "LevelsToLoad.csv" file.
PS: if somebody knows, why I don't get an .apk file here, you would help me A LOT! (probably this is the problem anyway...)
Pps: changing the locationPathName to locationPathName = Application.dataPath + "/gamename.apk",
does not make a change... (if somebody was wandering)
Best, Nina
Answer by Nin4ikP · Dec 10, 2017 at 03:27 PM
Okay guys,
The actual problem seemed to be within the command line: it must be "executeMethod" not "executedMethod". I accidently copy-pasted it from somewhere.
But I still do not get an .apk file in the end. Maybe somebody can help me with it?
Thank you.
Best, Nina
Your answer
Follow this Question
Related Questions
Problems in Android build when running game 1 Answer
Running application in Android: the file mainData is corrupted 0 Answers
Unity 5.3.1f1 android switch platform 2 Answers
How to simulate a real like sky diving physics in unity 5.3? 0 Answers
Android app: IsolatedStorageException when reading from file 1 Answer