Adam's Lair Forum

game development and casual madness
It is currently 2017/03/26, 11:17

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
Hey,

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?


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

Joined: 2013/05/11, 22:30
Posts: 1933
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)


Top
 Profile  
 
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 6 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