1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ScriptableObjectをそのまま別フォルダに上書き移動するAssetPostprocessor

Posted at

テラシュールさんのExcelImporter
【Unity】Excel Importer Maker、xlsxに対応
で出力したデータを、独自のフォルダにコピーしてゲーム中にロードしているのですが、
独自フォルダに自動でコピーするAssetPostprocessorを書いたのでメモしておきます。

using UnityEngine;
using UnityEditor;

namespace Twilight
{

	public class MasterDataOverrider : AssetPostprocessor
	{
		private const string kSrcDirectoryPath = "Assets/App/Data";
		private const string kDestDirectoryPath = "Assets/App/Resources/Data";

		/// <summary>
		/// OnPostprocessAllAssetsには効かないらしいけど一応
		/// </summary>
		/// <returns></returns>
		public override int GetPostprocessOrder()
		{
			return 100;
		}

		static void OnPostprocessAllAssets(
			string[] importedAssets,
			string[] deletedAssets,
			string[] movedAssets,
			string[] movedFromPath)
		{
			foreach (var asset in importedAssets)
			{
				if (!asset.Contains(kSrcDirectoryPath))
				{
					continue;
				}

				var loadAsset = AssetDatabase.LoadAssetAtPath<ScriptableObject>(asset);
				if (loadAsset == null)
				{
					continue;
				}

				var destFilePath = asset.Replace(kSrcDirectoryPath, kDestDirectoryPath);

				AssetDatabase.CopyAsset(asset, destFilePath);
				AssetDatabase.DeleteAsset(asset);
				AssetDatabase.Refresh();
			}
		}
	}
}

使う時は kSrcDirectoryPath、kDestDirectoryPath を、書き換えると良いかと。

※最初にAssetDatabase.CopyAsset()ではなくFile.Copy()でやろうとして時間がかかってしまった;
アセットのコピー(AssetDatabase.CopyAsset VS File.Copy)【Unity】【エディタ拡張】

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?