diff --git a/Assets/Fungus/Camera/Editor/CameraMenuItems.cs b/Assets/Fungus/Camera/Editor/CameraMenuItems.cs index 4249f4c3..a203440f 100644 --- a/Assets/Fungus/Camera/Editor/CameraMenuItems.cs +++ b/Assets/Fungus/Camera/Editor/CameraMenuItems.cs @@ -10,17 +10,7 @@ namespace Fungus [MenuItem("GameObject/Fungus/Camera/View")] static void CreateBackground() { - InstantiatePrefab("View"); - } - - static void InstantiatePrefab(string prefabName) - { - GameObject prefab = Resources.LoadAssetAtPath("Assets/Fungus/Camera/Prefabs/" + prefabName + ".prefab", typeof(GameObject)) as GameObject; - if (prefab != null) - { - GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject; - go.name = prefabName; - } + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Camera/Prefabs/View.prefab"); } } diff --git a/Assets/Fungus/Dialog/Editor/DialogMenuItems.cs b/Assets/Fungus/Dialog/Editor/DialogMenuItems.cs index d6a66003..035051af 100644 --- a/Assets/Fungus/Dialog/Editor/DialogMenuItems.cs +++ b/Assets/Fungus/Dialog/Editor/DialogMenuItems.cs @@ -8,46 +8,29 @@ namespace Fungus public class DialogMenuItems { - [MenuItem("GameObject/Fungus/Dialog/Background")] - static void CreateBackground() - { - InstantiatePrefab("Background"); - } - [MenuItem("GameObject/Fungus/Dialog/Character")] static void CreateCharacter() { - InstantiatePrefab("Character"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Dialog/Prefabs/Character.prefab"); } [MenuItem("GameObject/Fungus/Dialog/NarratorDialog")] static void CreateNarratorDialog() { - InstantiatePrefab("NarratorDialog"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Dialog/Prefabs/NarratorDialog.prefab"); } [MenuItem("GameObject/Fungus/Dialog/SayDialog")] static void CreateSayDialog() { - InstantiatePrefab("SayDialog"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Dialog/Prefabs/SayDialog.prefab"); } [MenuItem("GameObject/Fungus/Dialog/ChooseDialog")] static void CreateChooseDialog() { - InstantiatePrefab("ChooseDialog"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Dialog/Prefabs/ChooseDialog.prefab"); } - - static void InstantiatePrefab(string prefabName) - { - GameObject prefab = Resources.LoadAssetAtPath("Assets/Fungus/Dialog/Prefabs/" + prefabName + ".prefab", typeof(GameObject)) as GameObject; - if (prefab != null) - { - GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject; - go.name = prefabName; - } - } - } } \ No newline at end of file diff --git a/Assets/Fungus/FungusScript/Editor/FungusScriptMenuItems.cs b/Assets/Fungus/FungusScript/Editor/FungusScriptMenuItems.cs index 8210048e..01c4ce8d 100644 --- a/Assets/Fungus/FungusScript/Editor/FungusScriptMenuItems.cs +++ b/Assets/Fungus/FungusScript/Editor/FungusScriptMenuItems.cs @@ -1,5 +1,6 @@ using UnityEngine; using UnityEditor; +using System.IO; using System.Collections; namespace Fungus @@ -19,7 +20,34 @@ namespace Fungus fungusScript.selectedSequence = sequence; fungusScript.scrollPos = Vector2.zero; Undo.RegisterCreatedObjectUndo(newFungusScriptGO, "Create Fungus Script"); - } + } + + public static GameObject SpawnPrefab(string prefabFile) + { + string prefabName = Path.GetFileNameWithoutExtension(prefabFile); + + // This will only succeed if Fungus is located in the root folder of the project + GameObject prefab = Resources.LoadAssetAtPath(prefabFile, typeof(GameObject)) as GameObject; + if (prefab == null) + { + return null; + } + + GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject; + go.name = prefabName; + + Camera sceneCam = SceneView.currentDrawingSceneView.camera; + + Vector3 pos = sceneCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 10f)); + pos.z = 0f; + go.transform.position = pos; + + Selection.activeGameObject = go; + + Undo.RegisterCreatedObjectUndo(go, "Create Object"); + + return go; + } } } \ No newline at end of file diff --git a/Assets/Fungus/Sprite/Editor/SpriteMenuItems.cs b/Assets/Fungus/Sprite/Editor/SpriteMenuItems.cs index 945c68e9..b62333ae 100644 --- a/Assets/Fungus/Sprite/Editor/SpriteMenuItems.cs +++ b/Assets/Fungus/Sprite/Editor/SpriteMenuItems.cs @@ -10,39 +10,26 @@ namespace Fungus [MenuItem("GameObject/Fungus/Sprite/ClickableSprite")] static void CreateClickableSprite() { - InstantiatePrefab("ClickableSprite"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Sprite/Prefabs/ClickableSprite.prefab"); } [MenuItem("GameObject/Fungus/Sprite/DraggableSprite")] static void CreateDraggableSprite() { - InstantiatePrefab("DraggableSprite"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Sprite/Prefabs/DraggableSprite.prefab"); } [MenuItem("GameObject/Fungus/Sprite/DragTargetSprite")] static void CreateDragTargetSprite() { - InstantiatePrefab("DragTargetSprite"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Sprite/Prefabs/DragTargetSprite.prefab"); } [MenuItem("GameObject/Fungus/Sprite/ParallaxSprite")] static void CreateParallaxSprite() { - InstantiatePrefab("ParallaxSprite"); + FungusScriptMenuItems.SpawnPrefab("Assets/Fungus/Sprite/Prefabs/ParallaxSprite.prefab"); } - - static void InstantiatePrefab(string prefabName) - { - GameObject prefab = Resources.LoadAssetAtPath("Assets/Fungus/Sprite/Prefabs/" + prefabName + ".prefab", typeof(GameObject)) as GameObject; - if (prefab != null) - { - GameObject go = PrefabUtility.InstantiatePrefab(prefab) as GameObject; - go.name = prefabName; - Undo.RegisterCreatedObjectUndo(go, go.name); - Selection.activeGameObject = go; - } - } - } } \ No newline at end of file