Search Unity

Apply Prefab Script

Discussion in 'Scripting' started by Chacek, Jun 21, 2012.

  1. Chacek

    Chacek

    Joined:
    Nov 10, 2010
    Posts:
    5
    Hi!
    I have same problem with writing apply prefab script.
    I found that I have to use "SetPropertyModifications" from "PrefabUtility" but when i use "PrefabUtility" i have "Unknown identifier", so i try "EditorUtility" becouse it worksfine with "CreateEmptyPrefab" and "ReplacePrefab", but now i have "Method not found".
    Did any one can help me with this problem.
     
  2. ricardo_arango

    ricardo_arango

    Unity Technologies

    Joined:
    Jun 18, 2009
    Posts:
    64
    This this code:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class ReplacePrefab : EditorWindow {
    5.     GameObject prefab;
    6.     GameObject go;    
    7.     [MenuItem ("Prefabs/ReplacePrefab")]
    8.     static void OpenWindow (){
    9.         EditorWindow.GetWindow (typeof (ReplacePrefab));
    10.     }
    11.     void OnGUI (){
    12.         go = EditorGUILayout.ObjectField ("GameObject", go, typeof (GameObject), true) as GameObject;
    13.         prefab = EditorGUILayout.ObjectField ("Target Prefab", prefab, typeof (GameObject), false) as GameObject;
    14.         if (GUI.changed  prefab != null){
    15.             PrefabType prefabType = PrefabUtility.GetPrefabType(prefab);
    16.             if (prefabType != PrefabType.Prefab)
    17.                 prefab = null;
    18.         }
    19.         if (go != null  prefab != null){
    20.             if (GUILayout.Button ("Replace")){
    21.                 PrefabUtility.ReplacePrefab(go, prefab, ReplacePrefabOptions.ConnectToPrefab | ReplacePrefabOptions.ReplaceNameBased );
    22.                 GUIUtility.ExitGUI();
    23.             }
    24.         }
    25.     }
    26. }
     
    Brogan89 and eDmitriy like this.