Last active
March 17, 2016 10:36
-
-
Save tsubaki/8173471 to your computer and use it in GitHub Desktop.
特定のプラットフォームでファイルを退避する、Unity Editor Extension
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 UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
using System.Collections.Generic; | |
using System.IO; | |
[InitializeOnLoad] | |
public sealed class SwitchPlatformRemoveFile | |
{ | |
static void SetupItems () | |
{ | |
// ここに移動したいファイルやプラットフォームを記述しておく | |
items.Add ( new Item ("Plugins/file.cs", BuildTarget.Android, BuildTarget.WebPlayer)); | |
} | |
//---------------------------------------------------------------------------- | |
static List<Item> items = new List<Item> (); | |
static SwitchPlatformRemoveFile () | |
{ | |
EditorUserBuildSettings.activeBuildTargetChanged += OnChangedPlatform; | |
SetupItems (); | |
} | |
private static void OnChangedPlatform () | |
{ | |
BuildTarget target = EditorUserBuildSettings.activeBuildTarget; | |
foreach (var item in items) { | |
if (item.removeTargets.FindIndex ((i) => i.Equals (target)) != -1) { | |
string fromFile = "Assets/" + item.fileName; | |
string toFile = "Evacuation/" + item.fileName; | |
MoveFile (fromFile, toFile); | |
} else { | |
string fromFile = "Evacuation/" + item.fileName; | |
string toFile = "Assets/" + item.fileName; | |
MoveFile (fromFile, toFile); | |
} | |
} | |
AssetDatabase.Refresh (ImportAssetOptions.Default); | |
} | |
private static void MoveFile (string fromFilePath, string toFilePath) | |
{ | |
string dir = Path.GetDirectoryName (toFilePath); | |
string metaFromFilePath = fromFilePath + ".meta"; | |
string metaToFilePath = toFilePath + ".meta"; | |
Directory.CreateDirectory (dir); | |
if (File.Exists (fromFilePath)) { | |
var data = File.ReadAllBytes (fromFilePath); | |
File.WriteAllBytes (toFilePath, data); | |
File.Delete (fromFilePath); | |
} | |
if (File.Exists (metaFromFilePath)) { | |
var data = File.ReadAllBytes (metaFromFilePath); | |
File.WriteAllBytes (metaToFilePath, data); | |
File.Delete (metaFromFilePath); | |
} | |
} | |
sealed class Item | |
{ | |
public Item (string fileName, params BuildTarget[] removeTargets) | |
{ | |
this.fileName = fileName; | |
foreach (var target in removeTargets) { | |
this.removeTargets.Add (target); | |
} | |
} | |
public string fileName; | |
public List<BuildTarget> removeTargets = new List<BuildTarget> (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以下の項目を更新して対象のファイルを増減する。例えばこんな感じ。
第二引数以降で指定されたBuildTargetに指定されている場合、フォルダから削除されEvacuationフォルダに行く。逆をするとEvacuationから復帰する。