The AddAndRemoveVariablesForType event allows plugins to add and remove variables from standard elements. Since Gum is designed to work with external engines the properties presented by standard types (such as Sprite) may not align with the feature set of the external engine. Variables can be added and removed to create a more natural development experience for users of Gum.
The following code can be used to add a variable to the Sprite standard element. This variable will be a float variable with the name "MyCustomVariable".
First the event must be added in the StartUp method:
The HandleAddAndRemoveVariablesForType method handles the event being raised and adds the necessary variables. Note that in this case we are only handling variables for the Sprite type, but the same method could be used for all types:
The end result would be that the Sprite object displays the variable when selected:
The example above shows how to add variables, but you can also remove variables from standard types. For example if your engine does not support texture wrapping on Sprites, you may do the following:
To prevent accidental deletion of data, Gum will still present variables which are defined in the XML files for standard elements even if the variable is removed through a plugin. Therefore, to fully remove a variable (like Wrap), it must be removed from the underlying XML file as well. This can be done by removing the individual file from the XML file, or deleting the entire XML file (which will cause Glue to regenerate it). Keep in mind that deleting and regenerating the entire XML file will result in all other changes being lost.
The syntax used to add new variables in plugins is the same as how Gum adds standard properties. For an extensive example on how to add variables, see the StandardElementsManager.Initialize method.
The Export delegate allows you to create a custom export for Gum elements. This delegate will automatically be called by Gum whenever an element is saved. By default this occurs whenever the user makes any modification to the element. If your export is especially processor intensive you may consider not adding your export logic here and rather requiring an explicit export by the user.
The following code will show a message box whenever an element is exported. Keep in mind this is only for demonstration purposes. By default Gum auto-saves every change made by the user, and showing a message box after every save can be very annoying for users of your plugin.
The above example shows how to react to a component being exported, but it doesn't get at the heart of what an export plugin does.
The first thing that an export plugin needs to do is to access properties on an element (such as its position and size) as well as the instances contained within the element. This information is available through the ElementSave class.
For more information on how to access properties and instances from the ElementSave, see the Gum Class Overview page.
The following shows what a very simple exporter might look like:
This may produce output that looks like this:
This page discusses how to write plugins for Gum. Plugins are a useful way to modify Gum because they allow you to customize Gum without making project-specific, technology-specific, or organization-specific modifications to the core source code. This means that you can customize the Gum experience while still maintaining ties to the core source code.
To begin writing a plugin:
Obtain the Gum source code. You can download the .zip or get the source through a version control client
Create a copy of Gum.sln. You will want to work with your own .sln file so that the project containing your plugin can be debugged easily. For example, you might want to call your solution GumWithPlugins.sln
Open your new .sln file in Visual Studio
Now that you have created a .sln which will contain your plugin project, you can add this project:
Right-click on your solution
Select "Add ->New Project..."
Select "Class Library" as the type
Verify that you are targeting .NET 4.5
Enter the name of your project, such as MyPluginProject
Click OK
Next you'll need to reference the Gum libraries. To do this:
Right-click on your project's References
Select "Add Reference"
Verify that "Solution" is selected (Assuming Visual Studio 2012)
Check the following projects:
Gum
GumDataTypes
InputLibrary
RenderingLibrary
ToolsUtilities
Click OK
Since Gum references XNA, and XNA doesn't have 64 bit libraries, you will need to modify your project's build configuration. To do this:
Select the "Build"->"Configuration Manager" menu item
Change the Active solution platform" from "Mixed Platforms" to "x86" if it isn't already set to x86. If your Active solution is set to "Any CPU", change that to "x86"
If your plugin project's Platform is set to "Any CPU", use the drop down to pick "x86" if it exists. If not, select ""
Change the New platform to "x86"
Click "OK"
Make sure your plugin project is set to build
Click Close to close the Configuration Manager
Adding a Plugin class
Next you'll want to add a Plugin class. This is a class that inherits from PluginBase. To do this:
Right-click on your project
Select "Add->Class..."
Name your class "MyPlugin" or whatever you want your plugin to be called
Add the following using:
Modify your plugin so it's public and inherits from PluginBase:
Add the following implementation into your plugin class:
Now that you have a simple plugin, you need to do two things to test this plugin out. The first is to mark the plugin as a class that is exported using Microsoft Extension Framework. To do this:
Right-click on your project's References item in the Solution Explorer
Select "Add Reference..."
Click the "Assemblies->Framework" tab
Check "System.ComponentModel.Composition"
Click OK
Add the following using statement to your plugin class: using System.ComponentModel.Composition;
Modify your class definition so it looks like this (we're making it public and also adding the Export attribute:
Now you can right-click on your project and select Build and it will generate a .dll for your project. This .dll file needs to be in the Plugins folder under the Gum binary. That is, from the location of your .sln file, you can go to . Once there, you will need to create a folder for your plugin (like MyPlugin) and place your .dll there.
To verify that your plugin is working correctly:
Run Gum
Click Plugins->"Manage Plugins"
This may happen if you are targeting different versions of the .NET framework. At the time of this writing Gum targets .NET 4.5. Therefore, you'll want to make sure that your Plugin project also targets .NET 4.5. To do this:
Right-click on your project
Select "Properties"
Select the "Application" tab
Verify that the "Target Framework:" is set to .NET Framework 4.5 (or whatever version Gum is currently on)
The AddMenuItem function allows adding menu items to Gum. The AddMenuItem accepts an enumerable of strings which allow you to embed menu items under a root menu item.
The following shows how to create a menu item called "My Plugin" which contains two items: First and Second. Clicking each item results in a message box appearing. Add the following to your plugin's StartUp function.
dll files created for plugins are files which are not required for Gum to function. In other words, if the file is there Gum will use it. If not, Gum can operate normally. This means that in your Visual Studio solution the project that you have created to hold your plugin will not automatically be copied to the build output folder. Fortunately Visual Studio supports post-build events which allow you to copy the built .dll to the plugins folder.
To access the post build event on a project:
Right-click on your project in the Solution Explorer
Select "Properties"
Select the "Build Events" tab on the properties page
Notice the "Post-build event command line:" section
To add a post build event, you will need to add command line commands to copy the .dll and .pdb to the plugin folder. Post-build events are simply run in a command line environment after the build succeeds. Therefore you can put any logic in here, including running other .exe or .bat files.
For this example we will assume that the project is called PluginProject. You will want to change the following text to match your project's name. To copy the files, paste (and modify) the following:
Notice that "PluginProject" appears 8 times in the text above, so be sure to replace all instances with your project name.
The copy script copies the .pdb file to allow you to debug your plugin. Specifically the PDB enables breakpoints to trigger and to break on exceptions.