• 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
1
Question by hardmode2236 · Nov 25, 2013 at 12:06 AM · platformdisabledpreformance

does disabled code affect performance

if you have some portions of code disabled say like you have a game with mobile platform controls, as well as computer controls. then when you play it on the computer, do the disabled mobile controls portion have any negative effects besides making the game use more total memory?

do people usually make different versions of games for each platform, or just one dynamic game that picks the correct sections of code to run based on whats its being played on?

Comment
Add comment · Show 1
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 hardmode2236 · Nov 25, 2013 at 05:33 AM 0
Share

thank you all for taking the time to help me with these informative answers :D !

3 Replies

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

Answer by Bunny83 · Nov 25, 2013 at 02:29 AM

No, if you have code that you don't use (i.e. you don't execute it) it has no impact on the performance. Like you said it might result in a slightly larger memory footprint, but code is usually one of the smallest things in your application.

However you can even exclude certain code fragments from being compiled into the final app by using pre-processor tags. If you do so, keep in mind that code in an UNITY_IPHONE seciont literally doesn't exist when building for any other platform (PC, Android, ...). So make sure you don't build too tight dependencies between classes / methods. One way is in addition to the preprocessor directives to use the strategy pattern like iwaldrop showed you.

That way you can exclude things ment for other platforms but you don't rely on a certain class.

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 TunerSymphon · Nov 25, 2013 at 04:55 AM

As I understand, it won't affect performance, as it won't be actively using those sections of script, but it will affect load time, albeit only by a small amount. Short answer, no, I don't believe so.

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 iwaldrop · Nov 25, 2013 at 02:04 AM

It depends on your implementation. You could actually spawn an instance of the appropriate class depending on the control method you're using and never have the memory footprint required to have instances of control methods that aren't used on the target platform. Take this example:

 using UnityEngine;
 
 public interface IControl
 {
     void DetectInput();
 }
 
 public class JoystickControl : IControl
 {
     public void DetectInput()
     {
         // detect and do stuff with player input from a joystick
         Debug.Log("detecting input from joystick");
     }
 }
 
 public class KeyboardAndMouseControl : IControl
 {
     public void DetectInput()
     {
         // detect and do stuff with player input from the keyboard and mouse
         Debug.Log("detecting input from keyboard and mouse");
     }
 }
 
 
 public class InputController : MonoBehaviour
 {
     public bool useJoystick;  // for demonstrational purposes, replace with target platform sensing logic
     private IControl playerControlMethod;
 
     void Awake()
     {
         if (useJoystick)
             playerControlMethod = new JoystickControl();
         else
             playerControlMethod = new KeyboardAndMouseControl();
     }
 
     void Update()
     {
         playerControlMethod.DetectInput();
     }
 }
Comment
Add comment · Show 4 · 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 Bunny83 · Nov 25, 2013 at 02:19 AM 0
Share

That wouldn't make any difference. The "code" is still there. code also needs memory, even when not used, however it's usually just a few $$anonymous$$B depending on how much code ;)

avatar image iwaldrop · Nov 25, 2013 at 02:21 AM 0
Share

Right, but the instances wouldn't be allocated on the heap. The OP asked about disabled code, so I offered a method of instantiating only the objects that would be required.

Of course other methods include preprocessor directives, but now I'm just preaching to the choir. :)

avatar image Bunny83 · Nov 25, 2013 at 02:34 AM 0
Share

:) But in your case you have no data at all in your concrete instances. The instances you create just adds overhead in your case. So at the end your implementation needs more memory than having all in one class and just ignore one part.

Also code is never instantiate. Code is always static. The only thing you can have are virtual overrides, but they are just references to static methods. $$anonymous$$ethods belong to the class, not to an instance. They are not part of the "data".

avatar image iwaldrop · Nov 25, 2013 at 02:41 AM 0
Share

True. I didn't mean to lead anyone astray with my quick and dirty example of an alternative which didn't include member variables.

Thanks for covering my tracks. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Wall Jump script 2 Answers

Variable value as code?! 1 Answer

Array index is out of range 1 Answer

How to post code samples without it messing up 2 Answers

how do i use two animations on the same object?? 1 Answer


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