Пример #1
0
 extern public static void GenerateSpriteShape(SpriteShapeRenderer renderer, SpriteShapeParameters shapeParams, ShapeControlPoint[] points, SpriteShapeMetaData[] metaData, AngleRangeInfo[] angleRange, Sprite[] sprites, Sprite[] corners);
 public static void GenerateSpriteShape(SpriteShapeRenderer renderer, SpriteShapeParameters shapeParams, ShapeControlPoint[] points, SpriteShapeMetaData[] metaData, AngleRangeInfo[] angleRange, Sprite[] sprites, Sprite[] corners)
 {
     SpriteShapeUtility.GenerateSpriteShape_Injected(renderer, ref shapeParams, points, metaData, angleRange, sprites, corners);
 }
 private static extern void GenerateSpriteShape_Injected(SpriteShapeRenderer renderer, ref SpriteShapeParameters shapeParams, ShapeControlPoint[] points, SpriteShapeMetaData[] metaData, AngleRangeInfo[] angleRange, Sprite[] sprites, Sprite[] corners);
Пример #4
0
    void StartSpriteShape()
    {
        if (init == true)
        {
            return;
        }


        Rect rect = new Rect();

        switch (spriteShapeBounds)
        {
        case SpriteShapeBounds.Renderer:
            Bounds bounds = new Bounds();
            bool   found  = false;
            foreach (Component c in gameObject.GetComponents <Component>())
            {
                if (c.GetType().ToString() == "UnityEngine.U2D.SpriteShapeRenderer")
                {
                    UnityEngine.U2D.SpriteShapeRenderer ssr = (UnityEngine.U2D.SpriteShapeRenderer)c;

                    bounds = ssr.bounds;
                    found  = true;

                    break;
                }
            }
            if (found)
            {
                rect = new Rect(bounds.center.x - bounds.extents.x - transform.position.x, bounds.center.y - bounds.extents.y - transform.position.y, bounds.extents.x * 2, bounds.extents.y * 2);
            }
            else
            {
                Debug.LogWarning("Destruction2D: SpriteShape game object is missing SpriteShapeRenderer");
            }
            break;

        case SpriteShapeBounds.Collider:
            List <Polygon2D> polygons = Polygon2DList.CreateFromGameObject(gameObject);
            if (polygons.Count < 1)
            {
                Debug.LogError("Destruction2D: SpriteShape game object is missing collider");
                return;
            }

            rect = polygons[0].GetBounds();
            break;
        }

        GameObject newGamObject = new GameObject();

        newGamObject.name = gameObject.name;
        newGamObject.transform.position   = transform.position;
        newGamObject.transform.rotation   = transform.rotation;
        newGamObject.transform.localScale = transform.localScale;
        newGamObject.transform.parent     = transform.parent;

        foreach (Component c in gameObject.GetComponents <Component>())
        {
            if (c.GetType().ToString() == "UnityEngine.PolygonCollider2D")
            {
                continue;
            }
            if (c.GetType().ToString() == "UnityEngine.EdgeCollider2D")
            {
                continue;
            }
            if (c.GetType().ToString() != "UnityEngine.U2D.SpriteShapeController" && c.GetType().ToString() != "UnityEngine.U2D.SpriteShapeRenderer")
            {
                if (CopyComponent(c, newGamObject, gameObject) == true)
                {
                    Destroy(c);
                }
            }
        }

        Destruction2D destructible = newGamObject.GetComponent <Destruction2D>();

        destructible.init = true;

        Destroy(gameObject.GetComponent <EdgeCollider2D>());

        PolygonCollider2D collider = gameObject.GetComponent <PolygonCollider2D>();

        if (collider != null)
        {
            newGamObject.AddComponent <PolygonCollider2D>();
            Destroy(collider);
        }

        gameObject.transform.parent     = Destruction2DManager.instance.transform;
        gameObject.name                 = "Buffer: " + gameObject.name;
        gameObject.transform.rotation   = Quaternion.Euler(0, 0, 0);
        gameObject.transform.localScale = new Vector3(1, 1, 1);

        destructible.buffer = Destruction2DManager.CreateCustomBuffer(gameObject);

        destructible.pixelsPerUnit = scaledPixelsPerUnit;

        destructible.filterMode = filterMode;

        int textureWidth  = (int)(rect.width * destructible.pixelsPerUnit);
        int textureHeight = (int)(rect.height * destructible.pixelsPerUnit);

        destructible.renderTexture = new RenderTexture(textureWidth, textureHeight, 32);
        destructible.outputTexture = new Texture2D(textureWidth, textureHeight);

        destructible.buffer.renderCamera.orthographicSize = (rect.height) / 2;
        destructible.buffer.renderCamera.transform.Translate(rect.center.x, rect.center.y, -1f);
        destructible.buffer.renderCamera.farClipPlane  = 5;
        destructible.buffer.renderCamera.targetTexture = destructible.renderTexture;

        newGamObject.transform.Translate(rect.center.x, rect.center.y, -1f);

        destructible.spriteRenderer = newGamObject.AddComponent <SpriteRenderer>();

        // Set Up Destructible
        destructible.buffer.SetDestructible(destructible);

        destructible.buffer.renderCamera.enabled = true;

        destructible.originalSprite = Sprite.Create(destructible.outputTexture, new Rect(0.0f, 0.0f, destructible.renderTexture.width, destructible.renderTexture.height), new Vector2(0.5f, 0.5f), pixelsPerUnit, 2, SpriteMeshType.FullRect);
    }