Search Unity

Create from sprite missing?

Discussion in 'Editor & General Support' started by Per, Nov 12, 2013.

  1. Per

    Per

    Joined:
    Jun 25, 2009
    Posts:
    460
    In the documentation for Polygon Collider 2D (and videos) there is an section "Create From Sprite", seems to be missing here completely... anyone else?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's from an earlier version; guess the docs weren't updated properly for the released version. At least for the "edit collider" toggle, you can hold shift instead. The other stuff was removed.

    --Eric
     
  3. Per

    Per

    Joined:
    Jun 25, 2009
    Posts:
    460
    So there's no automatic way to optimize/poly-reduce the mesh now then? Seems a little silly to remove that, I can understand the alpha threshold being extraneous, but I can't see a way to get rid of many points quickly and some points can't be removed at all if it generates them internally on the mesh.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The alpha threshold was useful. I was told those features were removed because they were having some issues with them, but they might return eventually. You can remove any points by holding down control while you're editing the collider. The collider you use doesn't have to bear any resemblance to the auto-generated collider if you don't want it to.

    --Eric
     
  5. Per

    Per

    Joined:
    Jun 25, 2009
    Posts:
    460
    I understand that you can manually remove points using the control key, but you can't remove internal points that way and it's going to be a lot of work with many meshes or even just a single larger/more complex shape. It's not the end of the world as 2d path reduction algorithms aren't complex to make yourself, but it just seems an odd thing to omit and makes a bit of a nuisance. Hopefully they'll be back.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can remove almost any points that way, except that it won't let you have fewer than 3 points in a polygon (internal or external). Also, a possibility would be to make an image in the shape of the polygon collider that you want, add the polygon collider component to auto-generate the collider, then swap out the image for a different one.

    --Eric
     
  7. sun_spider

    sun_spider

    Joined:
    Dec 3, 2013
    Posts:
    6
    My sprite has a bunch of transparency and it generates a poly that's very complex and doesn't edit effectively... sure would be nice to be able to build a simple shape around the sprite from scratch or have more effective editting capabilities
     
  8. Pixelcloud-Games

    Pixelcloud-Games

    Joined:
    Apr 29, 2011
    Posts:
    160
    Just noticed the discussion.

    Sorry for the advertisement, but maybe you would like to give 2D ColliderGen a try, it has a lot of options (alpha threshold, vertex count, disable/enable individual holes, etc) to generate 2D or 3D Colliders from Unity's built-in sprites (and other packages as well).

    https://www.assetstore.unity3d.com/#/content/7540

    Cheers,
    Harald
     
  9. Engidia_EduardBosch

    Engidia_EduardBosch

    Joined:
    Nov 14, 2013
    Posts:
    25
    Hi,

    I want to share an editor script I'm using to remove internal collider shapes from selected gameobjects. It works perfect to remove internal colliders, but it's useful also to remove all shapes except one in case you only want your collider to have one shape. In this case, be careful because the remaining shape won't bound perfectly your object in some cases.

    One example when it works perfect removing internal shapes:
    $good_case.jpg

    You have to take care if you use this script to remove all collider shapes except one. Here is an example that what can happen:
    $bad_case.jpg

    You can download the class here: View attachment $RemoveInternalShapes.cs

    Hope it helps anyone

    PD: I adapted the code from this post, and allowed to work with multiple selected GameObjects http://forum.unity3d.com/threads/22...remove-shape?p=1504572&viewfull=1#post1504572.

    Code (csharp):
    1.  
    2. /**
    3.  * Author: Engidia SCP - Eduard Bosch (eduardbosch@engidia.com)
    4.  * File: RemoveInternalShapes.cs
    5.  */
    6.  
    7. using UnityEngine;
    8. using UnityEditor;
    9. using System.Collections;
    10.  
    11. public class RemoveInternalShapes : MonoBehaviour {
    12.  
    13.     [MenuItem ("GameObject/Remove Internal Shapes", true)]
    14.     static bool Validate ()
    15.     {
    16.         // Check if there are any selected GameObject with more than one path
    17.         foreach (GameObject lObj in Selection.gameObjects)
    18.         {
    19.             PolygonCollider2D lCollider = lObj.GetComponent<PolygonCollider2D>();
    20.             if (lCollider!=null  lCollider.pathCount > 1)
    21.             {
    22.                 return true;
    23.             }
    24.         }
    25.  
    26.         return false;
    27.     }
    28.  
    29.  
    30.  
    31.     [MenuItem ("GameObject/Remove Internal Shapes")]
    32.     static void RemoveShapes ()
    33.     {
    34.         foreach (GameObject lObj in Selection.gameObjects)
    35.         {
    36.             PolygonCollider2D lCollider = lObj.GetComponent<PolygonCollider2D>();
    37.             if (lCollider==null)
    38.             {
    39.                 continue;
    40.             }
    41.  
    42.             // Allow undo action
    43.             Undo.RecordObject (lCollider, "Remove Interior Shapes");
    44.  
    45.  
    46.             // Get the shape that are more to the left than the others to take it as the exterior path
    47.             int   lExteriorShape = 0;
    48.             float lLeftmostPoint = Mathf.Infinity;
    49.  
    50.             Vector2[] lPath;
    51.  
    52.             for (int i=0, length=lCollider.pathCount; i<length ; ++i)
    53.             {
    54.                 lPath = lCollider.GetPath(i);
    55.  
    56.                 foreach (Vector2 lPoint in lPath)
    57.                 {
    58.                     if (lPoint.x < lLeftmostPoint)
    59.                     {
    60.                         lExteriorShape = i;
    61.                         lLeftmostPoint = lPoint.x;
    62.                     }
    63.                 }
    64.             }
    65.  
    66.             // Initialize collider with exterior path
    67.             lPath = lCollider.GetPath (lExteriorShape);
    68.  
    69.             // Set only the exterior path
    70.             lCollider.pathCount = 1;
    71.             lCollider.SetPath (0, lPath);
    72.         }
    73.     }
    74. }
    75.  
     
    Last edited: Feb 21, 2014
    Arkade likes this.
  10. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Hello Eduard

    Thx for your script, i'm interested on using it, but I don't get how to.
    Do you have to place the .cs on the objects you wish to use it on ?
    and from then, what action will I have to do ?

    Thx.
     
  11. deus_duke

    deus_duke

    Joined:
    Mar 12, 2012
    Posts:
    11
    It's an editor script, so you just add the script to a folder called Editor in your project view. Then a menu item shows up for you to use under GameObject.

    Hope that helps, thanks!
     
    krousty_bat likes this.
  12. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
    Thx a lot
    I actually found this recently for free too:
    (Edit: it seems it's now $5 ;(... not expensive but too bad it's not free anymore)
    Unity Advanced Polygon Collider : http://u3d.as/mZU

    It worked very well with many options and possible configurations,
    Thanks again for your answer and your script !!!

     
    Last edited: Mar 24, 2016