Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by khamosh132 · Oct 22, 2016 at 09:39 AM · instantiateterraininputeditor-scriptingmouseposition

How to get mouse click world position in the scene view in editor script?

I am trying to write an editor script for Unity Terrain that instantiates objects where I click on terrain in the scene view (pretty much like the tree generation of terrain). Input.GetMouseButtonDown(0); appears to be not working in scene view.

How can i get the mouse button down event and the position of click in scene view in world coordinates?

Thanks.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by FlightOfOne · Nov 26, 2018 at 04:24 PM

@Adam-Mechtley's, pointed me in the right direction. Here's what I came up with:

 using UnityEngine;
 
 #if UNITY_EDITOR
 using UnityEditor;
 /// <summary>
 /// 
 /// </summary>
 [ExecuteInEditMode]
 public class ClickSpawn : MonoBehaviour
 {
     private void OnEnable()
     {
         if (!Application.isEditor)
         {
             Destroy(this);
         }
         SceneView.onSceneGUIDelegate += OnScene;
     }
 
     void OnScene(SceneView scene)
     {
         Event e = Event.current;
 
         if (e.type == EventType.MouseDown && e.button == 2)
         {
             Debug.Log("Middle Mouse was pressed");
 
             Vector3 mousePos = e.mousePosition;
             float ppp = EditorGUIUtility.pixelsPerPoint;
             mousePos.y = scene.camera.pixelHeight - mousePos.y * ppp;
             mousePos.x *= ppp;
 
             Ray ray = scene.camera.ScreenPointToRay(mousePos);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit))
             {
                 //Do something, ---Example---
                 GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 go.transform.position = hit.point;
                 Debug.Log("Instantiated Primitive " + hit.point);
             }
             e.Use();
         }
     }
 }
 #endif
Comment
Add comment · Show 2 · 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 Brad-Newman · Mar 08, 2019 at 09:04 PM 1
Share

The Unity API supports different display scaling so this code that multiplies by pixelsPerPoint is required. I can confirm this code works with Unity 2018.3

avatar image tim12332000 · Sep 30, 2020 at 02:02 AM 1
Share

Thanks bro you save my time

avatar image
4

Answer by Adam-Mechtley · Oct 24, 2016 at 08:54 AM

 if (Event.current.type != EventType.MouseDown || Event.current.button != 0)
     return;
 // convert GUI coordinates to screen coordinates
 Vector3 screenPosition = Event.current.mousePosition;
 screenPosition.y = Camera.current.pixelHeight - screenPosition.y;
 Ray ray = Camera.current.ScreenPointToRay (screenPosition);
 RaycastHit hit;
 // use a different Physics.Raycast() override if necessary
 if (Physics.Raycast (ray, out hit))
 {
     // do stuff here using hit.point
     // tell the event system you consumed the click
     Event.current.Use ();
 }
Comment
Add comment · 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
-2

Answer by CoMANDOR · Oct 22, 2016 at 11:01 AM

The same problem I have. Just found this question on Google

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 tim12332000 · Sep 30, 2020 at 01:54 AM 0
Share

This is a good idea bro.

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

109 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Infinite floor with object as terrain. 1 Answer

Need Help instantiating GameObjects in an array using the middle mouse wheel. whew. 1 Answer

How to handle multiple terrains? 0 Answers

Mouse position from new InputSystem is not correct and is not the same as Input.mousePosition 0 Answers

Mesh Selection in Dropdown?? 2 Answers


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