Posts 유니티 에디터 - 게임 뷰 객체 찾는 코드
Post
Cancel

유니티 에디터 - 게임 뷰 객체 찾는 코드

Note


  • 흔히 게임 뷰라고 알려져 있는 에디터 윈도우는 UnityEditor.PlayModeView 타입이다.
  • 리플렉션을 통해 접근할 수 있다.


Source Code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
private static Type GameViewType
{
    get
    {
        if (gameViewType == null)
        {
            gameViewType =
                AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(ass => ass.GetTypes())
                .Where(t => t.Namespace == "UnityEditor" && t.Name == "PlayModeView")
                .FirstOrDefault();

            if (gameViewType == null)
            {
                throw new Exception("UnityEditor.PlayModeView does NOT exist in current version.");
            }
        }
        return gameViewType;
    }
}
private static Type gameViewType;


/// <summary> 열려 있는 게임 뷰 하나 찾아서 반환 </summary>
private static EditorWindow GameView
{
    get
    {
        if (_gameView != null)
            return _gameView;

        UnityEngine.Object[] gameViews = Resources.FindObjectsOfTypeAll(GameViewType);
        if (gameViews.Length > 0)
        {
            _gameView = gameViews[0] as EditorWindow;
            return _gameView;
        }

        return null;
    }
}
private static EditorWindow _gameView;


/// <summary> 현재 포커스된 게임 뷰 반환 </summary>
private static EditorWindow ActiveGameView
{
    get
    {
        if (_activeGameView != null && _activeGameView.hasFocus)
            return _activeGameView;
        
        UnityEngine.Object[] gameViews = Resources.FindObjectsOfTypeAll(GameViewType);
        for (int i = 0; i < gameViews.Length; i++)
        {
            EditorWindow current = gameViews[i] as EditorWindow;
            if (current != null && current.hasFocus)
            {
                _activeGameView = current;
                return _activeGameView;
            }
        }

        return null;
    }
}
private static EditorWindow _activeGameView;


References


This post is licensed under CC BY 4.0 by the author.