Take a dive deep into the new prefab features with developers Steen Lund. What's the inside versus outside of a prefab? How does it relate to Prefab mode, overrides, unpacking, and prefab variants? How has the new prefab back-end changed? This session will give you a better understanding of the Prefab system, the pros and cons of various implementation decisions, and some Aha moments working with Prefab nesting.
44. Editor Stages: [ExecuteAlways]
44
void Update()
{
// Rotate only in playmode
// We don't want this in edit mode nor in prefab mode
// as it would update the prefab asset
if (Application.IsPlaying(this))
{
Transform t = GetComponent<Transform>();
t.Rotate(rotationSpeed * Time.deltaTime, 0, 0);
}
++updateCount;
Debug.Log(string.Format("Updates {0}", updateCount));
}
45. Case:
45
I want to dynamically create an editing
environment for my prefab
46. 46
I want to dynamically create an editing
environment for my prefab
using UnityEditor.Experimental.SceneManagement;
[InitializeOnLoad]
class CustomPrefabEnvironment
{
static CustomPrefabEnvironment()
{
PrefabStage.prefabStageOpened += OnPrefabStageOpened;
}
static void OnPrefabStageOpened(PrefabStage prefabStage)
{
…
49. Is my GameObject a Prefab (and what kind)?
49
if (PrefabUtility.IsPartOfPrefabInstance(go))
{
var type = PrefabUtility.GetPrefabAssetType(go);
Debug.Log(string.Format("GameObject is part of a Prefab Instance of type {0}" ,type));
}
else
{
Debug.Log("Selected GameObject is not part of a Prefab Instance");
}
50. Case:
50
How do I know if 2 GameObjects are part of the
same Prefab Instance?
51. 51
How do I know if 2 GameObjects are part of the same Prefab Instance?
var firstHandle = PrefabUtility.GetPrefabInstanceHandle(Selection.gameObjects[0]);
var secondHandle = PrefabUtility.GetPrefabInstanceHandle(Selection.gameObjects[1]);
if (firstHandle == secondHandle)
Debug.Log("GameObject belongs to same instance");
else
Debug.Log("GameObject are not from the same instance");
53. I want to create a Prefab from script
53
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
// If you simply want the cube to be written as an asset but not turn into a prefab instance
// use this method
PrefabUtility.SaveAsPrefabAsset(cube, path);
// This method will create a Prefab asset and make the GameObject an instance of the
// new prefab asset
PrefabUtility.SaveAsPrefabAssetAndConnect(cube, path, InteractionMode.AutomatedAction);
57. 57
I want to nest Prefabs from script
var instanceA = (GameObject)PrefabUtility.InstantiatePrefab(goA);
var instanceB = (GameObject)PrefabUtility.InstantiatePrefab(goB);
instanceB.GetComponent<Transform>().parent = instanceA.GetComponent<Transform>();
PrefabUtility.ApplyPrefabInstance(instanceA, InteractionMode.AutomatedAction);
58. 58
I want to nest Prefabs from script
var instanceB = (GameObject)PrefabUtility.InstantiatePrefab(goB);
var root = PrefabUtility.LoadPrefabContents(path);
instanceB.GetComponent<Transform>().parent = root.GetComponent<Transform>();
PrefabUtility.SaveAsPrefabAsset(root, path);
PrefabUtility.UnloadPrefabContents(root);
60. 60
I want to change a flag on multiple Prefabs assets
// Load the content of the Prefab asset so we can modify it
var assetRoot = PrefabUtility.LoadPrefabContents(path);
GameObjectUtility.SetStaticEditorFlags(assetRoot, StaticEditorFlags.LightmapStatic);
// Write the updated GameObject to the Prefab asset
PrefabUtility.SaveAsPrefabAsset(assetRoot, path);
// Clean up is important or we will leak scenes and
// eventually run out of temporary scenes.
PrefabUtility.UnloadPrefabContents(assetRoot);
61. Case:
61
I want to add assets to my prefab file
AssetDatebase.AddObjectToAsset(newObject, asset);
63. 63
Advanced: I modified a Library folder object,
now I want to update the source asset
string path = "Assets/Prefabs/A.prefab";
var goA = (GameObject)AssetDatabase.LoadMainAssetAtPath(path);
GameObjectUtility.SetStaticEditorFlags(goA, ~GameObjectUtility.GetStaticEditorFlags(goA));
PrefabUtility.SavePrefabAsset(goA);