3
1

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.

Property を Inspector に公開しつつ get だけ使いたい

Posted at

Unity 2021.3.17f1 を使用しています。
(何も作らずに) Inspector へ公開する方法に少し手間取ったので、残します。

方法

[field: SerializeField] public float hp { get; private set; }

SerializeField の先頭に field: を付け、set を private にする。

Header もつけたい

[Header("ステータス")] // エラー! できない!
[field: SerializeField] public float hp { get; private set; }

とすると、「属性 'Header' はこの宣言型では無効です。'フィールド' 宣言でのみ有効です。」とエラーが出ます

[field: SerializeField, Header("ステータス")] public float hp { get; private set; }

対策としては、上記のように SerializeField の後ろに繋げれば使えます。(少し気持ち悪いけど)

参照

https://papersloth.hatenablog.com/entry/2020/05/25/135813
https://qiita.com/RyotaMurohoshi/items/b64b36009ba652b21e2c

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?