Search Unity

Repaint a whole Inpector windows

Discussion in 'UI Toolkit' started by NotGoodEnoughh, Sep 6, 2019.

  1. NotGoodEnoughh

    NotGoodEnoughh

    Joined:
    Feb 1, 2018
    Posts:
    35
    I have a custom editor and I wanna to repaint a whole inspector when it's ObjectField has changed a value.
    Code (CSharp):
    1. itemDatabase.RegisterCallback<ChangeEvent<Object>>(evt =>
    2. {
    3.     blueprintDatabase.itemDatabase = evt.newValue as ItemDatabase;
    4.     //do inspector repaint here
    5. });
    Editor.Repaint() won't work, I don't know why
    VisualElement.MarkDirtyRepaint() seems to be a wrong in this case, also it is not working
     
    Last edited: Sep 7, 2019
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hello!

    Could you give more information about what you want to achieve and what are the results you are currently obtaining?
     
  3. NotGoodEnoughh

    NotGoodEnoughh

    Joined:
    Feb 1, 2018
    Posts:
    35
    I'm making a blueprint database custom editor and it needs an item database. I'm setting an item database through an object field. Then I need to show the blueprint database custom editor stuff(when I got an item database set in the object field). Also I'm saving the item database in a blueprint database to load it. Problem is that when I set an item database in the object field blueprint database stuff should show and if the object field is null it won't show anything, just the object field for item database. But it won't Repaint the editor to show those stuff.
     
  4. NotGoodEnoughh

    NotGoodEnoughh

    Joined:
    Feb 1, 2018
    Posts:
    35
    Okay, I wrote some code like my blueprint database editor. Can you please show me what am I doing wrong.

    RepaintTest:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RepaintTest : MonoBehaviour
    6. {
    7.     public bool test;
    8. }
    UXML:
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <UXML xmlns="UnityEngine.UIElements" xmlns:ui="UnityEditor.UIElements">
    3.     <Toggle name="test-toggle" label="Test"/>
    4. </UXML>
    Editor:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using UnityEditor.UIElements;
    5. using PopupWindow = UnityEngine.UIElements.PopupWindow;
    6.  
    7. [CustomEditor(typeof(RepaintTest))]
    8. public class RepaintTestEditor : Editor
    9. {
    10.     private VisualElement root;
    11.     private PopupWindow popup;
    12.     private RepaintTest repaintTest;
    13.  
    14.     public override VisualElement CreateInspectorGUI()
    15.     {
    16.         repaintTest = target as RepaintTest;
    17.  
    18.         root = new VisualElement();
    19.         root.Clear();
    20.  
    21.         // Import UXML
    22.         var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Scripts/Editor/RepaintTestEditor.uxml");
    23.         visualTree.CloneTree(root);
    24.  
    25.         Toggle testToggle = root.Q<Toggle>("test-toggle");
    26.         testToggle.SetValueWithoutNotify(repaintTest.test);
    27.         testToggle.RegisterCallback<ChangeEvent<bool>>(evt =>
    28.         {
    29.             //Debug.Log(evt.newValue); // -- called
    30.             repaintTest.test = evt.newValue;
    31.  
    32.             //here it needs to be repainted and passed through the statment below to draw a popup
    33.             root.MarkDirtyRepaint(); //won't work
    34.         });
    35.  
    36.         if (repaintTest.test)
    37.         {
    38.             DrawPopup();
    39.         }
    40.  
    41.         return root;
    42.     }
    43.  
    44.     private void DrawPopup()
    45.     {
    46.         if (popup != null)
    47.         {
    48.             //remove previouse popup to show new
    49.             popup.RemoveFromHierarchy();
    50.         }
    51.  
    52.         popup = new PopupWindow()
    53.         {
    54.             name = "test-popup",
    55.             text = "Test popup"
    56.         };
    57.  
    58.         Button removeBtn = new Button(() =>
    59.         {
    60.             popup.RemoveFromHierarchy();
    61.             repaintTest.test = false;
    62.             root.MarkDirtyRepaint(); //needed to update the toggle. Also won't work
    63.         })
    64.         {
    65.             name = "remove-button",
    66.             text = "Remove popup"
    67.         };
    68.  
    69.         popup.Add(removeBtn);
    70.  
    71.         root.Add(popup);
    72.     }
    73. }
     
  5. NotGoodEnoughh

    NotGoodEnoughh

    Joined:
    Feb 1, 2018
    Posts:
    35
    Thank you for reply! I will try it and try to make it better in my case.