Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by noonche · Jan 15, 2014 at 05:23 AM · editorsnap

Extending Unity's Grid

You can hold control while manipulating an object to move it in grid increments. I would like to extend the editor to snap an object back to the grid if you move it while holding down control.

I can't seem to figure out how to do this.

I thought about making a custom EditorWindow, but those only have their update called while the window is open.

I can add a delegate to the editor's update using [InitializeOnLoad], but then I can't detect when control is being held down.

Any ideas?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jan 15, 2014 at 09:36 AM 0
Share

All runtime classes work in the editor too. You can use Input anywhere, so detecting when control is being held down is easy.

Editor Extensions are quite limited, I suggest you use a $$anonymous$$enu item as a shortcut for snapping the object to the nearest grid relative to the world origin, ins$$anonymous$$d of trying to replace current editor functionality.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jamora · Jan 15, 2014 at 02:07 PM

I don't really understand why you would want to implement the snapping to grid yourself, as Unity already provides the functionality.

The way you can catch the control-key being pressed in a scene view (or rather, while a scene view is being shown) is by the following code:

 using UnityEngine;
 using UnityEditor;
 
 public static class ControlDown{
 
     [MenuItem("NoticeControlDown/Start")]
     public static void InitializeStart(){
         Debug.Log("started");
         SceneView.onSceneGUIDelegate += SceneGUI;
     }
 
     [MenuItem("NoticeControlDown/Stop")]
     public static void InitializeStop(){
         Debug.Log("stopped");
         SceneView.onSceneGUIDelegate -= SceneGUI;
     }
 
     private static void SceneGUI(SceneView sceneView){
         if(Event.current.control)
             Debug.Log("Control is down");
     }
 }

This script should be located in an Editor folder.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image noonche · Jan 17, 2014 at 06:15 AM 0
Share

Thanks that worked.

The reason I want to make grid snapping myself is because it's an inconvenient menu item in unity. If something is off grid (say at 2.1 when you have a grid with spacing of 1) holding control and moving the object will move the object to 1.1 or 3.1. We want it to get back on the grid.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to set snapping for scale to units? 1 Answer

Rotation Snapping is adding harmful floats! 1 Answer

Editor drag&drop override 1 Answer

Editor Script - Access the User's Snap Settings? 1 Answer

Wheres the snap to pixel option in the shader/material editor? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges