How Can I Optimize Unity Logic Code, Lua, and Hotfix Runtime Performance?
Logic code optimization is one of the most difficult parts of performance optimization because it is highly dependent on project architecture, coding patterns, and runtime frameworks. Unlike rendering or loading bottlenecks, logic-related CPU pressure is often hidden inside nested call stacks, scripting layers, or hot-update runtimes. In Unity projects, performance issues may come from native C# logic, Lua execution, or hot-update frameworks like ILRuntime. By using GameOptim’s profiling APIs together with GOT Online’s visualization capabilities, teams can identify bottlenecks more accurately and optimize both logic execution and scripting runtime overhead.
About GameOptim GameOptim helps Unity developers identify memory issues, rendering bottlenecks, and performance regressions through automated profiling and cloud based performance analysis. Explore more: 🌐 Website: www.gameoptim.com https://www.gameoptim.com/?fopt=blog 📘 Blog: www.gameoptim.com/blog/ https://www.gameoptim.com/blog/ 💼 LinkedIn: www.linkedin.com/company/gameoptim/ https://www.linkedin.com/company/gameoptim/ 🎥 YouTube: GO.PerformanceLab https://www.youtube.com/@GO.PerformanceLab 💬 Discord: GameOptim https://discord.gg/4Jh6hj9gRw ⭐ GitHub: GameOptim https://github.com/GameOptim/unity mobile performance guide 💻 Dev: GameOptim https://dev.to/gameoptim https://uwa overseas images.oss us east 1.aliyuncs.com/Blog/HeadImage/OptimizationGuide21%280%29.jpg Summary Logic code bottlenecks must be analyzed through call stacks, not assumptions. JobSystem can effectively reduce main thread logic pressure. Lua bottlenecks usually come from high self time or excessive call frequency. Reverse call analysis is useful for locating real Lua hotspots. Lua–C interaction is one of the most common hidden CPU costs. ILRuntime usually introduces higher CPU and heap overhead than native C . Core combat logic should avoid heavy hotfix runtime execution whenever possible. 3.8 Logic Code Core Concepts Logic code optimization is highly project dependent and difficult to quantify with fixed standards. GameOptim provides: Custom logic profiling APIs Function level split profiling GOT Online stack visualization This allows teams to: Split large logic functions Measure actual execution cost Verify optimization effectiveness For complex logic systems, profiling before optimization is always the first step. Best Practices Use JobSystem for Parallel Logic An increasing number of teams use Unity’s JobSystem to move part of their logic from the main thread to worker threads. Recommended scenarios: Parallelizable logic Large scale calculations Independent data processing Benefits: Reduces main thread CPU pressure Improves frame stability Better CPU utilization For parallel friendly logic: JobSystem is strongly recommended. 3.9 Lua Core Concepts GOT Online Lua mode provides: Full Lua call stack visualization CPU time tracking Reverse call analysis Function names follow: FunctionName@FileName:LineNumber This helps locate: High cost functions CPU spikes Source location Important: If Lua runs in bytecode: LineNumber becomes 0 Therefore: Use Lua source code during profiling whenever possible. 1 Forward Call Analysis — Summary Curve 75 https://uwa ducument img.oss cn beijing.aliyuncs.com/GameOptim/3.9Lua/75.png The curve displays: Total Lua execution time Top 5 functions sorted by average time This helps quickly locate long term bottlenecks. Function List 76 https://uwa ducument img.oss cn beijing.aliyuncs.com/GameOptim/3.9Lua/76.png Sorted by: Average Time Descending Shows: Function Name Total CPU Time Scene CPU Time Average Time Click any function to enter detailed analysis. 2 Forward Call Analysis — Single Function Page Runtime Screenshot 77 https://uwa ducument img.oss cn beijing.aliyuncs.com/GameOptim/3.9Lua/77.png Helps correlate function spikes with actual runtime scenarios. CPU Curve + Call Count Curve 78 https://uwa ducument img.oss cn beijing.aliyuncs.com/GameOptim/3.9Lua/78.png Use this to determine: Sustained high cost Sudden spikes High frequency accumulation Key observations: High Avg Time → optimize function body High Calls → reduce frequency Function Stack Detail 79 https://uwa ducument img.oss cn beijing.aliyuncs.com/GameOptim/3.9Lua/79.png Supported scopes: Full stack Scene stack Frame stack Important indicators: Total % Self % Total Time Self Time Calls Avg Time Significant Frames This helps determine whether the bottleneck is: In the function itself In child functions 3 Reverse Call Analysis Unlike forward analysis: Reverse analysis sorts functions by: Self Time This makes it easier to find: Real CPU hotspots Frequent expensive paths Hidden self cost bottlenecks 4 Reverse Call Analysis — Single Function Page 80 https://uwa ducument img.oss cn beijing.aliyuncs.com/GameOptim/3.9Lua/80.png Indicators differ slightly: Self % Self Time Calls Avg Time Use reverse analysis to answer: Where exactly is this function consuming its own time? Best Practices Optimize High Self Time Functions If Avg Time per call is high: Simplify logic Reduce internal loops Optimize local calculations Reduce High Frequency Calls Even low cost functions become expensive when called too frequently. Focus on: Repeated update logic Nested loops Polling logic Reduce Lua–C Cross Language Calls This is one of the most common hidden costs. Problems: High overhead Higher power consumption Increased heat Important: Lua time includes called C time. Optimization: Move repeated cross language logic into C . Only expose parameters to Lua. This significantly reduces overhead. 3.10 ILRuntime Core Concepts ILRuntime is still used by some legacy mobile projects. However, based on GameOptim’s experience: Projects using ILRuntime usually show weaker performance in: Logic execution Heap memory usage Main causes: Runtime interpretation overhead Boxing allocations Debug overhead Recommended alternatives: Lua HybridCLR Best Practices CPU Optimization 1. Avoid Core Combat Logic Do not put: Combat loops Heavy calculations High frequency logic inside ILRuntime. Use pure static C instead. 2. Build in Release Mode Always use: Release DLL to reduce runtime overhead. 3. Android Optimization For Android: Use: Mono runtime Reflection loading This can achieve near native C performance. Heap Memory Optimization Reduce ILRuntime Usage Especially avoid: Value type heavy logic because boxing causes extra allocations. Disable Debug Macros Use: This reduces heap allocations. Official optimization guide: https://ourpalm.github.io/ILRuntime/public/v1/guide/FastQA.html https://ourpalm.github.io/ILRuntime/public/v1/guide/FastQA.html Key Takeaways Logic code optimization must start from profiling. JobSystem is effective for parallel workloads. Lua forward analysis helps locate total cost bottlenecks. Lua reverse analysis helps locate self cost bottlenecks. Lua–C calls should be minimized whenever possible. ILRuntime should avoid critical high frequency systems. Native C remains the best choice for performance critical gameplay. FAQ Why is my logic code consuming too much CPU? Usually because of: Large functions High frequency calls Hidden child function costs Should I use JobSystem? Yes, if your logic is parallelizable. Why is Lua performance unstable? Common reasons: Expensive function bodies Too many calls Too many Lua–C interactions Is ILRuntime suitable for combat logic? Generally no. Its CPU and memory overhead are usually much higher. Continue reading the series 1. Why Do Mobile Games Crash, Lag, or Overheat? A Unified Framework for CPU, GPU, and Memory Optimization in Unity https://www.gameoptim.com/blog/post/OptimizationGuide1 2. How to Control Runtime Memory in Unity Mobile Games: PSS Standards, Memory Profiler Analysis, and Optimization Workflows https://www.gameoptim.com/blog/post/OptimizationGuide2 3. Common Resource Memory Issues in Unity Mobile Games https://www.gameoptim.com/blog/post/OptimizationGuide3 4. Why Is Texture Memory So High in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide4 5. How Do Vertex Count, Vertex Attributes, and Read/Write Settings Affect Mesh Performance in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide5 6. How Can You Reduce Animation Memory Usage and Runtime Overhead in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide6 7. How Can You Reduce Audio Memory Usage and Playback Overhead in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide7 8. How Can You Reduce Material Count and Avoid Material Related Performance Waste in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide8 9. How Can You Reduce Render Texture Memory Usage and Rendering Overhead in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide9 10. What Causes Excessive Shader Memory Usage and Variant Explosion in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide10 11. Why Do Font and Particle System Resources Consume Excessive Memory in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide11 12. Why Does Mono Heap Memory Keep Growing and Trigger GC Spikes in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide12 13. Why Is Memory Usage Still High After Optimizing Unity Resources? https://www.gameoptim.com/blog/post/OptimizationGuide13 14. What Defines a CPU Bottleneck in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide14 15. Unity Rendering CPU Optimization: Why Is Rendering Time So High? https://www.gameoptim.com/blog/post/OptimizationGuide15 16. What Causes UI Performance Bottlenecks in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide16 17. Is Unity Physics Wasting CPU Time on Mobile? How to Detect and Reduce Hidden Physics Overhead https://www.gameoptim.com/blog/post/OptimizationGuide17 18. Why Is Unity Animation Taking Too Much CPU on Mobile? https://www.gameoptim.com/blog/post/OptimizationGuide18 19. How to Reduce Particle System CPU Spikes and Runtime Overhead in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide19 20. How Can I Reduce Unity Loading Time and Avoid Runtime Stutters? https://www.gameoptim.com/blog/post/OptimizationGuide20 22. How Do I Identify GPU Bottlenecks in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide22 23. How can developers accurately determine whether a Unity mobile game is GPU Bound using GPU Clocks? https://www.gameoptim.com/blog/post/OptimizationGuide23 24. How can developers identify and optimize GPU vertex stage bottlenecks in Unity mobile games? https://www.gameoptim.com/blog/post/OptimizationGuide24 25. How can developers identify and reduce GPU fragment stage bottlenecks in Unity mobile games? https://www.gameoptim.com/blog/post/OptimizationGuide25 26. How to Reduce Shader Complexity in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide26 27. How to Optimize Post processing in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide27 28. How to Reduce GPU Bandwidth in Unity Mobile Games? https://www.gameoptim.com/blog/post/OptimizationGuide28 29. Why Does My Unity Mobile Game Overheat, Drain Battery Fast, and Drop FPS After a While? https://www.gameoptim.com/blog/post/OptimizationGuide29