我需要使用脚本迭代我的所有游戏场景并进行一些更改。我需要在游戏编译之前完成这项工作。
我尝试在Unity中使用谷歌预处理脚本。我是否正确理解我需要使用类似BuildPipeline的东西?是否有任何简单的例子可以理解它是如何工作的?
答案 0 :(得分:2)
好吧,最后我设法自己做了。如果有人会处理同样的问题,这是我的代码:
class MyBuilder : Editor {
private static string[] FillLevels ()
{
return (from scene in EditorBuildSettings.scenes where scene.enabled select scene.path).ToArray ();
}
[MenuItem ("MyTool/BuildGame")]
public static void buildGame()
{
string[] levels = FillLevels ();
foreach (string level in levels) {
EditorApplication.OpenScene (level);
object[] allObjects = Resources.FindObjectsOfTypeAll (typeof(UnityEngine.GameObject));
foreach (object thisObject in allObjects) {
/* my gameObjects changing before compilation */
}
EditorApplication.SaveScene ();
}
BuildPipeline.BuildPlayer (levels, Path.GetFullPath (Application.dataPath + "/../game.exe"), BuildTarget.StandaloneWindows, BuildOptions.None);
}
}
答案 1 :(得分:1)
您想要创建Editor Script。使用API参考链接,您应该能够在场景中执行任何操作。下面的脚本将允许您保存这些更改。
这是一个编辑脚本示例,可让您保存对场景的更改,here:
import UnityEditor;
class SimpleAutoSave extends EditorWindow
{
var saveTime : float = 300;
var nextSave : float = 0;
@MenuItem("Example/Simple autoSave")
static function Init()
{
var window : SimpleAutoSave = EditorWindow.GetWindowWithRect(SimpleAutoSave, Rect(0,0,165,40));
window.Show();
}
function OnGUI()
{
EditorGUILayout.LabelField("Save Each:", saveTime + " Secs");
var timeToSave : int = nextSave - EditorApplication.timeSinceStartup;
EditorGUILayout.LabelField("Next Save:", timeToSave.ToString() + " Sec");
this.Repaint();
if(EditorApplication.timeSinceStartup > nextSave)
{
var path : String [] = EditorApplication.currentScene.Split(char.Parse("/"));
path[path.Length -1] = "AutoSave_" + path[path.Length-1];
EditorApplication.SaveScene(String.Join("/",path), true);
Debug.Log("Saved Scene");
nextSave = EditorApplication.timeSinceStartup + saveTime;
}
}
}