|
Post by britannis on Mar 31, 2016 22:49:24 GMT
Unity offers tags like 'Player' and 'MainCamera' but I always hear that use of those tags has a performance impact. A lot of people recommend only using tags in Awake() to grab references, although some note that excessive grabbing of tags from many objects will still impact performance.
So I'm curious - what are your thoughts on tags? On one hand I like the fact that they help prevent spaghetti code since an object can be found via tag, but on the other hand I'm curious if having an object post an event when it enters the game and then having other objects get the sender type of the event is bad practice.
|
|
|
Post by Admin on Apr 1, 2016 19:18:57 GMT
Unity provides a lot of convenient things like tags because Unity is a great prototyping tool. It allows you to create something complex and do it fast. Some of those prototype architectures end up making it into live products. I think this falls into one of those "pre-mature optimization" categories. In my opinion if you like it and aren't personally noticing a performance impact then it isn't worth stressing about.
I don't personally use tags. There are a lot of ways to architect a project without needing to let Unity scan the entire object graph such as: * Good object hierarchy. Use GetComponent or a variation which checks hierarchy - these should be faster than the project wide Find methods * Direct reference in a script through the inspector * The Singleton pattern. You will hear a lot of people bash that as well, but at least it doesn't suffer from performance issues. You could also consider more complex alternatives to this pattern like dependency injection or service locator. * Notifications or events. Checking the kind of class like you mentioned isn't super fast but you'd have to do it a lot before you saw an issue. Ideally you should just know what kind of class it is or not care what kind of class it is - but that takes experience to architect correctly.
|
|