Skip to content

Sprite Renderer Code Sample Explanation

Kenneth Hurley edited this page Jan 30, 2019 · 2 revisions

SpriteRenderTexture.cs Details

This class is setup to enable runtime sprite generation from 3D Model. The class script is added to a GameObject and is the link between the runtime generator and the Sprite Renderer class

public class SpriteRenderTexture : MonoBehaviour
{
    private SpriteRenderer _spriteRenderer;
    private PreviewGeneratorComponent _previewGeneratorComponent;
    private Texture2D _curTexture = null;
    private GameObject _curGameObject;

_spriteRenderer is the sprite component that is required and does the rendering of the sprite with RenderTexture attached.

_previewGenereratorComponent is the preview generator component that takes a 3D model with optional background and Postprocessing to render a texture to be used for the sprite.

_curTexture is the current rendered texture from the Preview Generator Component

_curGameObject is the target object to render for the sprite

    // Start is called before the first frame update
    void Start()
    {

        _spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
        _previewGeneratorComponent = gameObject.GetComponent<PreviewGeneratorComponent>();
        if (_previewGeneratorComponent != null)
        {
            SetSprite(true);
            _curGameObject = _previewGeneratorComponent.PreviewGenerator.GameObjectToRender;
        }
    }

The Start function simple gets the two necessary components that this class acts as the bridge between. It gets the SpriteRenderer component and the PreviewGeneratorComponent.

Next is calls the SetSprite function that grabs the Rendered Texture and creates a new Sprite, see code below in SetSprite.

    // Update is called once per frame
    void Update()
    {
        if (_previewGeneratorComponent != null)
        {
            // only gets new renderTexture if object changes or texture changes
            SetSprite(_curGameObject != 
                _previewGeneratorComponent.PreviewGenerator.GameObjectToRender);
        }
    }

The update function simple checks if the _curGameObject has changed and Sets the new RenderTexture in a new Sprite. This is useful since this class executes in the Editor as well.

   void SetSprite(bool doRender)
    {
        if (_previewGeneratorComponent != null)
        {
            if (_spriteRenderer != null)
            {
                Texture2D spriteTexture = _previewGeneratorComponent.GetRenderTexture(doRender);
                if ((spriteTexture != null) && (spriteTexture != _curTexture))
                {
                    _curTexture = spriteTexture;
                    Sprite sprite = Sprite.Create(spriteTexture,
                        new Rect(0.0f, 0.0f, spriteTexture.width, spriteTexture.height), 
                        new Vector2(.5f, .5f), 100.0f);
                    _spriteRenderer.sprite = sprite;
                }
            }
        }
        
    }

This function takes a bool that will Re-Render the texture (in case of an GameObject change). It does a few safety checks to make sure the components were found and then gets the Rendered Texture from the preview generator. The preview generator only executes if the GameObject being rendered is changed for the underlying rendered texture changes.