Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create Polygon Collider 2D from or set to SpriteRenderer at runtime

Discussion in '2D' started by nsfnotthrowingaway, Mar 7, 2016.

  1. nsfnotthrowingaway

    nsfnotthrowingaway

    Joined:
    Feb 18, 2016
    Posts:
    48
    Two related questions:
    Is there a way to set the PolygonCollider2D to a Sprite the way it does when you first add it. For instance if I do:
    gameObject.AddComponent<PolygonCollider2D> ();

    The collider is in the shape of the sprite by default. Is there a command to make it recalculate itself to the sprite (after changing images for instance).

    If there is such a command, is it possible to set the alpha threshold for what gets into the shape? And if there isn't such a command is there a way to set the threshold it uses upon adding a PolygonCollider2D component to a sprite, so that I can increase the tolerance for less opaque, but not fully transparent, pixels to be included in the generated polygon?
     
  2. nsfnotthrowingaway

    nsfnotthrowingaway

    Joined:
    Feb 18, 2016
    Posts:
    48
    I'd still like to know answers to the above questions, but I'm also curious about
    UnityEngine.UI.Image.eventAlphaThreshold . The documentation states:

    In order for lower values to work, the sprite used by the Image must have readable pixels. This can be achived by enabling Read/Write enabled in the advanced Texture Import Settings for the sprite and disabling atlassing for the sprite.

    Where do I find the advanced Texture Import Settings for the sprite?

    Edit: I was able to locate these settings by setting Texture Type to Advanced under the texture import settings. Is there any way to do this when loading a sprite with image.overrideSprite = Sprite.Create()?

    In case anyone has any additional insight, I just want to be able to load a texture off of an arbitrary disk location (not necessarily in a subfolder of the exported project or the asset directory). I need to be able to drag and drop these images. I already got the basic drag and drop code working for a sprite or a ui.image, but I only want the drag to start if the clicked pixel has an alpha value above 0. Also, these images may be rotated or scaled.
     
    Last edited: Mar 7, 2016