Last active
September 11, 2019 02:47
-
-
Save kankikuchi/39e6d9c0ff64b26c2a4e3678fc3769d3 to your computer and use it in GitHub Desktop.
Hierarchy上で選択しているGameObjectを固定する【Unity】【エディタ拡張】
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SelectedGameObjectFixer.cs | |
// http://kan-kikuchi.hatenablog.com/entry/SelectedGameObjectFixer | |
// | |
// Created by kan.kikuchi on 2019.08.17. | |
#if UNITY_EDITOR | |
using UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// Hierarchy上で選択しているGameObjectを固定するクラス | |
/// </summary> | |
[ExecuteInEditMode]//ExecuteInEditModeを付ける事でOnEnableやOnDestroyが再生していなくても実行されるようになる | |
public class SelectedGameObjectFixer : MonoBehaviour { | |
//選択を固定する対象 | |
[SerializeField] | |
private GameObject _target = null; | |
//================================================================================= | |
//初期化、破棄 | |
//================================================================================= | |
private void OnEnable() { | |
EditorApplication.update += EditorUpdate; | |
} | |
private void OnDestroy() { | |
EditorApplication.update -= EditorUpdate; | |
} | |
//================================================================================= | |
//更新 | |
//================================================================================= | |
private void EditorUpdate() { | |
//対象が選択されていない時は何もしない | |
if (_target == null) { | |
return; | |
} | |
//自身を選択している時は無効(固定を解除出来ないので) | |
if (Selection.activeGameObject == gameObject) { | |
return; | |
} | |
//強制的に対象を選択し続けるように、ただしProjectのものを選択している時は除く | |
if (Selection.activeGameObject != null || Selection.activeObject == null) { | |
Selection.objects = new Object[]{_target}; | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment