private static void SetSceneCell(GridLayout grid, Transform parent, Vector3Int position, GameObject go, Vector3 offset, Vector3 scale, Quaternion orientation) { if (parent == null || go == null) { return; } GameObject instance = null; if (PrefabUtility.IsPartOfPrefabAsset(go)) { instance = (GameObject)PrefabUtility.InstantiatePrefab(go); } else { instance = Instantiate(go); instance.hideFlags = HideFlags.None; instance.name = go.name; } Undo.RegisterCreatedObjectUndo(instance, "Paint GameObject"); instance.transform.SetParent(parent); instance.transform.position = grid.LocalToWorld(grid.CellToLocalInterpolated(new Vector3Int(position.x, position.y, position.z) + new Vector3(.5f, .5f, .5f))); instance.transform.localRotation = orientation; instance.transform.localScale = scale; instance.transform.Translate(offset); }
static void CreateSpriteMaskGameObject() { var go = ObjectFactory.CreateGameObject(""); go.AddComponent <SpriteMask>(); if (Selection.activeObject is Sprite) { go.GetComponent <SpriteMask>().sprite = (Sprite)Selection.activeObject; } else if (Selection.activeObject is Texture2D) { var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (!string.IsNullOrEmpty(assetPath)) { var sprite = AssetDatabase.LoadAssetAtPath <Sprite>(assetPath); if (sprite != null) { go.GetComponent <SpriteMask>().sprite = sprite; } } } else if (Selection.activeObject is GameObject) { var activeGO = (GameObject)Selection.activeObject; if (!PrefabUtility.IsPartOfPrefabAsset(activeGO)) { GameObjectUtility.SetParentAndAlign(go, activeGO); } } go.name = GameObjectUtility.GetUniqueNameForSibling(go.transform.parent, Contents.newSpriteMaskName.text); Undo.SetCurrentGroupName(Contents.createSpriteMaskUndoString.text); Selection.activeGameObject = go; }
void CalculatePrefabStatus() { m_PlayModeObjects = false; m_IsAsset = false; m_ImmutableSelf = false; m_ImmutableSourceAsset = false; m_IsDisconnected = false; m_IsMissing = false; m_IsPrefabInstanceAnyRoot = true; m_IsPrefabInstanceOutermostRoot = true; m_AllOfSamePrefabType = true; PrefabAssetType firstType = PrefabUtility.GetPrefabAssetType(targets[0]); PrefabInstanceStatus firstStatus = PrefabUtility.GetPrefabInstanceStatus(targets[0]); foreach (var o in targets) { var go = (GameObject)o; PrefabAssetType type = PrefabUtility.GetPrefabAssetType(go); PrefabInstanceStatus status = PrefabUtility.GetPrefabInstanceStatus(go); if (type != firstType || status != firstStatus) { m_AllOfSamePrefabType = false; } if (Application.IsPlaying(go)) { m_PlayModeObjects = true; } if (!PrefabUtility.IsAnyPrefabInstanceRoot(go)) { m_IsPrefabInstanceAnyRoot = false; // Conservative is false if any is false } if (!m_IsPrefabInstanceAnyRoot || !PrefabUtility.IsOutermostPrefabInstanceRoot(go)) { m_IsPrefabInstanceOutermostRoot = false; // Conservative is false if any is false } if (PrefabUtility.IsPartOfPrefabAsset(go)) { m_IsAsset = true; // Conservative is true if any is true } if (m_IsAsset && PrefabUtility.IsPartOfImmutablePrefab(go)) { m_ImmutableSelf = true; // Conservative is true if any is true } GameObject originalSourceOrVariant = PrefabUtility.GetOriginalSourceOrVariantRoot(go); if (originalSourceOrVariant != null && PrefabUtility.IsPartOfImmutablePrefab(originalSourceOrVariant)) { m_ImmutableSourceAsset = true; // Conservative is true if any is true } if (PrefabUtility.IsDisconnectedFromPrefabAsset(go)) { m_IsDisconnected = true; } if (PrefabUtility.IsPrefabAssetMissing(go)) { m_IsMissing = true; } } }
protected override void Awake() { base.Awake(); m_ContainsMissingSerializeReferenceType = false; foreach (var prefabAssetRoot in assetTargets) { if (PrefabUtility.HasInvalidComponent(prefabAssetRoot)) { m_PrefabsWithMissingScript.Add(AssetDatabase.GetAssetPath(prefabAssetRoot)); } if (PrefabUtility.IsPartOfPrefabAsset(prefabAssetRoot) && PrefabUtility.HasManagedReferencesWithMissingTypes(prefabAssetRoot)) { m_ContainsMissingSerializeReferenceType = true; } } m_PrefabsWithMissingScript.Sort(); }
void CalculatePrefabStatus() { m_IsPrefabInstanceAnyRoot = false; m_IsAsset = false; m_AllOfSamePrefabType = true; PrefabAssetType firstType = PrefabUtility.GetPrefabAssetType(targets[0]); PrefabInstanceStatus firstStatus = PrefabUtility.GetPrefabInstanceStatus(targets[0]); foreach (GameObject go in targets) { PrefabAssetType type = PrefabUtility.GetPrefabAssetType(go); PrefabInstanceStatus status = PrefabUtility.GetPrefabInstanceStatus(go); if (type != firstType || status != firstStatus) { m_AllOfSamePrefabType = false; } if (PrefabUtility.IsAnyPrefabInstanceRoot(go)) { m_IsPrefabInstanceAnyRoot = true; } if (m_IsPrefabInstanceAnyRoot) { m_IsPrefabInstanceOutermostRoot = PrefabUtility.IsOutermostPrefabInstanceRoot(go); } if (PrefabUtility.IsPartOfPrefabAsset(go)) { m_IsAsset = true; } if (m_IsAsset && PrefabUtility.IsPartOfImmutablePrefab(go)) { m_ImmutableSelf = true; } GameObject originalSourceOrVariant = PrefabUtility.GetOriginalSourceOrVariantRoot(go); if (originalSourceOrVariant != null && PrefabUtility.IsPartOfImmutablePrefab(originalSourceOrVariant)) { m_ImmutableSourceAsset = true; } } }
public void OnSceneDrag(SceneView sceneView) { GameObject go = target as GameObject; if (!PrefabUtility.IsPartOfPrefabAsset(go)) { return; } var prefabAssetRoot = go.transform.root.gameObject; Event evt = Event.current; switch (evt.type) { case EventType.DragUpdated: Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene(); if (dragObject == null) { dragObject = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene); dragObject.hideFlags = HideFlags.HideInHierarchy; dragObject.name = go.name; } if (HandleUtility.ignoreRaySnapObjects == null) { HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>(); } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; object hit = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(evt.mousePosition)); if (hit != null) { RaycastHit rh = (RaycastHit)hit; float offset = 0; if (Tools.pivotMode == PivotMode.Center) { float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, rh.normal); if (geomOffset != Mathf.Infinity) { offset = Vector3.Dot(dragObject.transform.position, rh.normal) - geomOffset; } } dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(rh.point + (rh.normal * offset)); } else { dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10); } // Use prefabs original z position when in 2D mode if (sceneView.in2DMode) { Vector3 dragPosition = dragObject.transform.position; dragPosition.z = prefabAssetRoot.transform.position.z; dragObject.transform.position = dragPosition; } evt.Use(); break; case EventType.DragPerform: var stage = StageNavigationManager.instance.currentItem; if (stage.isPrefabStage) { var prefabAssetThatIsAddedTo = AssetDatabase.LoadMainAssetAtPath(stage.prefabAssetPath); if (PrefabUtility.CheckIfAddingPrefabWouldResultInCyclicNesting(prefabAssetThatIsAddedTo, go)) { PrefabUtility.ShowCyclicNestingWarningDialog(); return; } } Transform parent = sceneView.customParentForDraggedObjects; string uniqueName = GameObjectUtility.GetUniqueNameForSibling(parent, dragObject.name); if (parent != null) { dragObject.transform.parent = parent; } dragObject.hideFlags = 0; Undo.RegisterCreatedObjectUndo(dragObject, "Place " + dragObject.name); EditorUtility.SetDirty(dragObject); DragAndDrop.AcceptDrag(); Selection.activeObject = dragObject; HandleUtility.ignoreRaySnapObjects = null; if (SceneView.mouseOverWindow != null) { SceneView.mouseOverWindow.Focus(); } dragObject.name = uniqueName; dragObject = null; evt.Use(); break; case EventType.DragExited: if (dragObject) { UnityObject.DestroyImmediate(dragObject, false); HandleUtility.ignoreRaySnapObjects = null; dragObject = null; evt.Use(); } break; } }
public void OnSceneDrag(SceneView sceneView) { GameObject go = target as GameObject; if (!PrefabUtility.IsPartOfPrefabAsset(go)) { return; } var prefabAssetRoot = go.transform.root.gameObject; Event evt = Event.current; switch (evt.type) { case EventType.DragUpdated: Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene(); if (dragObject == null) { // While dragging the instantiated prefab we do not want to record undo for this object // this will cause a remerge of the instance since changes are undone while dragging. // The DrivenRectTransformTracker by default records Undo when used when driving // UI components. This breaks our hideflag setup below due to a remerge of the dragged instance. // StartRecordingUndo() is called on DragExited. Fixes case 1223793. DrivenRectTransformTracker.StopRecordingUndo(); if (!EditorApplication.isPlaying || EditorSceneManager.IsPreviewScene(destinationScene)) { dragObject = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene); dragObject.name = go.name; } else { // Instatiate as regular GameObject in Play Mode so runtime logic // won't run into restrictions on restructuring Prefab instances. dragObject = Instantiate(prefabAssetRoot); SceneManager.MoveGameObjectToScene(dragObject, destinationScene); } dragObject.hideFlags = HideFlags.HideInHierarchy; } if (HandleUtility.ignoreRaySnapObjects == null) { HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>(); } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; Vector3 point, normal; if (HandleUtility.PlaceObject(evt.mousePosition, out point, out normal)) { float offset = 0; if (Tools.pivotMode == PivotMode.Center) { float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, normal); if (geomOffset != Mathf.Infinity) { offset = Vector3.Dot(dragObject.transform.position, normal) - geomOffset; } } dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(point + (normal * offset)); } else { dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10); } // Use prefabs original z position when in 2D mode if (sceneView.in2DMode) { Vector3 dragPosition = dragObject.transform.position; dragPosition.z = prefabAssetRoot.transform.position.z; dragObject.transform.position = dragPosition; } evt.Use(); break; case EventType.DragPerform: if (!DragPerform(sceneView, dragObject, go)) { return; } dragObject = null; evt.Use(); break; case EventType.DragExited: // DragExited is always fired after DragPerform so we do no need to call StartRecordingUndo // in DragPerform DrivenRectTransformTracker.StartRecordingUndo(); if (dragObject) { UnityObject.DestroyImmediate(dragObject, false); HandleUtility.ignoreRaySnapObjects = null; dragObject = null; evt.Use(); } break; } }
public override void OnInspectorGUI() { if (m_ServerOnlyProperty == null) { m_Initialized = false; } Init(); serializedObject.Update(); if (m_ServerOnlyProperty.boolValue) { EditorGUILayout.PropertyField(m_ServerOnlyProperty, m_ServerOnlyLabel); EditorGUILayout.LabelField("Local Player Authority cannot be set for server-only objects"); } else if (m_LocalPlayerAuthorityProperty.boolValue) { EditorGUILayout.LabelField("Server Only cannot be set for Local Player Authority objects"); EditorGUILayout.PropertyField(m_LocalPlayerAuthorityProperty, m_LocalPlayerAuthorityLabel); } else { EditorGUILayout.PropertyField(m_ServerOnlyProperty, m_ServerOnlyLabel); EditorGUILayout.PropertyField(m_LocalPlayerAuthorityProperty, m_LocalPlayerAuthorityLabel); } serializedObject.ApplyModifiedProperties(); if (!Application.isPlaying) { return; } // Runtime actions below here EditorGUILayout.Separator(); if (m_NetworkIdentity.observers != null && m_NetworkIdentity.observers.Count > 0) { m_ShowObservers = EditorGUILayout.Foldout(m_ShowObservers, "Observers"); if (m_ShowObservers) { EditorGUI.indentLevel += 1; foreach (var o in m_NetworkIdentity.observers) { GameObject obj = null; foreach (var p in o.playerControllers) { if (p != null) { obj = p.gameObject; break; } } if (obj) { EditorGUILayout.ObjectField("Connection " + o.connectionId, obj, typeof(GameObject), false); } else { EditorGUILayout.TextField("Connection " + o.connectionId); } } EditorGUI.indentLevel -= 1; } } if (PrefabUtility.IsPartOfPrefabAsset(m_NetworkIdentity.gameObject)) { return; } if (m_NetworkIdentity.gameObject.activeSelf && m_NetworkIdentity.netId.IsEmpty() && NetworkServer.active) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(m_SpawnLabel); if (GUILayout.Toggle(false, "Spawn", EditorStyles.miniButtonLeft)) { NetworkServer.Spawn(m_NetworkIdentity.gameObject); EditorUtility.SetDirty(target); // preview window STILL doens't update immediately.. } EditorGUILayout.EndHorizontal(); } }
internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab) { if (gameObjects == null) { throw new ArgumentNullException("gameObjects"); } if (gameObjects.Length == 0) { throw new ArgumentException("gameObjects array is empty"); } if (targetPrefab == null) { throw new ArgumentNullException("targetPrefab"); } if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab)) { throw new ArgumentException("Target Prefab has to be a Prefab Asset"); } Object targetPrefabInstance = null; var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab); foreach (GameObject go in gameObjects) { if (go == null) { throw new ArgumentException("GameObject in input 'gameObjects' array is null"); } if (EditorUtility.IsPersistent(go)) // Prefab asset { throw new ArgumentException("Game object is part of a prefab"); } var parentPrefabInstance = GetParentPrefabInstance(go); if (parentPrefabInstance == null) { throw new ArgumentException("GameObject is not (directly) parented under a target Prefab instance."); } if (targetPrefabInstance == null) { targetPrefabInstance = parentPrefabInstance; if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject)) { throw new ArgumentException("GameObject is not parented under a target Prefab instance."); } } else { if (parentPrefabInstance != targetPrefabInstance) { throw new ArgumentException("GameObjects must be parented under the same Prefab instance."); } } if (PrefabUtility.IsPartOfNonAssetPrefabInstance(go)) { var correspondingGO = PrefabUtility.GetCorrespondingObjectFromSource(go); var correspondingGOPrefabObject = PrefabUtility.GetPrefabAssetHandle(correspondingGO); if (targetPrefabObject == correspondingGOPrefabObject) { throw new ArgumentException("GameObject is already part of target prefab"); } } } string prefabGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetPrefab)); if (!VerifyNestingFromScript(gameObjects, prefabGUID, null)) { throw new ArgumentException("Cyclic nesting detected"); } AddGameObjectsToPrefabAndConnect_Internal(gameObjects, targetPrefab); }
public override void OnInspectorGUI() { var body = target as Rigidbody2D; serializedObject.Update(); EditorGUILayout.PropertyField(m_BodyType); EditorGUILayout.PropertyField(m_Material); // Provide the user some information when simulation is turned off. EditorGUILayout.PropertyField(m_Simulated); if (!m_Simulated.boolValue && !m_Simulated.hasMultipleDifferentValues) { EditorGUILayout.HelpBox("The body has now been taken out of the simulation along with any attached colliders, joints or effectors.", MessageType.Info); } // Can only multi-edit if we have the same body-type. if (m_BodyType.hasMultipleDifferentValues) { EditorGUILayout.HelpBox("Cannot edit properties that are body type specific when the selection contains different body types.", MessageType.Info); } else { // Non-static options. m_ShowIsStatic.target = body.bodyType != RigidbodyType2D.Static; if (EditorGUILayout.BeginFadeGroup(m_ShowIsStatic.faded)) { // Kinematic options. m_ShowIsKinematic.target = body.bodyType != RigidbodyType2D.Kinematic; if (EditorGUILayout.BeginFadeGroup(m_ShowIsKinematic.faded)) { // Collider Mass. EditorGUILayout.PropertyField(m_UseAutoMass); // Only show mass property if selected objects have the same useAutoMass value. if (!m_UseAutoMass.hasMultipleDifferentValues) { // If we're using auto-mass but either the object is part of a prefab parent or is not active then we cannot show the calculated mass value. if (m_UseAutoMass.boolValue && targets.Any(x => PrefabUtility.IsPartOfPrefabAsset(x) || !(x as Rigidbody2D).gameObject.activeInHierarchy)) { EditorGUILayout.HelpBox("The auto mass value cannot be displayed for a prefab or if the object is not active. The value will be calculated for a prefab instance and when the object is active.", MessageType.Info); } else { EditorGUI.BeginDisabledGroup(body.useAutoMass); EditorGUILayout.PropertyField(m_Mass); EditorGUI.EndDisabledGroup(); } } EditorGUILayout.PropertyField(m_LinearDrag); EditorGUILayout.PropertyField(m_AngularDrag); EditorGUILayout.PropertyField(m_GravityScale); } EditorGUILayout.EndFadeGroup(); if (!m_ShowIsKinematic.target) { EditorGUILayout.PropertyField(m_UseFullKinematicContacts); } EditorGUILayout.PropertyField(m_CollisionDetection); EditorGUILayout.PropertyField(m_SleepingMode); EditorGUILayout.PropertyField(m_Interpolate); GUILayout.BeginHorizontal(); m_Constraints.isExpanded = EditorGUILayout.Foldout(m_Constraints.isExpanded, "Constraints", true); GUILayout.EndHorizontal(); var constraints = (RigidbodyConstraints2D)m_Constraints.intValue; if (m_Constraints.isExpanded) { EditorGUI.indentLevel++; ToggleFreezePosition(constraints, m_FreezePositionLabel, 0, 1); ToggleFreezeRotation(constraints, m_FreezeRotationLabel, 2); EditorGUI.indentLevel--; } // Provide end-user warning about the equivalence of all constraints on versus no Rigidbody2D component. if (constraints == RigidbodyConstraints2D.FreezeAll) { EditorGUILayout.HelpBox("Rather than turning on all constraints, you may want to consider removing the Rigidbody2D component which makes any colliders static. This gives far better performance overall.", MessageType.Info); } } EditorGUILayout.EndFadeGroup(); } serializedObject.ApplyModifiedProperties(); ShowBodyInfoProperties(); }
public override void OnInspectorGUI() { var body = target as Rigidbody2D; serializedObject.Update(); EditorGUILayout.PropertyField(m_BodyType); EditorGUILayout.PropertyField(m_Material); // Provide the user some information when simulation is turned off. EditorGUILayout.PropertyField(m_Simulated); if (!m_Simulated.boolValue && !m_Simulated.hasMultipleDifferentValues) { EditorGUILayout.HelpBox("The body has now been taken out of the simulation along with any attached colliders, joints or effectors.", MessageType.Info); } // Can only multi-edit if we have the same body-type. if (m_BodyType.hasMultipleDifferentValues) { EditorGUILayout.HelpBox("Cannot edit properties that are body type specific when the selection contains different body types.", MessageType.Info); } else { // Non-static options. m_ShowIsStatic.target = body.bodyType != RigidbodyType2D.Static; if (EditorGUILayout.BeginFadeGroup(m_ShowIsStatic.faded)) { // Kinematic options. m_ShowIsKinematic.target = body.bodyType != RigidbodyType2D.Kinematic; if (EditorGUILayout.BeginFadeGroup(m_ShowIsKinematic.faded)) { // Collider Mass. EditorGUILayout.PropertyField(m_UseAutoMass); // Only show mass property if selected objects have the same useAutoMass value. if (!m_UseAutoMass.hasMultipleDifferentValues) { // If we're using auto-mass but either the object is part of a prefab parent or is not active then we cannot show the calculated mass value. if (m_UseAutoMass.boolValue && targets.Any(x => PrefabUtility.IsPartOfPrefabAsset(x) || !(x as Rigidbody2D).gameObject.activeInHierarchy)) { EditorGUILayout.HelpBox("The auto mass value cannot be displayed for a prefab or if the object is not active. The value will be calculated for a prefab instance and when the object is active.", MessageType.Info); } else { EditorGUI.BeginDisabledGroup(body.useAutoMass); EditorGUILayout.PropertyField(m_Mass); EditorGUI.EndDisabledGroup(); } } EditorGUILayout.PropertyField(m_LinearDrag); EditorGUILayout.PropertyField(m_AngularDrag); EditorGUILayout.PropertyField(m_GravityScale); } EditorGUILayout.EndFadeGroup(); if (!m_ShowIsKinematic.target) { EditorGUILayout.PropertyField(m_UseFullKinematicContacts); } EditorGUILayout.PropertyField(m_CollisionDetection); EditorGUILayout.PropertyField(m_SleepingMode); EditorGUILayout.PropertyField(m_Interpolate); if (targets.Any(x => (x as Rigidbody2D).interpolation != RigidbodyInterpolation2D.None)) { if (Physics2D.simulationMode == SimulationMode2D.Update) { EditorGUILayout.HelpBox("The physics simulation mode is set to run per-frame. Any interpolation mode will be ignored and can be set to 'None'.", MessageType.Info); } if (Physics2D.simulationMode == SimulationMode2D.Script) { EditorGUILayout.HelpBox("The physics simulation mode is set to run manually in the scripts. Some or all selected Rigidbody2D are using an interpolation mode other than 'None' which will be executed per-frame. If the manual simulation is being run per-frame then the interpolation mode should be set to 'None'.", MessageType.Info); } } GUILayout.BeginHorizontal(); m_Constraints.isExpanded = EditorGUILayout.Foldout(m_Constraints.isExpanded, "Constraints", true); GUILayout.EndHorizontal(); var constraints = (RigidbodyConstraints2D)m_Constraints.intValue; if (m_Constraints.isExpanded) { EditorGUI.indentLevel++; ToggleFreezePosition(constraints, m_FreezePositionLabel, 0, 1); ToggleFreezeRotation(constraints, m_FreezeRotationLabel, 2); EditorGUI.indentLevel--; } } EditorGUILayout.EndFadeGroup(); } serializedObject.ApplyModifiedProperties(); ShowBodyInfoProperties(); }
public void OnSceneDrag(SceneView sceneView) { GameObject go = target as GameObject; if (!PrefabUtility.IsPartOfPrefabAsset(go)) { return; } var prefabAssetRoot = go.transform.root.gameObject; Event evt = Event.current; switch (evt.type) { case EventType.DragUpdated: Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene(); if (dragObject == null) { if (!EditorApplication.isPlaying || EditorSceneManager.IsPreviewScene(destinationScene)) { dragObject = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene); dragObject.name = go.name; } else { // Instatiate as regular GameObject in Play Mode so runtime logic // won't run into restrictions on restructuring Prefab instances. dragObject = Instantiate(prefabAssetRoot); SceneManager.MoveGameObjectToScene(dragObject, destinationScene); } dragObject.hideFlags = HideFlags.HideInHierarchy; } if (HandleUtility.ignoreRaySnapObjects == null) { HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>(); } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; Vector3 point, normal; if (HandleUtility.PlaceObject(evt.mousePosition, out point, out normal)) { float offset = 0; if (Tools.pivotMode == PivotMode.Center) { float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, normal); if (geomOffset != Mathf.Infinity) { offset = Vector3.Dot(dragObject.transform.position, normal) - geomOffset; } } dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(point + (normal * offset)); } else { dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10); } // Use prefabs original z position when in 2D mode if (sceneView.in2DMode) { Vector3 dragPosition = dragObject.transform.position; dragPosition.z = prefabAssetRoot.transform.position.z; dragObject.transform.position = dragPosition; } evt.Use(); break; case EventType.DragPerform: if (!DragPerform(sceneView, dragObject, go)) { return; } dragObject = null; evt.Use(); break; case EventType.DragExited: if (dragObject) { UnityObject.DestroyImmediate(dragObject, false); HandleUtility.ignoreRaySnapObjects = null; dragObject = null; evt.Use(); } break; } }
internal void OnSceneDragInternal(SceneView sceneView, int index, EventType type, Vector2 mousePosition, bool alt) { GameObject go = target as GameObject; if (!PrefabUtility.IsPartOfPrefabAsset(go)) { return; } var prefabAssetRoot = go.transform.root.gameObject; switch (type) { case EventType.DragUpdated: Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene(); if (m_DragObject == null) { // While dragging the instantiated prefab we do not want to record undo for this object // this will cause a remerge of the instance since changes are undone while dragging. // The DrivenRectTransformTracker by default records Undo when used when driving // UI components. This breaks our hideflag setup below due to a remerge of the dragged instance. // StartRecordingUndo() is called on DragExited. Fixes case 1223793. DrivenRectTransformTracker.StopRecordingUndo(); if (!EditorApplication.isPlaying || EditorSceneManager.IsPreviewScene(destinationScene)) { m_DragObject = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene); m_DragObject.name = go.name; } else { // Instatiate as regular GameObject in Play Mode so runtime logic // won't run into restrictions on restructuring Prefab instances. m_DragObject = Instantiate(prefabAssetRoot); SceneManager.MoveGameObjectToScene(m_DragObject, destinationScene); } m_DragObject.hideFlags = HideFlags.HideInHierarchy; if (HandleUtility.ignoreRaySnapObjects == null) { HandleUtility.ignoreRaySnapObjects = m_DragObject.GetComponentsInChildren <Transform>(); } else { HandleUtility.ignoreRaySnapObjects = HandleUtility.ignoreRaySnapObjects.Union(m_DragObject.GetComponentsInChildren <Transform>()).ToArray(); } PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); if (prefabStage != null) { GameObject prefab = AssetDatabase.LoadMainAssetAtPath(prefabStage.assetPath) as GameObject; if (prefab != null) { if (PrefabUtility.CheckIfAddingPrefabWouldResultInCyclicNesting(prefab, target)) { s_CyclicNestingDetected = true; } } } } DragAndDrop.visualMode = DragAndDropVisualMode.Copy; Vector3 point, normal; float offset = 0; if (index == 0) { s_PlaceObject = HandleUtility.PlaceObject(mousePosition, out s_PlaceObjectPoint, out s_PlaceObjectNormal); } point = s_PlaceObjectPoint; normal = s_PlaceObjectNormal; if (s_PlaceObject) { if (Tools.pivotMode == PivotMode.Center) { float geomOffset = HandleUtility.CalcRayPlaceOffset(m_DragObject.GetComponentsInChildren <Transform>(), normal); if (geomOffset != Mathf.Infinity) { offset = Vector3.Dot(m_DragObject.transform.position, normal) - geomOffset; } } m_DragObject.transform.position = Matrix4x4.identity.MultiplyPoint(point + (normal * offset)); } else { m_DragObject.transform.position = HandleUtility.GUIPointToWorldRay(mousePosition).GetPoint(10); } if (alt) { if (offset != 0) { m_DragObject.transform.position = point; } m_DragObject.transform.position += prefabAssetRoot.transform.localPosition; } // Use prefabs original z position when in 2D mode if (sceneView.in2DMode) { Vector3 dragPosition = m_DragObject.transform.position; dragPosition.z = prefabAssetRoot.transform.position.z; m_DragObject.transform.position = dragPosition; } // Schedule selection clearing for when we start performing the actual drag action s_ShouldClearSelection = true; break; case EventType.DragPerform: DragPerform(sceneView, m_DragObject, go); m_DragObject = null; break; case EventType.DragExited: // DragExited is always fired after DragPerform so we do no need to call StartRecordingUndo // in DragPerform DrivenRectTransformTracker.StartRecordingUndo(); if (m_DragObject) { DestroyImmediate(m_DragObject, false); HandleUtility.ignoreRaySnapObjects = null; m_DragObject = null; } s_ShouldClearSelection = false; s_CyclicNestingDetected = false; break; } }