Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
PrefabAPIExamples/Assets/Editor/Scripts/CustomPrefabEnvironment.cs /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
36 lines (30 sloc)
1.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.SceneManagement; | |
| using UnityEditor; | |
| using UnityEditor.Experimental.SceneManagement; | |
| [InitializeOnLoad] | |
| class CustomPrefabEnvironment | |
| { | |
| static CustomPrefabEnvironment() | |
| { | |
| // Uncomment this to dynamically create a plane when entering Prefab Mode | |
| //PrefabStage.prefabStageOpened += OnPrefabStageOpened; | |
| } | |
| static void OnPrefabStageOpened(PrefabStage prefabStage) | |
| { | |
| Debug.Log("OnPrefabStageOpened " + prefabStage.prefabAssetPath); | |
| // Get info from the PrefabStage | |
| var root = prefabStage.prefabContentsRoot; | |
| var scene = prefabStage.scene; | |
| var renderer = root.GetComponent<Renderer>(); | |
| // If no renderer skip our custom environment | |
| if (renderer == null) | |
| return; | |
| // Create environment plane | |
| var plane = GameObject.CreatePrimitive(PrimitiveType.Plane); | |
| SceneManager.MoveGameObjectToScene(plane, scene); | |
| // Adjust environment plane to the prefab root's lower bounds | |
| Bounds bounds = renderer.bounds; | |
| plane.transform.position = new Vector3(bounds.center.x, bounds.min.y, bounds.center.z); | |
| } | |
| } |