PO/Library/PackageCache/com.unity.2d.spriteshape@5.1.4/Samples~/Extras/Scripts/GenerateSpriteShapes.cs

44 lines
1.7 KiB
C#
Raw Normal View History

2022-01-12 10:06:03 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.U2D;
// Please add this Component to Camera or some top level object on each loadable scene.
public class GenerateSpriteShapes : MonoBehaviour
{
// Once all SpriteShapes are rendered, remove this Component if On or remove it from elsewhere.
public bool destroyOnCompletion = true;
void OnGUI()
{
// Loop all invisible SpriteShapeRenderers and generate geometry.
SpriteShapeRenderer[] spriteShapeRenderers = (SpriteShapeRenderer[]) GameObject.FindObjectsOfType (typeof(SpriteShapeRenderer));
CommandBuffer rc = new CommandBuffer();
rc.GetTemporaryRT(0, 256, 256, 0);
rc.SetRenderTarget(0);
foreach (var spriteShapeRenderer in spriteShapeRenderers)
{
var spriteShapeController = spriteShapeRenderer.gameObject.GetComponent<SpriteShapeController>();
if (spriteShapeRenderer != null && spriteShapeController != null)
{
if (!spriteShapeRenderer.isVisible)
{
spriteShapeController.BakeMesh();
rc.DrawRenderer(spriteShapeRenderer, spriteShapeRenderer.sharedMaterial);
// Debug.Log("generating shape for " + spriteShapeRenderer.gameObject.name);
}
}
}
rc.ReleaseTemporaryRT(0);
Graphics.ExecuteCommandBuffer(rc);
// SpriteShape Renderers are generated. This component is no longer needed. Delete this [or] remove this Component from elsewhere.
if (destroyOnCompletion)
Destroy(this);
}
}