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
3
Question by SinisterRainbow · Apr 21, 2013 at 05:52 AM · texturematerialsruntimelayerschange

Removing Texture at Runtime Without replacing it

I'm pretty new to Unity still, I have a multi-layered material where I need to remove one of the textures of the material during runtime. I don't want to waste memory setting a new clear texture. Can anyone point me in the right direction?

I should note, using Destroy on a layer seems to destroy the entire material. For example: Destroy(gameObject.renderer.materials[1].mainTexture);

If I do this:

gameObject.renderer.materials[1].mainTexture = new Texture2D(1,1);

it works, but may be a waste of memory. Open to something better. Thanks.

Comment
Add comment · Show 3
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 Owen-Reynolds · Apr 21, 2013 at 02:02 PM 1
Share

A 1x1 texture really does use 4 bytes plus overhead. It isn't grown to 512x512, for example.

avatar image SinisterRainbow · Apr 21, 2013 at 07:35 PM 0
Share

Thanks Owen, this is good to know. I come from other game engines where this would not be true.

avatar image Owen-Reynolds · Apr 22, 2013 at 09:30 PM 1
Share

$$anonymous$$ust be based on pre-graphics card code. For example, at least until a few years ago, visual C# ("Forms") hand-blended pixels -- something a graphics card does much much faster.

A modern graphics card wants to rescale all textures. If you draw an X by X texture over exactly X by X screen pixels, it still scales by 1.0 and does all the adjacent pixels math. Any "modern" game engine will probably take advantage of auto-rescaling.

1 Reply

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

Answer by Bunny83 · Apr 22, 2013 at 09:42 PM

This isn't really one "multilayered material" but simple two materials attached to the same renderer. That simply means the object is drawn twice, one time with the first material and one time with the second. You also don't replace / remove the material, you just set a new texture fot the material.

Multiple materials are usually used when your Mesh contains submeshes, so each material corresponds to a submesh. If there's only one submesh in the mesh it is simply drawn twice.

To remove the second material you have to replace the materials array and only include those materials you want to keep:

 // C#
 Material[] tmp = gameObject.renderer.materials;
 gameObject.renderer.materials = new Material[] {tmp[0]};

May i ask why there are actually two materials when you want to remove one at runtime? I don't get the point.

Comment
Add comment · Show 2 · 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 SinisterRainbow · Apr 23, 2013 at 01:47 AM 0
Share

Ahh..simple.. Guess modifying the mat directly is not possible then? As for your question.. I have a game board with tiles that will have numbers added to them or removed, when removed it isn't needed again. This is kind of an aside, as I'm not particularly worried about drawcalls or texture memory for this application, but this won't be my last one either. ;) Thanks.

avatar image Bunny83 · Apr 23, 2013 at 07:12 AM 1
Share

If you just want to remove the texture from the second material, you can set it to null:

  gameObject.renderer.materials[1].mainTexture = null;

Destroying the texture would remove it "forever" (in terms of the current execution).

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

13 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

Related Questions

LoadImageIntoTexture Runtime Change 1 Answer

change terrain texture and tree at runtime 1 Answer

Texture change on multi material character 1 Answer

Change and add Terrain Texture During Runtime 1 Answer

Same Objects Shared Material each with a different offset 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