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
2
Question by Crazydadz · Apr 16, 2012 at 11:35 PM · instantiatesceneviewtool

Instantiate object in middle of Editor Scene View

I'm creating a tool that instantiate spawnPoint by clicking a GUILayout.Button. I want to instantiate it at the middle of the Editor Scene View and not always at Vector3(0f,0f,0f).

The tool is not for InGame only for production.

Thank you!

Just to help other to have an idea of how we can do it. The function instantiate an object on top of the gameobject that raycast hit from the middle of the Editor Scene view.

(Little work to do on it, because if the hitted GameObject have a X or Z rotation, the instantiated object isn't exactly positionned on top of it)

     //Instantiate a spawnPoint at the middle of the screen on the object that raycast hit
 void SpawnInstantiating ()
 {
     // Check if sceneView is the current drawing one
     if (SceneView.currentDrawingSceneView == null){
         Debug.Log ("Click on scene view before adding a spawnPoint!");
         return;
     }
     
     //Cast a ray from the middle of the screen
     Camera sceneCam = SceneView.currentDrawingSceneView.camera;
     Vector3 rayPos = sceneCam.ViewportToWorldPoint (new Vector3 (0.5f, 0.5f, 1000f));
     Ray ray = new Ray (sceneCam.transform.position, rayPos);
     RaycastHit hit;
     if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
         
         //Check if object it isn't the background. If True instantiate spawnPoint on object hit
         if (hit.collider.tag != "Background") {
             Vector3 spawnPos;
             
             //If object tag isn't floor, add lossyscale/2 to ajust the Y position of the spawnPoint
             if (hit.collider.tag != "Floor")
                 spawnPos = new Vector3 (hit.point.x, hit.collider.bounds.center.y + (hit.transform.lossyScale.y / 2f), hit.point.z);
             else
                 spawnPos = new Vector3 (hit.point.x, hit.collider.bounds.center.y, hit.point.z);
             GameObject _go = Instantiate (Resources.Load ("SpawnPoint"), spawnPos, Quaternion.identity) as GameObject;
             _go.name = "Unassigned_SpawnPoint";
             Selection.activeGameObject = _go;
         } else
             Debug.Log ("Need to point a Gameobject!");
     } else
         Debug.Log ("Need to point a GameObject!");
 }
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 ryanmillerca · Nov 17, 2020 at 04:19 PM 0
Share

Apologies for resurrecting this, but there's an easier and better way built-in to Unity. Use SceneView.$$anonymous$$oveToView as defined here: https://docs.unity3d.com/ScriptReference/SceneView.$$anonymous$$oveToView.html

 SceneView.lastActiveSceneView.$$anonymous$$oveToView(transform);

1 Reply

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

Answer by rutter · Apr 16, 2012 at 11:39 PM

You could use `Camera.ViewportToWorldPoint()`, passing in a vector like (0.5, 0.5, 1).

If you're in-game, you'll usually want to do this with `Camera.main`. In-editor, last I knew, most people use `Camera.current` to get the scene view camera, but if that doesn't work you can try some searches like "How do I find the scene view camera in the Unity editor?"

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 Crazydadz · Apr 17, 2012 at 12:05 AM 2
Share

Nice! Thank a lot. Just need to add some line to ajust it well.

         Selection.activeObject = SceneView.currentDrawingSceneView;
         Camera sceneCam = SceneView.currentDrawingSceneView.camera;
         Vector3 spawnPos = sceneCam.ViewportToWorldPoint(new Vector3(0.5f,0.5f,10f));

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting gameobjects to display in scene view 0 Answers

How to remove all children objects & how to instantiate prefab as child to specific object 5 Answers

Editor Extention - How to instantiate? 1 Answer

Is instantiating prefabs on awake more expensive to performance than setting prefabs in the scene view? 1 Answer

Checking if object intersects? 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