Seanba / Tiled2Unity

Export Tiled Map Editor (TMX) files into Unity
Other
495 stars 120 forks source link

Custom handler after the prefab is saved #150

Closed ssideleau closed 5 years ago

ssideleau commented 6 years ago

Add the possibility to execute code when the prefab is saved.

This is my first PR to this project so any edits / commentary is more than welcome.

I used this to create scenes with my prefab in it. Ex:

[Tiled2Unity.CustomTiledImporter]
class CustomPrefabSaved : Tiled2Unity.ICustomAfterSave {
    public void HandlePrefabSaved(UnityEngine.Object prefab, string prefabName) {
        UnityEngine.SceneManagement.Scene scene = UnityEditor.SceneManagement.EditorSceneManager.GetSceneByName(prefabName);

        if (!scene.IsValid()) {
            UnityEngine.SceneManagement.Scene newScene = UnityEditor.SceneManagement.EditorSceneManager.NewScene(UnityEditor.SceneManagement.NewSceneSetup.EmptyScene, UnityEditor.SceneManagement.NewSceneMode.Additive);
            UnityEditor.SceneManagement.EditorSceneManager.SetActiveScene(newScene);
            UnityEditor.PrefabUtility.InstantiatePrefab(prefab, newScene);
            UnityEditor.SceneManagement.EditorSceneManager.SaveScene(newScene, "Assets/Scenes/Maps/" + prefabName + ".unity");
        }
    }
}