본문 바로가기
■Unity Engine/Editor

[Unity] [UnityEditor -2] UnityEditor.Selection Class에 대해서

by Popbox 2018. 10. 31.
반응형

 UnityEditor.Selection Class에 대해서

 

 

 

 

Selection Class

 

 

개발중에 Animator Component가 붙어있는 오브젝트를 선택했을 때, Animation Editor Window를 보면 해당 Animator의 AnimationController에 대한 

편집모드가 자동으로 열리는 것을 볼 수 있습니다. 


Editor에 대해 관심을 가지기 전에는 그냥 "아~ 편리하네"라고 생각만 하고 넘겼던 기능이지만..

Editor를 만들다 보니 "이런 것을 어떻게 만들지?" 라는 생각이 먼저 들었습니다.


여기서 알게된 클래스가 UnityEditor namespace의 Selection Class입니다.

Selection ClassEditor에서(Project모든 파일) 선택된 항목에 접근을 해주는 클래스입니다.



[공식 Documentation]

https://docs.unity3d.com/ScriptReference/Selection.html



[Static Properties] (갱신 중......)

 activeContext 

  Returns the current context object, as was set via SetActiveObjectWithContext.

 activeGameObject

  Returns the active game object. (The one shown in the inspector).

 activeInStanceID

  Returns the instanceID of the actual object selection. Includes prefabs, non-modifiable objects.

 activeObject

  Returns the actual object selection. Includes prefabs, non-modifiable objects.

 activeTransform

  Returns the active transform. (The one shown in the inspector).

 assetGUIIDs

  Returns the guids of the selected assets. 

 gameObjects

  실제 게임 인스턴스를 반환, prefabs, non-modifiable 오브젝트들 포함

 instanceIDs

  The actual unfiltered selection from the Scene returned as instance ids instead of objects. (Scene에서 선택된(다중) 오브젝트들의 instanceID)

 objects

  The actual unfiltered selection from the Scene. (Scene에서 선택된(다중) 오브젝트들 반환) 

 selectionChanged

  selection change에 변경시 트리거 되는 delegate callback

 transforms

  프리팹을 제외한 최상위 레벨의 오브젝트를 선택한다. (Hierarchy에 존재하는 계층형 오브젝트 선택시 최상위 부모(root)를 반환










[1] 오브젝트를 클릭하면, Log를 띄우는 스크립트를 작성해 봤습니다.

 
1
2
3
4
5
  // Has a GameObject been selection?
    public void OnSelectionChange()
    {
        Debug.Log(Selection.activeGameObject.name);
    }
cs


여기서 주목할 것이 OnSelectionChange() 콜백 함수입니다.

말 그대로 Selection에 대해서 변경이 일어나면 콜이 되는 함수입니다.

UnityEditor 네임스페이스를 이용시 사용 가능합니다.






[2] Hierarchy에서 Walls란 오브젝트를 클릭하면 아래처럼 로그가 뜨는 것을 확인할 수 있습니다.




 




결론적으로,

Selection Class를 이용하면 현재 선택된 객체의 정보를 얻을 수 있다는 걸 알 수 있었습니다.

 

 

 

 감사합니다. 공감 한번 부탁드려요.

 


반응형

'■Unity Engine > Editor' 카테고리의 다른 글

[Unity] [UnityEditor -1] UnityEditor 공부시작  (0) 2018.10.31

댓글