Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Mr.Hal · Jul 10, 2014 at 08:32 PM · gameobjecteditorclickableselecting objectshandle

Select un-selectable Objects with mouse.

(Problem)

I have came across a pretty annoying issue, I need to select a game object that does not have a single selectable component. Which is infuriating because I have alot of those kinds of objects, I need a handle, gizmo, or a dummy object that only has a presence in editor, that I can click on in the editor. I want to see if the community has a solution first before I try to make one.

(Context)

I made a pathfinding system involving a lot of waypoints for a complex 2D platformer. All the waypoints sit on a single parent named Waypoints, There is at least 100 or more waypoints. My current way to select a object is to scan through the objects to find the one I want to edit. Do to the nature of the garbage collector, it has to dispose of memory often because what I'm assuming is the inspectors fault, Leaving a small delay before I can continue scanning through the objects, Annoying and a little bearable but definatly adding to the currently fubar situation.

(What I hope exists)

An example script to help select those un-selectable objects(With a mouse).

A handle function(that I haven't found by name) so I can make a editor script for the waypoint comp to make it selectable.

Something that exists in the doc that pertains to this situation.

A feature request link, so I can upvote it.

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

1 Reply

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

Answer by ricardo_arango · Jul 10, 2014 at 09:03 PM

You can draw handles using the Handle class:

http://docs.unity3d.com/ScriptReference/Handles.PositionHandle.html

You can use Gizmos to draw the Spheres in the scene:

http://docs.unity3d.com/ScriptReference/Gizmos.DrawSphere.html

And you can detect the intersection of a ray and a sphere using this code:

 public static bool IntersectRaySphere(Ray ray, Vector3 sphereOrigin, float sphereRadius, ref float t, ref Vector3 q)
 {
     Vector3 m = ray.origin - sphereOrigin;
     float b = Vector3.Dot(m, ray.direction);
     float c = Vector3.Dot(m, m) - (sphereRadius * sphereRadius);
     // Exit if rís origin outside s (c > 0)and r pointing away from s (b > 0)
     if ((c > 0.0f) && (b > 0.0f)) return false;
     float discr = (b * b) - c;
 
     // A negative discriminant corresponds to ray missing sphere
     if (discr < 0.0f) return false;
 
     // Ray now found to intersect sphere, compute smallest t value of intersection
     t = -b - Mathf.Sqrt(discr);
 
     // If t is negative, ray started inside sphere so clamp t to zero
     if (t < 0.0f) t = 0.0f;
     q = ray.origin + t * ray.direction;
     return true;
 }


t is the distance to the sphere, q is the point of collision.

ray is the Ray to the scene from the camera, which you can calculate using

 var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

sphereOrigin is the position of each waypoint.

sphereRadius is the radius of each waypoint.

What you need to do is that when your Waypoints GameObject is selected, you will display all the waypoints using Gizmos.DrawSphere.

When the user clicks in the scene:

 var mouseUpEvent = Event.current.type == EventType.MouseUp;

you will then use the IntersectRaySphere function above to detect if a sphere/waypoint was clicked and store the reference to it (you will have to iterate all the waypoints).

With a reference to a waypoint (and it's data) you can draw a position Handle with Handles.PositionHandle, which will allow you to edit the position of the waypoint.

Comment
Add comment · Show 4 · 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 Mr.Hal · Jul 10, 2014 at 10:10 PM 0
Share

Now that I can click the object and get a tangible reference to it, I'm assu$$anonymous$$g that I just assign the reference to Selection.activeGameObject...? Really wish they made a function or class attribute that basically did all this. Thanks Ricardo!

avatar image ricardo_arango ♦♦ · Jul 10, 2014 at 11:16 PM 0
Share

Do you need a GameObject for each waypoint? If it's just a point you can simply have an array of Vector3 in a single "Wayponts" class. And use the Gizmos and Handles to do all the editing in the scene.

The handles don't care about object selection, they are simply gizmos that are drawn at a point in space. So all you need is a position to represent the waypoint/handle. If you really need to have a GameObject per waypoint object, then yes, you can change the selection with Selection.activeGameObject.

avatar image Mr.Hal · Jul 11, 2014 at 06:27 PM 1
Share

It would have been a great idea to have each waypoint to not have there own game object, And I certainly could do what you said. However the waypoints are going to be added onto prefabs and I realise that the complexity could grow even larger than ever if I made them all on one object(Also I'm using the transforms attached to all of them). I could make each prefab have a container for those waypoints but I think there is really no positive performance impact or any other practical reason to do so, Beside it being neat a pretty. Sounds like I should experiment into it however, maybe punch them all into a scriptable object so I could edit them as the game is playing. I would have to do it later in dev, Its not worth throwing more time into if it works fine right now.

I eventually was able to make my waypoints operate sorta like the light probe system in the editor, When you select a GameObject with a Waypoint component, you will have the ability to click other GameObjects with the same component. However when you click something else that doesn't, You lose that ability.

Here is a code snip for anyone interested. You are going to have to format it a bit.

     [DrawGizmo (GizmoType.NotSelected | GizmoType.Pickable)]
     static void OnDrawGizmo (Waypoint aTarget, GizmoType aGizmoType){
     if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<Waypoint> () != null) {
     Gizmos.color = Color.cyan;
     Gizmos.DrawSphere (aTarget.transform.position,SphereRadius);}}
     void OnSceneGUI(){
     foreach (Waypoint point in Waypoint.Waypoints) {
     if (point != null) {
     if (point != selected) {
     if (Event.current.type == EventType.$$anonymous$$ouseUp) {                        float t = 0f;
 Vector3 q = Vector3.zero;
 if (IntersectRaySphere (HandleUtility.GUIPointToWorldRay(Event.current.mousePosition), point.transform.position, SphereRadius, ref t, ref q)) {                                    Selection.activeGameObject = point.gameObject;}}}}}



avatar image Elecman · Apr 08, 2015 at 12:48 AM 0
Share

$$anonymous$$r. Hal, you saved my day :-)

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

22 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

Related Questions

Show Editor Handles at Object Location 0 Answers

Clickable empty GameObject in the editor 3 Answers

How would you override Editor object dragging? 0 Answers

Any way to lock an object from changes in the editor? 4 Answers

in-editor cloning of game objects 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