I’m using a folder-based structure for organizing and calling my in-game sounds. I use an XML file to direct an in-game sound manager to the correct files in the Resources folder.
I want to set it up so that the XML file is updated whenever a sound file is added or moved. I know I can use AssetPostprocessor to catch the sounds importing, but this doesn’t work when files are moved from one folder to another.
Where can I pick up the fact that a file has been moved?
that is more of an operating system question. AFAIK, Unity doesn't have that kind of access to the file system. You'd have to bundle your game with an assist script that watched a file or folder and updated your XML based on changes, which would then get pushed to Unity via XML.
For clarification, I'm not going to be trawling the file system with the game's executable. This is essentially a tool so that I don't have to trigger the XML by hand every time a sound gets added or moved.
RE: zombienc: I think the idea is someone will use the Unity Editor to rearrange sounds. In theory, there could be an editor callback OnAssetMoved. Or, the table(?) used by Unity auto-fix references for moved assets, that might be accessible somehow.
I don’t know if there is a way to do that in Unity, but most OSes have something like a FileSystemWatcher. It takes a bit to get right, and shouldn’t be used for a game after development, but during development it could probably help you do what you need to be done.
This works:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
[InitializeOnLoad]
public class AssetWatcher : EditorWindow
{
private static FileSystemWatcher assetFileWatcher;
[MenuItem("Window/Asset Watcher")]
static void Init()
{
AssetWatcher window = (AssetWatcher)EditorWindow.GetWindow(typeof(AssetWatcher));
window.OnGUI();
}
void OnGUI()
{
OnEnableFileWatch();
}
static void OnEnableFileWatch()
{
if (assetFileWatcher == null)
{
string currentPath = Path.GetFullPath("Assets");
assetFileWatcher = new FileSystemWatcher(currentPath);
assetFileWatcher.Changed += OnAssetFileWatcherChanged;
assetFileWatcher.Created += OnAssetFileWatcherChanged;
assetFileWatcher.Deleted += OnAssetFileWatcherChanged;
assetFileWatcher.Renamed += OnAssetFileWatcherChanged;
assetFileWatcher.Filter = "*.*";
assetFileWatcher.IncludeSubdirectories = true;
assetFileWatcher.EnableRaisingEvents = true;
}
}
static void OnAssetFileWatcherChanged(object sender, FileSystemEventArgs e)
{
// Do stuff here
}
}
This will go haywire if you do anything substantial in the asset folder (copying a bunch of stuff for example). But, you can set the .Filter = "*.xml" to only trigger on xml files (assuming you're using that file extension). And if you change Path.GetFullPath("Assets") - to a more specific folder, you'll limit the noise. I'd recommend reading up on FileSystemWatcher: [MSDN FileSystemWatcher][1] [1]: http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
that is more of an operating system question. AFAIK, Unity doesn't have that kind of access to the file system. You'd have to bundle your game with an assist script that watched a file or folder and updated your XML based on changes, which would then get pushed to Unity via XML.
– zombienceFor clarification, I'm not going to be trawling the file system with the game's executable. This is essentially a tool so that I don't have to trigger the XML by hand every time a sound gets added or moved.
– drizztmainswordRE: zombienc: I think the idea is someone will use the Unity Editor to rearrange sounds. In theory, there could be an editor callback
– Owen-ReynoldsOnAssetMoved. Or, the table(?) used by Unity auto-fix references for moved assets, that might be accessible somehow.ah, I see. I didn't read the question well enough, and assumed this was for game runtime.
– zombience