summaryrefslogtreecommitdiff
path: root/UnityCollection/Assets/Tools/BoneViewer/Editor/BoneViewerEditor.cs
blob: f143670666033734ff86d05bd7d5fc575d448b41 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.EditorTools;

[EditorTool("Bone Viewer", typeof(SkinnedMeshRenderer))]
public class BoneViewerEditor : EditorTool
{
    [SerializeField]
    Texture2D m_ToolIcon;

    GUIContent m_IconContent;

    void OnEnable()
    {
        m_IconContent = new GUIContent()
        {
            image = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/Tools/BoneViewer/Editor/Bone.png"),
            text = "BoneView Tool",
            tooltip = "BoneView Tool"
        };
    }

    public override GUIContent toolbarIcon
    {
        get { return m_IconContent; }
    }

    Rect m_WindowRect = new Rect(10, 30, 200, 0);

    bool m_ShowBones = true;

    Rect m_SceneRect;

    float m_Size = 0.03f;
    float m_Alpha = 0.2f;

    bool m_ShowName = false;

    void WindowFunction(int id)
    {
        Color col = GUI.color;
        if (m_ShowBones)
            GUI.color = Color.gray;
        if (GUILayout.Button("Show Bones"))
        {
            m_ShowBones = !m_ShowBones;
        }
        GUI.color = col;
        GUILayout.BeginHorizontal();
        GUILayout.Label("Size  ");
        m_Size = GUILayout.HorizontalSlider(m_Size, 0.01f, 0.1f);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.Label("Alpha");
        m_Alpha = GUILayout.HorizontalSlider(m_Alpha, 0f, 1f);
        GUILayout.EndHorizontal();

        m_ShowName = GUILayout.Toggle(m_ShowName, "Show Name");
    }

    public override void OnToolGUI(EditorWindow window)
    {
        SceneView scene = window as SceneView;
        if (scene == null)
            return;

        m_SceneRect = new Rect(0,0, scene.position.width, scene.position.height);

        SkinnedMeshRenderer renderer = this.target as SkinnedMeshRenderer;
        if (renderer == null)
            return;

        
        m_WindowRect = GUILayout.Window(1, m_WindowRect, WindowFunction, "Bone Viewer");

        EditorGUILayout.LabelField("TestTestTestTestTestTest");

        EditorGUI.BeginChangeCheck();

        Handles.zTest  = UnityEngine.Rendering.CompareFunction.Always;
        Handles.color = new Color(1, 1, 1, m_Alpha);
        Transform root = renderer.rootBone;
        if(root && m_ShowBones)
            DrawBone(null, root, 0);
    }

    void DrawBone(Transform parent, Transform bone, int depth)
    {
        if (bone == null)
            return;
        Handles.SphereHandleCap(0, bone.position, bone.rotation, m_Size, EventType.Repaint);
        if(parent)
            Handles.DrawLine(parent.position, bone.position);
        if(m_ShowName)
        {
            Handles.Label(bone.position, bone.name);
        }
        int count = bone.childCount;
        for(int i = 0;i < count; ++i)
        {
            DrawBone(bone, bone.GetChild(i), depth + 1);
        }
    }

}