Search Unity

EditorWindow, instantiate with prefab connection

Discussion in 'Scripting' started by CrashKonijn, Sep 23, 2014.

  1. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    Hello all,

    I'm creating an editor windows script as a tool to create our levels more easily. So far I've got it all working but the only problem is that no matter how i spawn the object, it loses the connection with the prefab (which is obviously a bad thing). I've tried for a couple hours now and I'm not getting any further so I thought I'd ask you guys for help.

    I've already tried with this thread (best one I could find regarding this problem) but I can't seem to get it to work
    http://answers.unity3d.com/questions/404789/instantiate-with-prefabs.html

    I hope one of you can help me out.

    Kind regards,

    Peter

    Code (CSharp):
    1. // C# example:
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections.Generic;
    5.  
    6. public class TrampolineWindow : EditorWindow
    7. {
    8.     [SerializeField]
    9.     public List<Trampoline> trampolines;
    10.     public Vector3 offset;
    11.     public GameObject trampolinePrefab;
    12.  
    13.     // Add menu named "My Window" to the Window menu
    14.     [MenuItem("Window/Trampoline Window")]
    15.     static void Init()
    16.     {
    17.         // Get existing open window or if none, make a new one:
    18.         TrampolineWindow window = (TrampolineWindow)EditorWindow.GetWindow(typeof(TrampolineWindow));
    19.     }
    20.  
    21.     void OnGUI()
    22.     {
    23.         // prefab selector
    24.         //trampolinePrefab = EditorGUILayout.ObjectField("Trampoline prefab", trampolinePrefab, typeof(PrefabType), true) as PrefabType;
    25.         trampolinePrefab = EditorGUILayout.ObjectField("Trampoline prefab", trampolinePrefab, typeof(GameObject), true) as GameObject;
    26.  
    27.         // enable this btn when count > 0
    28.         GUI.enabled = (trampolines.Count > 0 && trampolinePrefab != null);
    29.         // add btn
    30.         if (GUILayout.Button("Spawn trampoline"))
    31.         {
    32.             SpawnNew();
    33.         }
    34.         GUI.enabled = true;
    35.     }
    36.  
    37.     void OnSelectionChange()
    38.     {
    39.         GetTrampolineSelection();
    40.     }
    41.  
    42.     void GetTrampolineSelection()
    43.     {
    44.         // reset trampoline selection
    45.         trampolines = new List<Trampoline>();
    46.  
    47.         // get all the selected objects
    48.         GameObject[] rawObjects = Selection.gameObjects;
    49.        
    50.         // loop through the objects
    51.         foreach (GameObject o in rawObjects) {
    52.             if(o.GetComponent("Trampoline")){
    53.                 trampolines.Add(o.GetComponent<Trampoline>());
    54.             }
    55.         }
    56.  
    57.         // repaint the windows
    58.         this.Repaint();
    59.  
    60.         Debug.Log("Selected "+trampolines.Count+" trampolines");
    61.     }
    62.  
    63.     void SpawnNew()
    64.     {
    65.         GameObject obj = Instantiate(Resources.Load("prefabs/Trampoline"), SpawnPosition(), Quaternion.identity) as GameObject;
    66.  
    67.         Selection.activeGameObject = obj;
    68.     }
    69.  
    70.     Vector3 SpawnPosition()
    71.     {
    72.         Vector3 tPos = Vector3.zero;
    73.         int count = trampolines.Count;
    74.  
    75.         foreach (Trampoline t in trampolines) {
    76.             tPos += t.transform.position;
    77.         }
    78.         // create average
    79.         tPos = tPos / count;
    80.  
    81.         // add offset
    82.         tPos += offset;
    83.  
    84.         return tPos;
    85.     }
    86. }
     
  2. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    There's no-one that can help me out with fixing this? My tool doesn't help in anyway if it doesn't spawn prefabs that are still connected to the prefab.

    Btw I'm using the unity 4.6 beta, might this be the problem?

    Kind regards,

    Peter
     
  3. CrashKonijn

    CrashKonijn

    Joined:
    Mar 4, 2010
    Posts:
    245
    I'm very sorry to bump this topic again, but is there really no-one that can help me out with this? I really can't get this thing to work with the object still having a connection to the prefab, which should be doable right?

    Kind regards,

    Peter
     
  4. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    In my opinion, thinking scenes as levels is bad practice. This way you'll end up with tons of scenes and each time you change the level you'll be reloading assets all over again. This is of-course not the chase with all sorts of games, but most games I have worked on have benefited from this.

    When I am making level editor I am usually saving data which I can use to spawn objects. For example I have serializeable Entity class which is stored in EntityBehavior, this class contains the category and name of the object so I can easily fetch it's prefab with Resources.Load, it also contains position and rotation (and userdata for object specific data).

    this way I can easily save the levels to JSON files for example which you can load in a generic Gamescene. One of the many advantages of this is that you don't need to reload the scene to reset it. you just need to reset the objects to their positions and state specified in the level data.
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190