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 Draco Nared · Jul 02, 2015 at 09:56 AM · editorraycastcustom-inspectorgizmos

[Custom Inspector] Selecting Gizmo in Scene View and issues

Hi,

I am creating my own waypoint system for AI and decided to implement custom inspector for handling waypoints. I didn't want to use gameobjects as there would be too much work for other users. That's why I decided to draw gizmos and select them in scene view. I found a solution how to do it, however, it doesn't seem to work as I wanted and just selects the first gizmo I drew, even when I click other gizmos.

Here is some code with my implementation:

 public class AIWaypointsInspector : Editor {
 
 //_aiWaypoints.positions is a vector3[] with waypoints positions; IMPORTANT NOTE: I previously added two waypoints there to check the script
 //_aiWaypoints.selected is a bool[] which shows which waypoint is selected
 public override void OnInspectorGUI() {
         if (this._aiWaypoints == null) {
             this._aiWaypoints = (AIWaypoints)target;
         }
             this.DrawWaypoints();
     }
 
     private void DrawWaypoints() {
             Vector3 vec;
             this._moreThanOneSelected = false;
             for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
                 EditorGUI.BeginChangeCheck();
                 bool sel = EditorGUILayout.ToggleLeft("", this._aiWaypoints.selected[i]);
                 if (EditorGUI.EndChangeCheck()) {
                     if (sel != this._aiWaypoints.selected[i]) {
                         this._aiWaypoints.selected[i] = sel;
                         SceneView.RepaintAll();
                     }
                 }
                 EditorGUI.BeginChangeCheck();
                 vec = EditorGUILayout.Vector3Field("", this._aiWaypoints.positions[i]);
                 if (EditorGUI.EndChangeCheck()) {
                     if (vec != this._aiWaypoints.positions[i]) {
                         this._aiWaypoints.positions[i] = vec;
                         SceneView.RepaintAll();
                     }
                 }
             }
     
     }
 
     private void OnSceneGUI() {
         if (this._aiWaypoints == null) {
             this._aiWaypoints = (AIWaypoints)target;
         }
         Vector3 vec;
         
         if (Event.current.type == EventType.MouseDown) {
             if (Event.current.button == 0) {
                 Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                 SphereCollider sph = this._aiWaypoints.gameObject.AddComponent<SphereCollider>();
                 sph.radius = 1f;
                 for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
                     sph.center = this._aiWaypoints.positions[i];
                     if (Physics.Raycast(ray)) {
                         this._aiWaypoints.selected[i] = true;
                         break;
                     }
                 }
                 DestroyImmediate(this._aiWaypoints.gameObject.GetComponent<SphereCollider>());
             }
         }
         for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
             if (this._aiWaypoints.selected[i]) {
                 EditorGUI.BeginChangeCheck();
                 vec = Handles.PositionHandle(this._aiWaypoints.positions[i], Quaternion.identity);
                 if (EditorGUI.EndChangeCheck()) {
                     if (vec != this._aiWaypoints.positions[i]) {
                         this._aiWaypoints.positions[i] = vec;
                     }
                 }
             }
         }
 
         if (GUI.changed) {
             EditorUtility.SetDirty(this._aiWaypoints);
         }
     }}

I don't know what I do wrong. I will be grateful for any suggestions and help.

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
0
Best Answer

Answer by Draco Nared · Jul 03, 2015 at 07:39 AM

I managed to find answer that goes around the problem. To make waypoints interactable and movable, instead of Vector3 array, I decided to use GameObject array. Also, I added a GameOject that is a child of target's transform and parent to array of GameObjects and is using HideFlags to hide in hierarchy. This way I don't have waypoints in the hierarchy but I can move them as I want to, so mission accomplished.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

What should be handled in the custom inspector version of a script? 1 Answer

Drawing Editor Inspector GUI based on selected/current prefab (CustomPropertyDrawer) 2 Answers

Animation stops Recast from working 0 Answers

How to choose the transform tool from script ? 4 Answers

Why does this function, when called from OnDrawGizmosSelected, produce a different result and kill the 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