I want to nullify the z-coordinate for all new gameobjects that get created.
I try produce the following behaviour:
- Copied gameobjects inherit their z-coordinate
- Gameobjects that are dragged from the project view to the scene view get a 0 z-coordinate while being dragged into the scene
- Once a gameobject is dropped (mouseUp), I can modify the z-coordinates again, even by dragging in the scene view
Never actually needed something like you need, but I have two tips that should help:
- if you use CTRL+ALT+SHIFT while dragging an object into the scene, it will attach to surface. It will actually follow geometry so as to allow perfect positioning and stacking
- I’m certain you can ‘intercept’ instantiation by means of an editor script (for example the assetpostprocessor intercepts asset import), but sadly I don’t know the right routine you need for this case.
We did this in our project. A Scene object has to be selected for it to work, but it’s useful. It speeds up our scene construction process.
import UnityEngine
[CustomEditor(Scene)]
class SceneEditor (Editor):
scene as Scene
def Awake():
scene = target as Scene
def OnSceneGUI():
if Event.current.type == EventType.Repaint:
# draw scene-mode warning in scene window
if Event.current.type == EventType.DragPerform:
for r in DragAndDrop.objectReferences:
o = EditorUtility.InstantiatePrefab(r)
go = o as GameObject
go.transform.parent = scene.transform
zeroGround = 0f
ccam = Camera.current
mouseRay = ccam.ScreenPointToRay(Vector3(Event.current.mousePosition.x, ccam.pixelHeight - Event.current.mousePosition.y, 0.0f))
if mouseRay.direction.z > 0.0f:
t = -mouseRay.origin.z / mouseRay.direction.z
mouseWorldPos = mouseRay.origin + t * mouseRay.direction
mouseWorldPos.z = zeroGround
go.transform.position = mouseWorldPos
Event.current.Use()
DragAndDrop.AcceptDrag()