Exemple #1
0
    void DrawToolbar()
    {
        defaultColor = GUI.backgroundColor;
        EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
        if (GUILayout.Button("Clear", EditorStyles.toolbarButton, GUILayout.Width(40)))
        {
            DebugPanel.Clear();
        }
        if (GUILayout.Button("Show all", EditorStyles.toolbarButton, GUILayout.Width(60)))
        {
            keys = new List <string>(categories.Keys);
            foreach (string key in keys)
            {
                categories[key] = true;
            }
        }
        if (GUILayout.Button("Hide all", EditorStyles.toolbarButton, GUILayout.Width(60)))
        {
            keys = new List <string>(categories.Keys);
            foreach (string key in keys)
            {
                categories[key] = false;
            }
        }
        GUILayout.FlexibleSpace();

        if (ignoreDeflog)
        {
            GUI.backgroundColor = Color.Lerp(defaultColor, Color.black, 0.3f);
        }
        if (GUILayout.Button("Ignore Deflog", EditorStyles.toolbarButton, GUILayout.Width(80)))
        {
            ignoreDeflog = !ignoreDeflog;
        }
        GUI.backgroundColor = defaultColor;

        if (viewportSettings)
        {
            GUI.backgroundColor = Color.Lerp(defaultColor, Color.black, 0.3f);
        }
        if (GUILayout.Button("Viewport Settings", EditorStyles.toolbarButton, GUILayout.Width(100)))
        {
            viewportSettings = !viewportSettings;
        }
        GUI.backgroundColor = defaultColor;

        EditorGUILayout.EndHorizontal();
    }
    void CreateUI()
    {
        Vector2 controlButtonSize = new Vector2(60, 60);

        // Root canvas
        GameObject root = new GameObject();

        root.transform.name   = "ViewportDebugPanel";
        root.transform.parent = transform;
        root.transform.SetSiblingIndex(0);
        Canvas rootCanvas = root.AddComponent <Canvas>();

        rootCanvas.sortingOrder = 32767;
        rootCanvas.renderMode   = RenderMode.ScreenSpaceOverlay;
        rootCanvas.pixelPerfect = true;
        CanvasScaler scaler = root.AddComponent <CanvasScaler> ();

        scaler.uiScaleMode            = CanvasScaler.ScaleMode.ScaleWithScreenSize;
        scaler.screenMatchMode        = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
        scaler.matchWidthOrHeight     = 0.7f;
        scaler.referencePixelsPerUnit = 100;
        rootGroup = root.AddComponent <CanvasGroup> ();
        root.AddComponent <GraphicRaycaster> ();

        // Panel
        GameObject panel = new GameObject();

        panel.name             = "Panel";
        panel.transform.parent = root.transform;
        ContentSizeFitter csFitter = panel.AddComponent <ContentSizeFitter> ();

        csFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
        VerticalLayoutGroup vLayout = panel.AddComponent <VerticalLayoutGroup> ();

        vLayout.childForceExpandWidth  = true;
        vLayout.childForceExpandHeight = true;
        vLayout.padding = new RectOffset(0, (int)controlButtonSize.x + 5, 0, 0);
        Image background = panel.AddComponent <Image> ();

        background.sprite = null;
        background.color  = DebugPanel.main.bgColor;
        panelGroup        = panel.AddComponent <CanvasGroup> ();
        RectTransform rect = (RectTransform)panel.transform;

        rect.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0);
        rect.anchorMin        = new Vector2(0, 1);
        rect.anchorMax        = new Vector2(1, 1);
        rect.pivot            = new Vector2(0, 1);
        rect.anchoredPosition = new Vector2(0, 0);

        // Categories Layout
        GameObject categoriesLayout = new GameObject();

        categoriesLayout.name             = "Categories";
        categoriesLayout.transform.parent = panel.transform;
        GridLayoutGroup gLayout = categoriesLayout.AddComponent <GridLayoutGroup>();

        gLayout.padding      = new RectOffset(5, 5, 5, 5);
        gLayout.cellSize     = new Vector2(100, 40);
        gLayout.spacing      = new Vector2(4, 4);
        categoriesRect       = (RectTransform)gLayout.transform;
        categoriesRect.pivot = new Vector2(0, 1);

        // Fields Layout
        GameObject fieldsLayout = new GameObject();

        fieldsLayout.name             = "Fields";
        fieldsLayout.transform.parent = panel.transform;
        vLayout         = fieldsLayout.AddComponent <VerticalLayoutGroup>();
        vLayout.padding = new RectOffset(5, 5, 0, 5);
        vLayout.spacing = 3;
        vLayout.childForceExpandWidth  = false;
        vLayout.childForceExpandHeight = false;
        fieldsRect       = (RectTransform)vLayout.transform;
        fieldsRect.pivot = new Vector2(0, 1);

        // Controls Layout
        GameObject controlsLayout = new GameObject();

        controlsLayout.name             = "Controls";
        controlsLayout.transform.parent = root.transform;
        vLayout                        = controlsLayout.AddComponent <VerticalLayoutGroup> ();
        vLayout.padding                = new RectOffset(5, 5, 5, 5);
        vLayout.spacing                = 3;
        vLayout.childAlignment         = TextAnchor.UpperRight;
        vLayout.childForceExpandHeight = false;
        vLayout.childForceExpandWidth  = false;
        csFitter                       = controlsLayout.AddComponent <ContentSizeFitter> ();
        csFitter.verticalFit           = ContentSizeFitter.FitMode.PreferredSize;
        csFitter.horizontalFit         = ContentSizeFitter.FitMode.PreferredSize;
        rect                  = (RectTransform)vLayout.transform;
        rect.pivot            = new Vector2(1, 1);
        rect.anchorMin        = new Vector2(1, 1);
        rect.anchorMax        = new Vector2(1, 1);
        rect.anchoredPosition = new Vector2(0, 0);

        // Lock Button
        Button button = CreateButton("Lock", "LOCK", new Color(1, 1, 1, 0.5f));

        button.transform.SetParent(controlsLayout.transform);
        rect       = (RectTransform)button.transform;
        rect.pivot = new Vector2(1, 1);
        LayoutElement lElement = button.gameObject.AddComponent <LayoutElement> ();

        lElement.minWidth  = controlButtonSize.x;
        lElement.minHeight = controlButtonSize.y;
        button.onClick.AddListener(() => Lock());

        // Clear Button
        button = CreateButton("Clear", "CLEAR", new Color(1, 1, 1, 0.5f));
        button.transform.SetParent(controlsLayout.transform);
        clearButton        = (RectTransform)button.transform;
        clearButton.pivot  = new Vector2(1, 1);
        lElement           = button.gameObject.AddComponent <LayoutElement> ();
        lElement.minWidth  = controlButtonSize.x;
        lElement.minHeight = controlButtonSize.y;
        button.onClick.AddListener(() => DebugPanel.Clear());

        // Ignore Deflog Button
        button          = CreateButton("IgnoreDefLog", "IGNORE\nDEFLOG", new Color(1, 1, 1, 0.5f));
        ignoreButtonImg = button.gameObject.GetComponent <Image> ();
        button.transform.SetParent(controlsLayout.transform);
        ignoreButton       = (RectTransform)button.transform;
        ignoreButton.pivot = new Vector2(1, 1);
        lElement           = button.gameObject.AddComponent <LayoutElement> ();
        lElement.minWidth  = controlButtonSize.x;
        lElement.minHeight = controlButtonSize.y;
        button.onClick.AddListener(() => IngorDefLog(!DebugPanel.main.ignoreDefLog));

        // Show-all Button
        button = CreateButton("ShowAll", "SHOW\nALL", new Color(1, 1, 1, 0.5f));
        button.transform.SetParent(controlsLayout.transform);
        showAllButton       = (RectTransform)button.transform;
        showAllButton.pivot = new Vector2(1, 1);
        lElement            = button.gameObject.AddComponent <LayoutElement> ();
        lElement.minWidth   = controlButtonSize.x;
        lElement.minHeight  = controlButtonSize.y;
        button.onClick.AddListener(() => DebugPanel.TurnAll(true));

        // Hide-all Button
        button = CreateButton("HideAll", "HIDE\nALL", new Color(1, 1, 1, 0.5f));
        button.transform.SetParent(controlsLayout.transform);
        hideAllButton       = (RectTransform)button.transform;
        hideAllButton.pivot = new Vector2(1, 1);
        lElement            = button.gameObject.AddComponent <LayoutElement> ();
        lElement.minWidth   = controlButtonSize.x;
        lElement.minHeight  = controlButtonSize.y;
        button.onClick.AddListener(() => DebugPanel.TurnAll(false));

        Canvas.ForceUpdateCanvases();
    }