Skip to content

Instantly share code, notes, and snippets.

@ginxx009
Created December 14, 2017 05:06
Show Gist options
  • Save ginxx009/8d423ea56f1bdb284b825e56721a4ae4 to your computer and use it in GitHub Desktop.
Save ginxx009/8d423ea56f1bdb284b825e56721a4ae4 to your computer and use it in GitHub Desktop.
class CommandLine{
static string CurrentDate = "";
const string szReleasePath = "../RELEASE/ANDROID/GOOGLEPLAY/";
private static string MoveResource(string szOrigin, string szDest){
string szApplicationDataPath = Application.dataPath;
if(szOrigin.Contains(szApplicationDataPath)){
szOrigin = szOrigin.Replace(szApplicationDataPath, "Assets");
}
string szDestFullPath;
if(szDest.Contains(szApplicationDataPath)){
szDestFullPath = szDest;
szDest = szDest.Replace(szApplicationDataPath, "Assets");
} else {
szDestFullPath = szDest.Replace("Assets/", szApplicationDataPath + "/");
}
//Create folder with szDestFullPath
bool bCreate = hsFileIO.CreateFolder(szDestFullPath);
if(bCreate){
AssetDatabase.Refresh();
}
if(hsFileIO.IsExistPath(szDestFullPath)){
hsFileIO.DeleteFolder(szDestFullPath);
}
string szError = AssetDatabase.MoveAsset(szOrigin, szDest);
return szError;
}
[MenuItem("Build / Build Version / definition.......IN_ONLINE_;_IN_DEV_", false, 190)]
static void Change_Define_DevSymbol_1(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_ONLINE_;_IN_DEV_");
}
[MenuItem("Build / Build Version / definition......_IN_ONLINE_", false, 191)]
static void Change_Define_Symbol_1(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_ONLINE_");
}
[MenuItem("Build / Build Version / Build Online Version", false, 192)]
static void Build_Window_Online_Release(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_ONLINE_;_IN_DEV_");
Build_Console("PC", BuildOptions.None);
}
[MenuItem("Build / Build Version / Build Online Version", false, 193)]
static void Build_Window_Online_Release_2(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_ONLINE_");
Build_Console("PC", BuildOptions.None);
}
[MenuItem("Build / Build Version / definition......_IN_CONSOLE_;_IN_DEV_", false, 212)]
static void Change_Define_DevSymbol_2(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_CONSOLE_;_IN_DEV_");
}
[MenuItem("Build / Build Version / definition......_IN_CONSOLE_", false, 212)]
static void Change_Define_Symbol_2(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_CONSOLE_");
}
[MenuItem("Build / Build Version / Build Console Version")]
static void Build_Window(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_CONSOLE_;_IN_DEV_");
Build_Console("AC", BuildOptions.None);
}
[MenuItem("Build / Build Version / Build Console Version")]
static void Build_Window_2(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, "_IN_CONSOLE_");
Build_Console("AC", BuildOptions.None);
}
//Build version for Android
[MenuItem("Build / Build Version / Mobile Android (_IN_ANDROID_)", false, 215)]
static void Build_Android(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, "_IN_ANDROID_");
Build_Android_Release ("MA", BuildOptions.None);
}
//Build version for IOS
[MenuItem("Build / Build Version / Mobile IOS (_IN_IOS_)", false, 216)]
static void Build_IOS(){
PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, "_IN_IOS_");
Build_Console("MI", BuildOptions.None);
}
private static string[] FindEnabledEditorScenes(){
// Get a list of scenes registered in BuildSettings.
List<string> editorScene = new List<string>();
foreach(EditorBuildSettingsScene scene in EditorBuildSettings.scenes){
if(!scene.enabled) continue;
editorScene.Add(scene.path);
}
return editorScene.ToArray();
}
public static void Build_Console(string ext, BuildOptions buildoptions, bool auto_resource = true){
if(auto_resource){
// Move the resource from Editor /
// Resources to the Resources folder according to the Define symbol (or string ext).
switch(ext){
case "PC":
FileUtil.MoveFileOrDirectory("Assets/Editor/Resources/PC_VERSION", "Assets/Resources/PC_VERSION");
FileUtil.MoveFileOrDirectory("Assets/Editor/Resources/PC_VERSION.meta", "Assets/Resources/PC_VERSION.meta");
AssetDatabase.Refresh();
break;
case "AC":
FileUtil.MoveFileOrDirectory("Assets/Editor/Resources/AC_VERSION", "Assets/Resources/AC_VERSION");
FileUtil.MoveFileOrDirectory("Assets/Editor/Resources/AC_VERSION.meta", "Assets/Resources/AC_VERSION.meta");
AssetDatabase.Refresh();
break;
default:
Debug.LogWarningFormat("{0} There is no definition for :", ext);
return;
}
}
// Remember state
BuildTarget bBuildTarget = EditorUserBuildSettings.activeBuildTarget;
// Actual build scene
string[] scenes = FindEnabledEditorScenes();
try{
MakeCurrentDate();
// Rebuild asset bundle files
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone, BuildTarget.StandaloneWindows);
PlayerSettings.colorSpace = ColorSpace.Linear;
//ApiCompatibilityLevel = obsolete
PlayerSettings.SetApiCompatibilityLevel(BuildTargetGroup.Standalone,ApiCompatibilityLevel.NET_2_0);
string exedataPath = Application.dataPath;
string szStoreIdentity = "/WINDOWS/";
string[] arSplithDataPath = Application.dataPath.Split('/');
if(arSplithDataPath.Length == 1){
arSplithDataPath = Application.dataPath.Split('\\');
}
string szProjectFolderName = "";
if(arSplithDataPath.Length > 1){
szProjectFolderName = arSplithDataPath[arSplithDataPath.Length - 2];
}
m_szapkFileName = exedataPath.Replace(szProjectFolderName + "/Assets", "RELEASE/CONSOLE" + szStoreIdentity + ext + "_" + CurrentDate + "/" + eBuildInfo.ProductName + ".exe");
string szBuildResult = BuildPipeline.BuildPlayer(scenes, m_szapkFileName, BuildTarget.StandaloneWindows, buildoptions);
string buildTypeStr = "Build the Console Version";
if(szBuildResult.Length > 0){
throw new Exception(buildTypeStr + "Build Failure: " + szBuildResult);
} else {
hsDebug.Log(buildTypeStr + "Build Success: " + m_szapkFileName, WW.debug.cat.FSM_CHANGE);
}
}
catch (Exception e){
hsDebug.LogError("Build Failed : " + e);
}
finally{
if(auto_resource){
//Folders moved to Resources
switch(ext){
case "PC":
FileUtil.MoveFileOrDirectory("Assets/Resources/PC_VERSION", "Assets/Editor/Resources/PC_VERSION");
FileUtil.MoveFileOrDirectory("Assets/Resources/PC_VERSION.meta", "Assets/Editor/Resources/PC_VERSION.meta");
AssetDatabase.Refresh();
break;
case "AC":
FileUtil.MoveFileOrDirectory("Assets/Resources/AC_VERSION", "Assets/Editor/Resources/AC_VERSION");
FileUtil.MoveFileOrDirectory("Assets/Resources/AC_VERSION.meta", "Assets/Editor/Resources/AC_VERSION.meta");
AssetDatabase.Refresh();
break;
default:
Debug.LogWarningFormat("{0} Theres is no definition for :", ext);
break;
}
}
}
}
static string m_szapkFileName;
static string m_szBackupApkFilename;
public static void Build_Android_Release(string ex, BuildOptions eCVC, bool bRelease = true){
if(bRelease){
switch (ex) {
case "MA":
FileUtil.MoveFileOrDirectory("Assets/Editor/Resources/MC_VERSION", "Assets/Resources/MC_VERSION");
FileUtil.MoveFileOrDirectory("Assets/Editor/Resources/MC_VERSION.meta", "Assets/Resources/MC_VERSION.meta");
AssetDatabase.Refresh();
break;
default:
Debug.LogWarningFormat("{0} There is no definition for :", ex);
return;
}
}
string buildTypeStr = "";
string szBuildResult = "";
int qualityLevel = QualitySettings.GetQualityLevel();
//string KSN = "/user.keystore";
//string KSP = "d3rbyH0ld3mT3x4s";
//string KAN = "DerbyHoldemTexas";
//string KAP = "d3rbyH0ld3mT3x4s";
try
{
MakeCurrentDate();
//Sign-in to the keystore before anything else (presigned)
//PlayerSettings.Android.keystoreName = Application.dataPath + KSN;
//PlayerSettings.Android.keystorePass = KSP;
//PlayerSettings.Android.keyaliasName = KAN;
//PlayerSettings.Android.keyaliasPass = KAP;
//----------------------------------------
//Rebuild asset bundle files
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
//set the screen for android
Screen.orientation = ScreenOrientation.LandscapeLeft;
//#if UNITY_ANDROID
//Setup Graphics for Android only!
QualitySettings.antiAliasing = 0;
Application.targetFrameRate = 30;
QualitySettings.vSyncCount = 0;
if(qualityLevel == 0){
QualitySettings.shadowCascades = 0;
QualitySettings.shadowDistance = 15;
} else if(qualityLevel == 5){
QualitySettings.shadowCascades = 2;
QualitySettings.shadowDistance = 70;
}
Screen.sleepTimeout = SleepTimeout.NeverSleep;
//End
//#endif
PlayerSettings.use32BitDisplayBuffer = false;
PlayerSettings.accelerometerFrequency = 0;
PlayerSettings.runInBackground = true;
PlayerSettings.captureSingleScreen = true;
PlayerSettings.statusBarHidden = true;
//set graphic to open GLES
PlayerSettings.SetGraphicsAPIs (BuildTarget.Android, new [] {UnityEngine.Rendering.GraphicsDeviceType.OpenGLES2});
//Multi- threading rendering
PlayerSettings.mobileMTRendering = true;
PlayerSettings.MTRendering = true;
//PlayerSettings.useAnimatedAutorotation = true;
PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft; // UIOrientation.AutoRotation;
//PlayerSettings.allowedAutorotateToPortrait = true;
//PlayerSettings.allowedAutorotateToPortraitUpsideDown = true;
//PlayerSettings.allowedAutorotateToLandscapeLeft = false;
//PlayerSettings.allowedAutorotateToLandscapeRight = false;
PlayerSettings.colorSpace = ColorSpace.Gamma;
//-----------------------------------------------------------------------------------------------------------------------
//actual build scene
string[]scenes = FindEnabledEditorScenes();
string apkdataPath = Application.dataPath;
const string szWebApkName = eBuildInfo.ProductName;
const string szApkExt = ".apk"; //this will be the extension of the conversion
string szStoreIdentity = "GOOGLEPLAY";
m_szapkFileName = szWebApkName + "_";
m_szBackupApkFilename = szWebApkName + "_";
string szProjectFolderName = "";
string[] arSplitDataPath = Application.dataPath.Split ('/');
if(arSplitDataPath.Length == 1){
arSplitDataPath = Application.dataPath.Split ('\\');
}
if(arSplitDataPath.Length > 1){
szProjectFolderName = arSplitDataPath [arSplitDataPath.Length - 2];
}
m_szapkFileName = apkdataPath.Replace (szProjectFolderName + "/Assets", "RELEASE/ANDROID/" + szStoreIdentity + "/" + szWebApkName + "_" + CurrentDate + "_" + szApkExt);
m_szBackupApkFilename = apkdataPath.Replace (szProjectFolderName + "/Assets", "BACKUP/ANDROID/" + szStoreIdentity + "/" + szWebApkName + "_" + CurrentDate + "_" + szApkExt);
//bool bBuildResult = false;
string m_szapkDebugFilename = m_szapkFileName.Replace (".apk", "_debug.apk");
try{
if(bRelease) {
buildTypeStr = "Release";
BuildPipeline.BuildPlayer(scenes,m_szapkFileName,BuildTarget.Android,eCVC);
} else {
buildTypeStr = "Debug";
BuildPipeline.BuildPlayer(scenes,m_szBackupApkFilename,BuildTarget.Android,BuildOptions.AllowDebugging | BuildOptions.Development);
}
} catch(Exception exception){
throw new Exception (buildTypeStr + "Build Failed : " + exception);
}
if (szBuildResult.Length > 0) {
throw new Exception (buildTypeStr + " Build Failed: " + szBuildResult);
//return;
} else {
if (bRelease) {
hsDebug.Log (buildTypeStr + "Build Success" + m_szapkFileName, WW.debug.cat.FSM_CHANGE);
} else {
hsDebug.Log (buildTypeStr + "Build Success" + m_szBackupApkFilename, WW.debug.cat.FSM_CHANGE);
}
//bBuildResult = true;
}
}
catch(Exception exce){
hsDebug.Log (buildTypeStr + "Build Failed" + exce);
}
finally{
if(bRelease){
switch(ex){
case "MA":
FileUtil.MoveFileOrDirectory("Assets/Resources/MC_VERSION","Assets/Editor/Resources/MC_VERSION");
FileUtil.MoveFileOrDirectory("Assets/Resources/MC_VERSION.meta", "Assets/Editor/Resources/MC_VERSION.meta");
AssetDatabase.Refresh();
break;
default:
Debug.LogWarningFormat("{0} There is no definition for :", ex);
break;
}
}
}
}
static void AndroidManifestChanger(){
string storename = "";
string strDefaultLoadPath = "";
CXmlDataAccesor.GetDefaultLoadingXmlPath (out strDefaultLoadPath, false);
string szFileFullPath_Manifest = "";
szFileFullPath_Manifest = strDefaultLoadPath + "StoreCategorise\\{0}\\AndroidManifest.xml";
szFileFullPath_Manifest = string.Format (szFileFullPath_Manifest, storename);
string szAfterManifestFolder = strDefaultLoadPath + "Assets\\Plugins\\Android\\AndroidManifest.xml";
//if the files doesn't exist
if (!hsFileIO.IsExistFile (szFileFullPath_Manifest)) {
Debug.LogError (szFileFullPath_Manifest + "- none");
return;
} else {
if(hsFileIO.IsExistFile(szAfterManifestFolder))
hsFileIO.DeleteFileName(szAfterManifestFolder);
hsFileIO.CopyFile (szFileFullPath_Manifest, szAfterManifestFolder);
}
string szFileFullPath_Jar = "StoreCategorize\\PluginsLibsRtoran\\";
string szAfterJarFolder = "Assets\\Plugins\\Android\\";
//Delete the file if there is an existing
for (int i = 0; i < eBuildInfo.RtoranLibFileName.Length; i++)
hsFileIO.DeleteFileName (szAfterJarFolder + "libs\\" + eBuildInfo.RtoranLibFileName[i]);
//Copy File
for (int i = 0; i < eBuildInfo.RtoranLibFileName.Length; i++) {
string _filename = eBuildInfo.RtoranLibFileName [i];
hsFileIO.CopyFile (szFileFullPath_Jar + _filename, szAfterJarFolder + "libs\\" + _filename);
}
//Copy File
for (int i = 0; i < eBuildInfo.IconFileName.Length; i++) {
string _filename = eBuildInfo.IconFileName [i];
hsFileIO.CopyFile (szFileFullPath_Jar + _filename, "Assets\\UIResources\\System - Non Atlas\\" + _filename);
}
}
private static string UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetString(characters);
}
private static byte[] StringToUTF8ByteArray(String pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
return encoding.GetBytes(pXmlString);
}
public static void MakeCurrentDate()
{
CurrentDate = DateTime.Now.ToString("yyyy_MM_dd_HHmm");
}
public static void CreateFolder(string sourceFilename)
{
string szDestPath = Path.GetDirectoryName(sourceFilename);
if (!Directory.Exists(szDestPath))
{
UnityEngine.Debug.Log("Create a folder if there is none : " + szDestPath + "\nsourceFilename : " + sourceFilename);
Directory.CreateDirectory(szDestPath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment