Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Zaxgod · Feb 17, 2014 at 11:11 AM · 2dperformancetexture2d

Exploding texture into it's individual pixels

I'm trying to implement a system where upon taking damage, an enemy sprite loses one of it's pixels, until it reaches a point where it's health = 0, whereby all remaining pixels "explode".

Removing individual pixels is fine, I get a random coordinate for the pixel to remove, set it to Color.clear, create a new sprite with the modified texture. I then create a new empty gameObject for the removed pixel, create a 1x1 texture with the color of the chosen pixel, position it correctly, add a RigidBody2D and 1x1 Collider2D, and apply a upward velocity and gravity to it.

This all works fine, what is struggling is when I explode the sprite, I'm creating a lot of these individual pixels all at once, which is having an impact on performance. I've tried some optimisations, I'm storing all the unique textures in a dictionary on start and grabbing them when necessary, rather than creating them from scratch. I'm at a loss though, I assume there's more I can do to optimise this but I'm thinking maybe this is just too intensive, I assume this can't be achieved with a particle effect, because I want the pixels to already be created and be in their correct position and also be affected by gravity and physics.

Sorry for the wall of text, if I've missed anything just ask.

Comment
Add comment · Show 12
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 whydoidoit · Feb 17, 2014 at 11:16 AM 0
Share

Yeah that would normally be a particle effect as particles are individually light weight - what you are trying to do sounds like it's a pretty big load on the system presu$$anonymous$$g your objects are a decent size. A particle system would allow you to place each individual particle with scripting and presumably there is only one texture with a different color on the material?

avatar image Zaxgod · Feb 17, 2014 at 11:23 AM 0
Share

Well the sprite's textures are pretty small, and in the current one I'm testing there are only around 15 different colours, so 15 texture2D's are created when it's initialized. There is just one texture for the enemy sprite though yes, not sure what you mean about the material? So are you saying you think it'd be possible to do it with a particle system and have the particles initialised in the correct positions?

avatar image Zaxgod · Feb 17, 2014 at 11:31 AM 0
Share

I'll check that out, thanks for the quick response, I'll let you know how it goes!

avatar image emalb · Feb 17, 2014 at 01:51 PM 0
Share

Are you creating new game objects for each of the exploded pixels at the time of the explosion?

You might get a speed up by keeping a collection of game objects lying around (disabled/invisible) and just setting their colours, positions, etc. at the time of the explosion.

avatar image Zaxgod · Feb 18, 2014 at 02:18 PM 0
Share

Okay I implemented an object pool, however if didn't really have any effect on performance, which makes me think my issue is down to all the pixels having rigidBody / Collider components. I also tried using the SetParticles idea, but I can't seem to set an initial velocity for the particle. All I'm doing at the $$anonymous$$ute is this:

 ParticleSystem.Particle[] particles;
 
     void Start()
     {
         particles = new ParticleSystem.Particle[10];
 
         for (int i = 0; i < 10; i++)
         {
             particles[i].position = new Vector3(Random.Range(-20, 20), Random.Range(-20, 20));
             particles[i].color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
             particles[i].size = Random.Range(1, 10);
             particles[i].velocity = new Vector3(Random.Range(-20, 20), Random.Range(-20, 20));
         }
         
         particleSystem.SetParticles(particles, particles.Length);
     }

The particles don't move though, am I missing something obvious?

Show more comments

2 Replies

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

Answer by Globotix · May 25, 2017 at 04:24 PM

The trick is to use an object pool and create all your pixel sprites before hand. I created an asset that works this way https://www.assetstore.unity3d.com/en/#!/content/89628

Comment
Add comment · 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
1

Answer by whydoidoit · Feb 17, 2014 at 11:26 AM

Yes you can set an array of particles using SetParticles and that allows you to control a bunch of the useful stuff.

I may not have exactly visualised what you are doing - but hopefully this will help...

Comment
Add comment · 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

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

23 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

Related Questions

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 1 Answer

BoxCast, OverlapArea, and Raycasting; ground detection questions 0 Answers

PackTextures Horizontally? (1 Row) or Alternative Method 1 Answer

2D movement stutter 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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