LoginSignup
0
0

More than 1 year has passed since last update.

Prefabに設定しているデータ構造をプログラムで一括変更して保存する

Posted at

ゲームのバランス調整をしていると、色々仕様を変えたい部分が出てきます。。。

今回の場合、敵モーションのバリエーションを増やしたくなりました。
しかし、敵モーションは「攻撃」「必殺技1」「必殺技2」の3パターンを想定して、
Prefabにそのモーションの設定を個別に設定して実装していました。
(下記のような感じ)

リスト化して増やせるように修正したかったのですが、
既に大量の敵モーションをPrefabに設定してしまっていたので、
手作業で行うのは絶望的でした。

そこで、プログラムでPrefabに設定したデータを移行する処理を書いて一括変更する対応をしました。
移行後は下記のような感じです。

ポイントとしては、

  • 変更対象のPrefabのリストを取得
  • Prefabをロード
  • パラメータ変更の処理
  • Prefabを保存
EnemyMotionEdit.cs
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.IO;

public class EnemyMotionEdit : EditorWindow {
	[MenuItem("Appli/敵モーションパラメーターデータ移行")]
	public static void EnemyMotionParamEdit() {
		List<string> paths = Directory.GetFiles(Application.dataPath + "/AppliResources/AssetBundle/additional/enemy", "*.prefab", SearchOption.AllDirectories).ToList();
		foreach(string path in paths) {
			// Prefabロード
			GameObject contentsRoot = PrefabUtility.LoadPrefabContents(path);

			// モーションパラメータ移行処理
			var motionController = contentsRoot.GetComponent<EnemyMotionController>();
			motionController.MotionParamEdit();

			// 保存
			PrefabUtility.SaveAsPrefabAsset(contentsRoot, path);
			PrefabUtility.UnloadPrefabContents(contentsRoot);
		}
	}
}

EnemyMotionController.cs
	/// <summary>
	/// モーションパラメーター移行処理
	/// </summary>
	public void MotionParamEdit() {
		motionParamList = new List<MotionParam>();
		motionParamList.Add(attackMotionParam);
		motionParamList.Add(special1MotionParam);
		motionParamList.Add(special2MotionParam);
	}

メニューからデータ移行処理実行

0
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
0
0