How to structure c# code for custom editor panels

I have an editor window with multiple panels, and I want to structure it so each panel (i.e inspector panel, view panel, data panel etc) has it’s own uxml file which I can author using uibuilder, and have a corresponding c# class to manage the interactions of the controls.

How can the uxml be associated with the c# class, so when I drag the uxml file into the main editor window, and instance of that class is automatically created?

I think my c# class needs to inherit from Visual element, but I’m kinda stuck there what to do next.

Probably, yes, first make a custom control: Unity - Manual: Create a custom control

Then make a uxml file and use this custom visual element in it. Then you can add these uxml files to other uxml files in the UI Builder.

You can’t directly associate a C# class with a uxml file, however. You would have to query (.Q<T>) for the visual element type probably during initialisation.

Personally my preference is to use custom C# controls and build the layout via code. Then USS styles sheets and TSS themes can be used to style it all.