日期:2014-05-17  浏览次数:21097 次

[C#] 利用部分方法添加调试代码(附录)

项目生成时屏蔽Debug代码的脚本:

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public class BuildPlayer : ScriptableObject
{
	static string rootPath = Application.dataPath + Path.DirectorySeparatorChar;
	static string rootParent = new DirectoryInfo(rootPath).Parent.FullName + Path.DirectorySeparatorChar;
	static string projectName = new DirectoryInfo(rootPath).Parent.Name;
	static string scenePath = Application.dataPath + Path.DirectorySeparatorChar + "Scenes" + Path.DirectorySeparatorChar;
	static string apkNameDebug = rootParent + projectName + "_Debug_[Date].apk";
	static string apkNameRelease = rootParent + projectName + "_Release_[Date].apk";

	[MenuItem("Build Player/Build Android Debug", false, 101)]
	static void BuildAndroidDebug()
	{
		AssetDatabase.Refresh();
		// set build options and build player
		BuildOptions options = BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.ShowBuiltPlayer;
		string[] allScenes = GetAllScenes();
		BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options);
	}

	[MenuItem("Build Player/Build Android Debug(Auto Deploy)", false, 102)]
	static void BuildAndroidDebugAutoDeploy()
	{
		AssetDatabase.Refresh();
		// set build options and build player
		BuildOptions options = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.AllowDebugging;
		string[] allScenes = GetAllScenes();
		BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options);
	}

	[MenuItem("Build Player/Build Android Debug(Auto Deploy And Connect Profiler)", false, 103)]
	static void BuildAndroidDebugAutoDeployAndConnectProfiler()
	{
		AssetDatabase.Refresh();
		// set build options and build player
		BuildOptions options = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.ConnectWithProfiler | BuildOptions.AllowDebugging;
		string[] allScenes = GetAllScenes();
		BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options);
	}

	[MenuItem("Build Player/Build Android Release", false, 201)]
	static void BuildAndroidRelease()
	{
		// rename all debug scripts to *.rem
		RenameDebugScripts();
		// set build options and build player
		BuildOptions options = BuildOptions.ShowBuiltPlayer;
		string[] allScenes = GetAllScenes();
		BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameRelease), BuildTarget.Android, options);
		// restore all debug scripts' file names
		RestoreDebugScripts();
	}

	[MenuItem("Build Player/Build Android Release(Auto Deploy)", false, 202)]
	static void BuildAndroidReleaseAutoDeploy()
	{
		// rename all debug scripts to *.rem
		RenameDebugScripts();
		// set build options and build player
		BuildOptions options = BuildOptions.AutoRunPlayer;
		string[] allScenes = GetAllScenes();
		BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameRelease), BuildTarget.Android, options);
		// restore all debug scripts' file names
		RestoreDebugScripts();
	}

	static void RenameDebugScripts()
	{
		string[] allDebugDirectories = Directory.GetDirectories(rootPath, "_Debug", SearchOption.AllDirectories);
		foreach (string directory in allDebugDirectories)
		{
			string[] scripts = Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories);
			foreach (string script in scripts)
			{
				string newScriptName = script + ".rem";
				File.Move(script, newScriptName);
				File.Delete(script + ".meta");
			}
		}
		AssetDatabase.Refresh();
	}

	static void RestoreDebugScripts()
	{
		string[] allDebugDirectories = Directory.GetDirectories(rootPath, "_Debug", SearchOption.AllDirectories);
		foreach (string directory in allDebugDirectories)
		{
			string[] renamedScripts = Directory.GetFiles(directory,