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 /
avatar image
1
Question by mcwool · Jan 19, 2016 at 09:37 AM · buttoninspectoreditor-scriptingpolygon collider 2d

CustomEditor PolygonCollider2D, edit button disappear

Hello

I thought I'd just add a quick and easy editor script to snap my PolygonCollider2D points to a desired value. Turns out I'm having some trouble.

The script works fine and does exactly what I want, but when my script is active the "edit collider" button disappears from the inspector.

Any help with this is much appreciated, it's 2am here so hopefully it's not just me being stupid. This is my editor script.

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor (typeof(PolygonCollider2D))]
 [CanEditMultipleObjects]
 public class Polygon2DSnap : Editor
 {
     private float snap = 0.32f;
 
     public override void OnInspectorGUI ()
     {
 
         EditorGUILayout.Space ();
         EditorGUILayout.Space ();
 
         snap = EditorGUILayout.FloatField ("Snap Value", snap);
 
         if (GUILayout.Button ("Snap Points")) {
             for (int t = 0; t < targets.Length; t++) {
                 PolygonCollider2D collider = (PolygonCollider2D)targets [t];
                 for (int c = 0; c < collider.pathCount; c++) {
                     var path = collider.GetPath (c);
                     for (int p = 0; p < path.Length; p++) {
                         float x = Mathf.RoundToInt (path [p].x / snap) * snap;
                         float y = Mathf.RoundToInt (path [p].y / snap) * snap;
                         path [p] = new Vector2 (x, y);
                         collider.SetPath (c, path);
                     }
                 }
             }
         }
         EditorUtility.SetDirty (target);
 
         EditorGUILayout.Space ();
         EditorGUILayout.Space ();
         EditorGUILayout.Space ();
 
         DrawDefaultInspector ();
     }
 }

Comment
Add comment · Show 4
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 mcwool · Jan 19, 2016 at 06:44 AM 0
Share

Ok, so I did some more research. Seems I will have to recreate this button if I don't put this function on a separate script ins$$anonymous$$d.

I really want the button on the PolygonCollider2D because I will use it on most objects, and I don't want to add a script every time. Any help is still appreciated (with the recreation of the "Edit Collider" button).

avatar image Naphier mcwool · Jun 19, 2016 at 06:37 PM 0
Share

Did you ever figure this out? I've not found any way to do this.

avatar image mcwool Naphier · Jun 19, 2016 at 08:12 PM 0
Share

Actually no, it event too deep and was too much work. So I went with adding my editor script to the gameobjects and having the button there ins$$anonymous$$d. Turned out pretty well since you could have the polygon collider automatically added when you add the editor script.

avatar image huulong · Aug 04, 2016 at 06:08 PM 0
Share

New in Unity 5.4.0 (actually a feature retrieved after a regression): Physics2D: Point editing is now allowed in Inspector for Edge/PolygonCollider2D.

This doesn't directly solve your problem but you can at least input the coordinates you want manually, and therefore snap them easily to integers. Now, if you have many points and you want to snap them, say, to the closest multiple of 0.25 it's another story... In this case, do as you said, by adding another script to your object. Until 5.4.0 I was using a script to edit coordinate manually and it was working pretty well.

0 Replies

· Add your reply
  • Sort: 

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

Script to create instance from in inspector 2 Answers

Lining up GUI 1 Answer

Set the Rect of PopupWindowContent 1 Answer

How to record hideFlags for Undo/Redo 0 Answers

How To Force an Inspector to Repaint 3 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