Adam's Lair Forum

game development and casual madness
It is currently 2017/04/25, 12:31

All times are UTC + 1 hour [ DST ]

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: 2017/01/13, 11:52 
Junior Member
Junior Member

Joined: 2016/05/15, 18:27
Posts: 45
Role: Hobbyist

So I saw Adam discouraging modifying resources at run-time. That's also part of the engine, since modifying something at run-time permanently changes it (meaning the changes apply in the editor as well). How are we supposed to handle changes like this? For example, I have a heldWeapon gameobject that moves to my player. For making it look like different weapons, I change the material's texture. This permanently changes the material in the editor, so I have to add some initialization logic to reset it.

What would be the best way to handle these types of changes at run-time?

PostPosted: 2017/01/13, 15:12 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 1948
Location: Germany
Role: Professional
Hey there,

modifying resources at runtime is not discouraged in general, it's fully supported! What I meant was just that usually it's not what you want :)

I think we solved this in the chat, but just for the public: Instead of using a Material for rendering directly, then modifying it at runtime and ending up with changes in the editor, you could use a BatchInfo object that was created from that Material and modify this one instead.

  • BatchInfo: Describes how a batch of vertices is rendered.
  • Material: A BatchInfo that is persistently stored as a resource.

Instead of using the Material directly, you can always retrieve material.Info to generate a BatchInfo with the exact same settings that you can modify without affecting the Material it was created from. Store it somewhere and use it for rendering instead of the Material itself. The usual drawing APIs accept both BatchInfo and Material.

If you're only using predefined renderers such as SpriteRenderer and others, you'd simply dragdrop the Material to their CustomMaterial (BatchInfo) slot instead of the SharedMaterial (Material) slot, creating a local copy of that Material just for this single renderer instance. Or you'll make the switch at runtime in some init method, so your changes to the Material will still be propagated to all objects using it.

Blog | GitHub | Twitter (@Adams_Lair)

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]

Who is online

Users browsing this forum: No registered users and 8 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group