PrefabUtility









How do I use Prefab Utility
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PhotonViewInspector.cs Copy
345     private static GameObject GetPrefabParent(GameObject mp)
346     {
347         #if UNITY_2_6_1 || UNITY_2_6 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4
348         // Unity 3.4 and older use EditorUtility
349         return (EditorUtility.GetPrefabParent(mp) as GameObject);
350         #else
351         // Unity 3.5 uses PrefabUtility
352         return PrefabUtility.GetPrefabParent(mp) as GameObject;
353         #endif
354     }
File name: ScmlPostProcessor.cs Copy
66         static void ImportScml(string assetPath)
67         {
68             string folderPath = Path.GetDirectoryName(assetPath);
69
70             //Load the SCML as XML
71             var doc = new XmlDocument();
72             doc.Load(assetPath);
73
74             //Parse the SCML file
75             var scml = new Spriter.ScmlObject(doc);
76
77             //TODO: Verify that all files/folders exist
78             var pb = new PrefabBuilder();
79             foreach (var entity in scml.Entities)
80             {
81                 //TODO: Settings file to customize prefab location
82                 var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab");
83
84                 //Change to forward slash for asset database friendliness
85                 prefabPath = prefabPath.Replace('\\', '/');
86
87                 //Either instantiate the existing prefab or create a new one
88                 GameObject go;
89                 var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
90                 if (prefabGo == null)
91                 {
92                     go = new GameObject();
93                     prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab);
94                 }
95                 else
96                 {
97                     go = GameObject.Instantiate(prefabGo) as GameObject;
98
99                     var oldAnimator = go.GetComponent();
100                     if (oldAnimator) GameObject.DestroyImmediate(oldAnimator);
101                 }
102
103                 //Build the prefab based on the supplied entity
104                 pb.MakePrefab(entity, go, folderPath);
105
106                 var animator = go.AddComponent();
107
108
109
110                 //Add animations to prefab object
111                 var anim = new AnimationBuilder();
112                 var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath);
113                 AssetDatabase.SaveAssets();
114
115                 var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller");
116                 UnityEditor.Animations.AnimatorController oldController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof (UnityEditor.Animations.AnimatorController));
117                 UnityEditor.Animations.AnimatorController controller = oldController;
118
119                 if (!oldController)
120                 {
121                     controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath);
122                     foreach (var animationClip in allAnimClips)
123                     {
124                         if (animationClip)
125                         {
126                             //UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
127                         }
128                     }
129                 }
130                 UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller);
131                 go.SetActive(true);
132                 //Update the prefab
133                 PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab);
134
135                 //Add a generic avatar - because why not?
136                 //TODO: May need to eventually break this into a separate class
137                 // ie: if we want to look for a root motion node by naming convention
138                 //var avatar = AvatarBuilder.BuildGenericAvatar(go, "");
139                 //avatar.name = go.name;
140                 //AssetDatabase.AddObjectToAsset(avatar, prefabPath);
141
142                 GameObject.DestroyImmediate(go);
143
144                 AssetDatabase.SaveAssets();
145             }
146         }
File name: ImportTiled2Unity.Mesh.cs Copy
27         private void CreatePrefab(XElement xmlPrefab, string objPath)
28         {
29             var customImporters = GetCustomImporterInstances();
30
31             // Part 1: Create the prefab
32             string prefabName = xmlPrefab.Attribute("name").Value;
33             float prefabScale = ImportUtils.GetAttributeAsFloat(xmlPrefab, "scale", 1.0f);
34             GameObject tempPrefab = new GameObject(prefabName);
35             HandleCustomProperties(tempPrefab, xmlPrefab, customImporters);
36
37             // Part 2: Build out the prefab
38             AddGameObjectsTo(tempPrefab, xmlPrefab, objPath, customImporters);
39
40             // Part 3: Allow for customization from other editor scripts to be made on the prefab
41             // (These are generally for game-specific needs)
42             CustomizePrefab(tempPrefab, customImporters);
43
44             // Part 3.5: Apply the scale only after all children have been added
45             tempPrefab.transform.localScale = new Vector3(prefabScale, prefabScale, prefabScale);
46
47             // Part 4: Save the prefab, keeping references intact.
48             string prefabPath = ImportUtils.GetPrefabPath(prefabName);
49             UnityEngine.Object finalPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
50
51             if (finalPrefab == null)
52             {
53                 // The prefab needs to be created
54                 ImportUtils.ReadyToWrite(prefabPath);
55                 finalPrefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
56             }
57
58             // Replace the prefab, keeping connections based on name.
59             PrefabUtility.ReplacePrefab(tempPrefab, finalPrefab, ReplacePrefabOptions.ReplaceNameBased);
60
61             // Destroy the instance from the current scene hiearchy.
62             UnityEngine.Object.DestroyImmediate(tempPrefab);
63         }

PrefabUtility 143 lượt xem

Gõ tìm kiếm nhanh...