Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
こわくないエディター拡張入門
Search
ゲムシャリコ
October 14, 2023
Technology
0
220
こわくないエディター拡張入門
unity1week online共有会 #13にて発表させていただいたスライドです。
動画等は配信をご確認ください。
https://youtu.be/COFWY3BrDEw?t=2786
ゲムシャリコ
October 14, 2023
Tweet
Share
Other Decks in Technology
See All in Technology
The 5 Obstacles to High-Performing Teams
mdalmijn
0
270
技術負債の「予兆検知」と「状況異変」のススメ / Technology Dept
i35_267
1
1k
WAF に頼りすぎない AWS WAF 運用術 meguro sec #1
izzii
0
460
データ基盤の成長を加速させる:アイスタイルにおける挑戦と教訓
tsuda7
3
650
地方拠点で エンジニアリングマネージャーってできるの? 〜地方という制約を楽しむオーナーシップとコミュニティ作り〜
1coin
1
130
まだ間に合う! エンジニアのための生成AIアプリ開発入門 on AWS
minorun365
PRO
4
580
日経電子版 x AIエージェントの可能性とAgentic RAGによって提案書生成を行う技術
masahiro_nishimi
1
290
Classmethod AI Talks(CATs) #15 司会進行スライド(2025.02.06) / classmethod-ai-talks-aka-cats_moderator-slides_vol15_2025-02-06
shinyaa31
0
170
エンジニアの育成を支える爆速フィードバック文化
sansantech
PRO
3
660
バックエンドエンジニアのためのフロントエンド入門 #devsumiC
panda_program
16
6.5k
2.5Dモデルのすべて
yu4u
2
610
滅・サービスクラス🔥 / Destruction Service Class
sinsoku
6
1.5k
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
It's Worth the Effort
3n
184
28k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
How STYLIGHT went responsive
nonsquared
98
5.3k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
10
1.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
29
1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Building Your Own Lightsaber
phodgson
104
6.2k
Transcript
こわくない エディター拡張入門 ゲムシャリコ 2023/10/14 unity1week 共有会 #13 (お題: 1ボタン)
自己紹介 • 情報系大学 2年生 • ゲームプログラマー志望 • unity1week参加 3回目 @gameshalico
ゲムシャリコ
Orbit 重力2Dアクション 自転に沿って動いたり 公転に沿って動いたり 総合10位(発表時)
エディター拡張? Unityエディターをカスタマイズするもの ヒエラルキーの見た目を整えたり インスペクタにボタンを追加したり
エディター拡張で時短 ちま……ちま…… インスペクターから値を調整 ドラッグ移動で爆速調整
Orbitでの実装 書いたコードはこれだけ!
なんだか難しそう? 見慣れない構文や見たことない関数だらけで 尻込みしてしまいがち? 一回やってみると意外と単純 作業効率UPで作品のクオリティもUP
作ってみよう : 2点間を移動するギミック 本題ではないのでゲーム側の実装は割愛(配布あり)
今回作るもの ハンドルを使って目標点を移動できるようにする
実装 using UnityEditor; using UnityEngine; [CustomEditor(typeof(MoveBetween))] public class MoveBetweenEditor :
Editor { private void OnSceneGUI() { MoveBetween moveBetween = (MoveBetween)target; EditorGUI.BeginChangeCheck(); Vector3 position = Handles.PositionHandle(moveBetween.destination, Quaternion.identity); if(EditorGUI.EndChangeCheck()) { Undo.RecordObject(moveBetween, "Change Destination"); moveBetween.destination = position; } } }
Editorフォルダの作成 「Editor」という名前のフォルダを作成する エディター拡張のScriptはここに入れる Editorフォルダ以下はビルドに含まれない Scripts Editor 別のフォルダとして用意 通常のスクリプト エディター拡張の スクリプト
※ 階層は自由 参考: https://docs.unity3d.com/ja/2023.2/Manual/SpecialFolders.html
using UnityEditor; エディター拡張に関するクラスは UnityEditor名前空間にある。 using UnityEditorを忘れない。 大体のIDEで補完してくれるので こんなのあったな、ぐらいでOK。 ビルドに含まれない コードでのみ利用できる
using UnityEditor; using UnityEngine; クラス等をグループ化して まとめるもの。 各クラスはUnityEditor.Editor のような形で表現できるが、 using で省略できる。 名前空間 参考: https://docs.unity3d.com/ScriptReference/UnityEditor.html
[CustomEditor(typeof(XXX))] このクラスはXXX型のエディター拡張だよ、という宣言 Unity側に教えてあげるイメージ 指定した型の情報を取得する。 型を変数に入れられる形に変換する。 []で囲まれてるやつをAttributeといい、 クラスやメンバーに情報を付加できる。 typeof演算子 Attribute [CustomEditor(typeof(MoveBetween))]
public class MoveBetweenEditor : Editor 参考: https://docs.unity3d.com/ScriptReference/CustomEditor.html
class XXX : Editor MonoBehaviourみたいなもの。 MonoBehaviour(スクリプト)用の エディター拡張のクラスは、 Editorクラスを継承 させる。 [CustomEditor(typeof(MoveBetween))]
public class MoveBetweenEditor : Editor { …… } ScriptableObjectの エディターを拡張するときも Editorクラスを継承して使う。 ScriptableObject Editor MoveBetweenEditor 継承 親 子 参考: https://docs.unity3d.com/ScriptReference/Editor.html
private void OnSceneGUI() { …… } OnSceneGUI() Unityから呼ばれるイベント関数。 Updateみたいなイメージ。 ヒエラルキー上で
選択されているときに呼ばれる。 シーンビューの表示を拡張する。 インスペクターが表示されるときに呼ばれる インスペクターの機能を拡張できる。 UIToolkitではCreateInspectorGUIを使う。 OnInspectorGUI() 2 1 「2」を選択中 「2」のOnSceneGUIが呼ばれている 参考: https://docs.unity3d.com/ScriptReference/Editor.html
this.target 選択している拡張対象のオブジェクト。 Editorクラスのpublic変数。 UnityEngine.Object型で入っているので キャスト(型変換)して使う必要がある。 MoveBetween moveBetween = (MoveBetween)target; 万物の祖。Unity関係のオブジェクトはだ
いたいこいつを継承している。 UnityEngine.Object 「1」を選択中 参考: https://docs.unity3d.com/ja/current/ScriptReference/Editor-target.html
EditorGUI.BeginChangeCheck() EditorGUI.EndChangeCheck() : bool これに囲まれたGUI要素(後述)が 変更されたかを検知する。 EditorGUI.BeginChangeCheck(); …… if(EditorGUI.EndChangeCheck()) {
…… } BeginChangeCheck EndChangeCheck PositionHandle 変更されてる?👀👀 あったよ!(true) or なかったよ!(false) 変更見てるよ! 参考: https://docs.unity3d.com/ja/current/ScriptReference/EditorGUI.BeginChangeCheck.html
Handles.PositionHandle (Vector3 position, Quaternion rotation) : Vector3 移動できるハンドルを制御するGUI要素。 EditorGUI.Begin/EndChangeCheck で囲み、変更を検知する。
Vector3 position = Handles.PositionHandle( moveBetween.destination, Quaternion.identity ); 真ん中の半透明の面を ドラッグすると平面で移動 できるって知ってました? 私は知りませんでした。 参考: https://docs.unity3d.com/ja/current/ScriptReference/Editor-target.html
Undo.RecordObject (Object objectToUndo, string name) アンドゥ(Ctrl+Z)に対応させる。 オブジェクトを変更する直前に呼び出す。 Undo.RecordObject( moveBetween, "Change
Destination“ ); moveBetween.destination = position; Edit>Undo History で確認できる 参考: https://docs.unity3d.com/ja/current/ScriptReference/Undo.RecordObject.html
ね?簡単でしょ? using UnityEditor; using UnityEngine; [CustomEditor(typeof(MoveBetween))] public class MoveBetweenEditor :
Editor { private void OnSceneGUI() { MoveBetween moveBetween = (MoveBetween)target; EditorGUI.BeginChangeCheck(); Vector3 position = Handles.PositionHandle(moveBetween.destination, Quaternion.identity); if(EditorGUI.EndChangeCheck()) { Undo.RecordObject(moveBetween, "Change Destination"); moveBetween.destination = position; } } }
プロジェクト配布 GitHubにて今回の プロジェクトを配布中 今回紹介できなかった MoveBetweenの実装も コメント追記済み https://github.com/gameshalico/unity-custom-editor-sample-move-between
次のステップ • エディター拡張の記事を探して 自分でも実装してみる • 公式のマニュアルを読んでみる • UIToolkitはいいぞ(情報少なめ)
ご清聴ありがとうございました