- Home /
Question by
thrmotta · Aug 17, 2015 at 04:54 PM ·
c#scenesavesave scene
How to Intercept Unity "Save Scene As.." right after saving?
Hello, everyone!
So, I have a problem where my application needs to get the name of a saved scene right after pressing the "Save Scene as.." button. How can this be achieved?
I tried using AssetModificationProcessor with OnWillSaveAssets, but this gives me the exact moment BEFORE finishing saving and I need the exact moment AFTER save is finished.
Comment
Best Answer
Answer by thrmotta · Aug 17, 2015 at 07:17 PM
Answering my own question, I had to create a Singleton and fill the FileSystemWatcher delegates:
[ExecuteInEditMode]
class SceneNameSingleton : MonoBehaviour
{
private FileSystemWatcher m_SceneFileWatcher; // This watches to see if we have saved the level
private static SceneNameSingleton instance;
static GameObject genericSingletonGO;
public static string SINGLETON_NAME = "Singleton";
public static SceneNameSingleton Instance
{
get
{
if( instance == null )
{
if( genericSingletonGO == null )
{
genericSingletonGO = GameObject.Find( SINGLETON_NAME );
if( genericSingletonGO == null )
genericSingletonGO = new GameObject( SINGLETON_NAME );
}
if( genericSingletonGO != null && genericSingletonGO.GetComponent<SceneNameSingleton>( ) != null )
{
instance = genericSingletonGO.GetComponent<SceneNameSingleton>( );
}
else
{
instance = genericSingletonGO.AddComponent<SceneNameSingleton>( );
}
}
return instance;
}
}
void OnSceneFileWatcher_Created( object sender, FileSystemEventArgs e )
{
if( e.FullPath.Contains( ".meta" ) ) return;
if( !e.FullPath.Contains( ".unity" ) ) return;
Debug.Log( "OnSceneFileWatcher_Changed" );
string sceneName = System.IO.Path.GetFileNameWithoutExtension( e.FullPath );
Debug.Log( "File: " + sceneName + " " + e.ChangeType );
}
void OnEnable( )
{
Debug.Log( "OnEnable" );
m_SceneFileWatcher = new FileSystemWatcher( Path.GetFullPath( "Assets" ), "*.unity" );
m_SceneFileWatcher.Filter = "*.*";
m_SceneFileWatcher.NotifyFilter = NotifyFilters.LastWrite;
m_SceneFileWatcher.EnableRaisingEvents = true;
m_SceneFileWatcher.Created += new FileSystemEventHandler( OnSceneFileWatcher_Created );
m_SceneFileWatcher.Changed+= new FileSystemEventHandler( OnSceneFileWatcher_Created );
}
}
This way everytime a unity scene is created or modified it'll be detected and logged.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I create a save scene feature though int value in c# 2 Answers