How the Check Works
The CheckPerformanceOptimization
is designed to identify and report potential performance optimization suggestions within scripts. This check helps developers identify common performance pitfalls and suggests improvements. There are a number of things being checked here – mostly quick fixes in your code to make it run faster!
When to Use This Check
- Performance Optimization: Identify and address potential performance issues in your scripts, enhancing the overall performance of your Unity project.
- Best Practices Adherence: Ensure adherence to performance best practices by leveraging suggestions provided by the check.
How to Use the Check
- Configure Checks: Use the Unity Editor to configure which performance checks you want to enable or disable based on your project requirements.
How to fix errors like this
If you see results from this check, that means there are improvements you can do on your code:
- Performance suggestion: don’t use AddComponent: it is often faster to add components in the editor and turn them off/on when needed
- Performance suggestion: don’t use Find: this is a very slow function. When using it, make sure to pass it a sorting parameter to optimize for speed. Try caching and assigning these results in the editor, if possible
- Performance suggestion: don’t use GetComponent: try caching and assigning these results in the editor, if possible
- Performance suggestion: don’t use Debug log method: this can result in very slow gameplay if used too much. We recommend implementing a stand-in function like Dbg.Log that you can turn off for builds
- Performance suggestion: don’t use String-based method invocation: this can be very slow so don’t use it if there is a way to avoid it
- Performance suggestion: don’t use Camera.main: this is very slow, especially when done in Update functions. If possible, use Camera.main once and store the result in a variable.
- Performance suggestion: don’t use Unity object comparison with null: a faster way would be to use
System.Object.ReferenceEquals(myObject, null)
. Also see here.