I am trying to create a window, from where a user can add gameobjects(prefab) to a scene by the click of a button. The problem I am facing is that the gameobject is added to the first scene always, not to the scene I have selected.
I am new to unity, any help would be highly appreciated.
Following is my code:
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using UnityEngine.UI;
using System.IO;
public class ToolBox : EditorWindow
{
[MenuItem("Window/Tool Box")]
public static void ShowWindow()
{
GetWindow<ToolBox>("Tool Box");
}
void OnGUI()
{
if(GUILayout.Button("Button")){
//EditorUtility.SaveWindowLayout("Assets/Editor/Layouts/Customizer Layout.dwlt");
Object prefab = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/buttonPrefab.prefab", typeof(GameObject));
GameObject _prefab = Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
_prefab.transform.SetParent (GameObject.Find("Canvas").GetComponent<RectTransform>(), false);
}
if(GUILayout.Button("Text")){
Object prefab = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/textPrefab.prefab", typeof(GameObject));
GameObject _prefab = Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
_prefab.transform.SetParent (GameObject.Find("Canvas").GetComponent<RectTransform>(), false);
}
}
}