How to create a custom height snapping tool in Unity Editor - unity3d

I am making a strategy game and I need to have a tool which places the objects above the terrain while I am dragging them in Unity Editor when I work on level design.
Basically I want to get result like here:
https://www.youtube.com/watch?v=YI6F1x4pzpg
but I need it to work before I hit the Play button in Unity Editor.
Here is a tutorial
https://www.youtube.com/watch?v=gLtjPxQxJPk
where the author of it made a tool which snaps the object to the terrain height when a key is pressed. I need the same to happen automatically whenever I place an object over my terrain. And I want my tool to adjust the Y position of the object automatically even while I am dragging it inside of the editor.
Also just to clarify: I don't need grid snapping and I don't need this functionality during the gameplay. I just need to have a tool for my level design work.
Please give me a clue where to start with it.
Thanks!

There is this tag you can apply to classes so they do call their regular events during editor mode already: https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
A trivial way then would be to apply this to a special class/object which regularly "finds" all objects from the game object hierarchy. Then it shall filter that list for the ones you want to snap to the axis and enforce their Y.

Related

How to modify behaviour of a VR controller's pointer

My university colleagues and I are trying to develop a Virtual Reality project for university where we use an Oculus headset and create a scene with a mouse where you can select and click and drag different objects in the scene. You are supposed to be stationary and move one of the controllers as if it was a mouse. However, we want to modify the behaviour of the controller to better fit the 3D environment. When an object is not selected, we want to interpolate the depth of the cursor according to the interpolation of the nearest objects. There is a paper that we were shown in class that we are supposed to drag inspiration from, and it achieved this kind of behaviour of the cursor with a normal mouse but I can't seem to find any information on how they did it. Our final goal would be to compare both ways of managing the scene and assess which one is better. We are using Unity with VRTK as suggested by our professor, but we and can't really seem to be able to access the mouse's file on how it moves or its behaviour, and we are kind of lost on where to go. Could someone help with this?
Here is the paper where they talk about it:
https://dl.acm.org/doi/pdf/10.1145/3491102.3501884
We so far have tried creating a simple scene and adding objects with different behaviours as well as a controller instance, but we seem to only be able to modify the events of the mouse and not its specific behaviour.
Kind regards and thanks

Is it possible to turn off the visibility of the parent-grid of the tilemap in Unity?

I'm trying to turn off the visibility of the grid of the tilemap. I feel like it's easier to draw and design without grids and I couldn't find any solution to my problem yet. To clarify, I don't mean the main-grid from unity, I know how to change its visibility, I'm talking about the Grid that appears when you create a tilemap.
Simply disable the Grid Gizmo via the Gizmos tab in the Scene View:
(The editor I'm using here is Unity 2022.1.9f1. If you're using an older version, the icons might look a bit different, but should work the same.)

is there an option in unity to hide objects, UI elements, 3d model?

I'm new to unity and i'm trying to make an AR app, animal card game.
I have one scene/one AR camera (i'm not sure if it would be better to have one scene per animal) , I have one UI button that pop up when an image target is detected and I want to make more, one for each animal but my problem is that I can only put the buttons in one place in the canvas.
I would like to know if there is somewhere an option to make my object invisible just in the editor ?
thank you.
There's two ways in which you can hide objects in the editor - its either using Layers (you can set layer visibility in the editor and distribute objects among layers.
The alternative is to use gameobject's HideFlags but its realtively more complex (and easy to lost objects in the scene).
If you want to hide objects in the gameplay, theres two ways - you either disable the whole gameobject (using gameObject.SetActive), or you disable its rendering component (using GetComponent + enabled variable. Third way exists for UI objects - that's using CanvasGroup component

How to detect click on object in Unity3D' SceneView from a custom Editor Window

To speed up scene setup, I developed a bunch of custom editors. I want to add a new feature on one of those editor that would detect when an object has been clicked in the SceneView (and thus prevent a selection change, as this editor purpose is to link objects between them).
I managed to achieve that by preventing the default selection and raycasting from the mouse position on the SceneView. My only issue is that when the objects don't have any collider, it won't work (and adding a collider on those objects is out of question). And yet, the editor is capable of detecting clicks on objects without colliders.
All my google-fu brought are "Add a collider" approach (not possible) or a "Do your own raycasting" (a bit overkill to my taste).
Please note that I'm not looking for a workaround, I found a bunch of those already (Bounds.IntersectRay on all scene objects, reverting selection OnSelectionChange, using Selection.gameObjects, etc). I'm really looking for the ideal solution (even if not documented) out of curiosity to get a better understanding of what is possible or not from a custom editor perspective.

2d ordering in unity

i am making a 2d game in unity.
i am missing a feature that cocos2d has that when you create something in the parent it keeps its z ordering inside that parent.
the problem that i am experiencing is with popups, when i create a popup or menu i want to make sure that it gets displayed above everything else in the scene or any other popup, but i cant make sure that it does because some elements in the scene may have z value that even though they are inside a parent object may appear above my popups if i am not careful. same goes for popups if i would like to have a popup on top of another popup i will have to make sure that there are no intersecting children between them.
does anyone know is there any way of containing an object to be rendered only inside its parent z order like cocos2d does? or should this problem be approached differently.
When working in 2D mode in Unity, you should just focus on setting the Order In Layer value of the sprites to have them display as you want. Leave all the z values in 0.