Last active
March 7, 2018 09:29
-
-
Save fuqunaga/d75c9ac1ff22122ba9c3e4970c3689b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditorInternal; | |
using System.Linq; | |
public class TwinBuild : EditorWindow | |
{ | |
public const string configKey = "TwinBuildReimportAsset"; | |
public const char configKeySeparator = ','; | |
static List<Object> reimportAssets = new List<Object>(); | |
#region Window | |
static ReorderableList reimportAssetsList; | |
[MenuItem("Build/TwinBuildWindow")] | |
static void Init() | |
{ | |
var window = GetWindow<TwinBuild>(); | |
window.Show(); | |
} | |
void LoadConfig() | |
{ | |
var configStr = EditorUserSettings.GetConfigValue(configKey); | |
if (!string.IsNullOrEmpty(configStr)) | |
{ | |
reimportAssets = configStr | |
.Split(configKeySeparator) | |
.Select(path => AssetDatabase.LoadMainAssetAtPath(path)) | |
.Where(obj => obj != null) | |
.ToList(); | |
} | |
} | |
void SaveConfig() | |
{ | |
var configStr = string.Join(configKeySeparator.ToString(), reimportAssets.Select(obj => AssetDatabase.GetAssetPath(obj)).ToArray()); | |
EditorUserSettings.SetConfigValue(configKey, configStr); | |
} | |
private void OnEnable() | |
{ | |
LoadConfig(); | |
if (reimportAssetsList == null) | |
{ | |
reimportAssetsList = new ReorderableList(reimportAssets, typeof(Object)); | |
reimportAssetsList.drawElementCallback = (rect, index, isActive, isFocused) => | |
{ | |
rect.height -= 4; | |
rect.y += 2; | |
var asset = reimportAssets[index]; | |
var newAsset = EditorGUI.ObjectField(rect, asset, typeof(Object), false); | |
if (newAsset != asset) | |
{ | |
reimportAssets[index] = newAsset; | |
} | |
; | |
}; | |
} | |
} | |
private void OnDisable() | |
{ | |
SaveConfig(); | |
} | |
private void OnGUI() | |
{ | |
EditorGUILayout.PrefixLabel("ReimportAssets"); | |
reimportAssetsList.DoLayoutList(); | |
using (var h = new GUILayout.HorizontalScope()) | |
{ | |
if (GUILayout.Button("Build Release")) BuildRelease(); | |
if (GUILayout.Button("Build Dev")) BuildDev(); | |
} | |
} | |
#endregion | |
[MenuItem("Build/TwinBuild_Release")] | |
public static void BuildRelease() | |
{ | |
Build(BuildOptions.None); | |
} | |
[MenuItem("Build/TwinBuild_Dev")] | |
public static void BuildDev() | |
{ | |
Build(BuildOptions.Development, "Dev"); | |
} | |
protected static void Build(BuildOptions options, string postFix = "") | |
{ | |
ReimportAsset(); | |
var targetName = $"TwinBuild_{Application.productName}{postFix}"; | |
var folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "/" + targetName; | |
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions(); | |
buildPlayerOptions.scenes = new[] { "Assets/Scenes/main.unity", "Assets/Scenes/sub/online.unity", "Assets/Scenes/sub/offline.unity", }; | |
buildPlayerOptions.locationPathName = $"{folder}/{targetName}0/{targetName}0.exe"; | |
buildPlayerOptions.target = BuildTarget.StandaloneWindows64; | |
buildPlayerOptions.options = options; | |
BuildPipeline.BuildPlayer(buildPlayerOptions); | |
buildPlayerOptions.locationPathName = $"{folder}/{targetName}1/{targetName}1.exe"; | |
BuildPipeline.BuildPlayer(buildPlayerOptions); | |
} | |
static protected void ReimportAsset() | |
{ | |
reimportAssets.ForEach(obj => | |
{ | |
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(obj), ImportAssetOptions.ForceUpdate); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment