1
2

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 1 year has passed since last update.

Unity インスペクターでの設定値を維持したまま変数名を変更する方法

Posted at

0.0 はじめに

publicやSerializeFieldなどでインスペクターから値変更などアクセスが可能ですが、スクリプトで変数の名前を変更すると、デフォルト設定の状態ではインスペクターから値は消えてしまいます。
値の数が少ない場合はまた設定すればよいのですが、大量にある場合や調整済みの値などは再度設定に手間がかかります。
変更後も値を保持するための方法を説明します。

1.0 UnityEngine.Serializationの追加

スクリプトの先頭にあるusing名前空間にUnityEngine.Serializationを追加します。下記にあるFormerlySerializedAsアノテーションを使うために必要です。
image.png

2.0 FormerlySerializedAsアノテーションを挿入

値を保持したい変数の前にFormerlySerializedAsアノテーションを挿入します。FormerlySerializedAs()の()内の引数には保持したい値を持っている変数名(変更前の変数名)を""で囲んで入れます。
下記の例ではbeforeが変更前の変数、afterが変更後の変数です。
image.png

3.0 Unityでビルド

Unityに戻ってビルドしましょう。(Unityのデフォルト設定であればスクリプトを保存してUnity画面をアクティブにすると自動でビルドしてくれます。)
変更された変数に値が保持されているはずです。
この後はFormerlySerializedAsアノテーションを削除しても問題ありません。

1
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?