Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Grannita · Apr 08 at 02:05 PM · animatorspriterenderercache

How to clear cached images from sprite renderer

I have a sprite renderer and an animator. I use them to display a png sequence of a lot of images. Then I want to continue with another sprite renderer and animator to show different image sequence. And so on.

My question is how to release the memory from each gameobject with the attached sprite renderer.

Sorry I am new to unity - any help appreciated.,

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Apr 08 at 03:04 PM 0
Share

Destroy( texture ); releases the texture memory. But I'm not sure about sprites specifically - probably the same call or maybe Destroy( sprite ); will do the job.

One important thing to note here is that read-only textures allocate pixel data on the GPU memory only, while read-write textures allocate it both both on the GPU and CPU memory (so prefer RO).

avatar image Grannita andrew-lukasik · Apr 11 at 09:04 AM 0
Share

Thank you @andrew-lukasik for your answer! Unfortunately it didn't work. I can see from the profiler that the Texture memory is never released.

avatar image andrew-lukasik Grannita · Apr 11 at 09:32 AM 0
Share

I can tell you from my practical experience, where this was required for app not to crash - Destroy( texture ), as a memory release mechanism, does work.

The devil is in the details. But I won't insist.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by andrew-lukasik · Apr 11 at 02:14 PM


Here is an experiment. It:

  • reads all png images from given location as Texture2Ds & converts them to Sprites

  • displays them all in sequence as animation frames

  • deallocates everything

  • repeats


Note the square wave-like memory allocation graph it produced and let it be a proof this allocation/deallocation bussines works just fine.


rick-n-roll


LetsRick.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using IO = System.IO;
 public class LetsRick : MonoBehaviour
 {
     [SerializeField] string _directoryPath = "D:/rick-n-roll/";
     [SerializeField] float _delayBetweenFrames = 0.16f;
     SpriteRenderer _spriteRenderer = null;
     IEnumerator Start ()
     {
         _spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
         
         var wait = new WaitForSeconds( 0.5f );
         string[] files = IO.Directory.GetFiles( _directoryPath );
         if( files.Length!=0 )
         {
             List<Sprite> frames = new List<Sprite>(128);
             while( true )
             {
                 // load all images as sprites, at once:
                 foreach( string filePath in files )
                 {
                     var bytes = IO.File.ReadAllBytes( filePath );
                     Texture2D texture = new Texture2D( 2 , 2 , TextureFormat.RGB24 , 0 , false );
                     if( ImageConversion.LoadImage( texture , bytes , true ) )
                     {
                         Sprite sprite = Sprite.Create( texture , new Rect(0,0,texture.width,texture.height) , new Vector2( 0.5f , 0.5f ) );
                         frames.Add( sprite );
                     }
                     else Debug.LogWarning($"image load failed, path: \"{filePath}\"");
                 }
 
                 // display all images in sequence:
                 var frameDuration = new WaitForSeconds( _delayBetweenFrames );
                 for( int i=0 ; i<frames.Count ; i++ )
                 {
                     _spriteRenderer.sprite = frames[i];
                     yield return frameDuration;
                 }
 
                 // free memory up:
                 for( int i=frames.Count-1 ; i!=-1 ; i-- )
                 {
                     Object.Destroy( frames[i].texture );
                     Object.Destroy( frames[i] );
                 }
                 frames.Clear();
 
                 // a lof of time to check it out in the profiler:
                 yield return wait;
             }
         }
         else Debug.LogWarning($"no files found for path: \"{_directoryPath}\"");
     }
 }



screenshot-2022-04-11-161921.png (198.9 kB)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grannita · Apr 13 at 10:05 AM 0
Share

Thank you so much for your time! I tried it and I can see it works as for the memory. I can see the images on the scene tab but not on the game tab. I guess I am doing something wrong..

avatar image Grannita · Apr 13 at 01:39 PM 1
Share

Ok works fine with a small change!

When we create the sprite we should change new Vector2( texture.width*0.5f , 0 ) to something like new Vector2(0.5f, 0.5f). This sets the pivot point.

In the documentation it is said "This is a Vector2 relative to the rect where Vector2(0.0f, 0.0f) is the bottom left and Vector2(1.0f, 1.0f) is the top right.".

Otherwise the sprite will be moved to a crazy position and this was the reason I couldn't see it on Game tab.

avatar image andrew-lukasik Grannita · Apr 13 at 02:46 PM 0
Share

Good point! I totally missed that important detail

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2d sprite Animation - Fire at frame 2 Answers

Change sprite in a sprite renderer with an animation 0 Answers

2D animation sprite renderer not updating 1 Answer

Can't change sprite when Animator component is attached? 4 Answers

How to swap a spriteRenderers source sprite at runtime when used by animator 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges