01. Referencing Code in Power Inspector

  10. Extending Power Inspector No Comments

Assembly Definition Files

Power Inspector makes use of assembly definition files to separate its scripts into assemblies that are kept separate from the other scripts in your project.

This reduces the compilation times in your project, because Unity does not need to rebuild the Power Inspector assemblies every time you make changes to your own code when there are no references between them.

This however also means that in order to make any references from your code to code in Power Inspector, you will have to first go through a couple of steps:

  1. Make sure that the script file from which you are trying to reference Power Inspector is included in an assembly definition file. If not, you can create a new assembly definition file in the Project view by selecting the root folder for your script assets, clicking the Create button and selecting Assembly Definition. You should also create separate Assembly Definition assets inside all your Editor folders that contain script assets (if you have any).
  2. Select the assembly definition asset for your script to view it in the inspector.
  3. Click the + sign at the bottom of the Assembly Definition References list.
  4. Click the circular icon on the new entry that appeared at the bottom of the list to open the Object picker view.
  5. Select eitherĀ PowerInspector.Runtime or PowerInspector.Editor from the list, depending on what code you want to reference.

POWER_INSPECTOR preprocessor directive

When Power Inspector is added to a project, the preprocessor directive POWER_INSPECTOR is automatically added to the Scripting Define Symbols list in your Player Settings.

This makes it possible for you to write code that refers to classes inside the Power Inspector that is only compiled if Power Inspector is installed. This way your extensions won’t generate any compile errors even if Power Inspector is not installed on the project.

This is especially useful for any code that you are going to publish in the asset store.

Example

#if POWER_INSPECTOR // Code is only compiled if Power Inspector is installed.
void OnGUI()
{
	if(GUILayout.Button("Open In Power Inspector"))
	{
		PowerInspectorWindowUtility.OpenNewWindow(this);
	}
}
#endif