I am currently writing a Particle System using XNA. What I'd like to do is use an external file (XML for example) and be able to modify this file, whilst the application is running, and once saved, the changes will be reflected in the Particle System.
My original proposal: Use a FileWatcher (can't remember the exact class name) to monitor the particle effect file and when the date changes, reload the file thus causing the changes to be made.
Any help would开发者_如何学C be appreciated.
You are on the right track.
Create a System.IO.FileSystemWatcher.
Subscribe to the Changed event.
When the Changed event occurs
If the path/file extension corresponds to a buildable resource type
Either
Directly create and invoke a content importer and processor. (Xna 4.0)
Or
Use MSBuild to build a dummy content project.
Replace references to the existing resource with the newly built resource
Dispose of the old resource if necessary
Notes:
The FileSystemWatcher sometimes generates multiple change events for a single change. Also, some programs, like photoshop save to a temporary file, then delete the original and rename the temporary file to the original name. My system buffers file system events and combines them into single events where possible. Because they are buffered this also allows me to apply the events to the content at an appropriate point in the program. If you don't buffer these events could trigger while the resource you are trying to replace is in use.
Shawn Hargreaves has written about Effect compilation and Content Pipeline automation in XNA Game Studio 4.0.
AppHub has a Code Snippet WinForms Series 2: Content Loading which shows how to use MSBuild to build content dynamically. I have found this method is slower than invoking the importer and processor directly.
In my system, all references to dynamic resources are indirect references. These indirect references are allocated and tracked by a resource manager. When a resource is dynamically built the resource manager replaces the actual reference inside the indirect reference. A resource changed event is generated through the indirect reference so clients can take action if required.
精彩评论