0
0
Unityframework~8 mins

Data encryption basics in Unity - Performance & Optimization

Choose your learning style9 modes available
Performance: Data encryption basics
MEDIUM IMPACT
This affects the time it takes to encrypt and decrypt data, impacting app responsiveness and load times when handling secure data.
Encrypting user data frequently during gameplay
Unity
using System.Security.Cryptography;

public class Encryptor {
    private Aes aes;
    private ICryptoTransform encryptor;

    public Encryptor(byte[] key, byte[] iv) {
        aes = Aes.Create();
        aes.Key = key;
        aes.IV = iv;
        encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
    }

    public string EncryptData(string plainText) {
        using (MemoryStream ms = new MemoryStream()) {
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
                using (StreamWriter sw = new StreamWriter(cs)) {
                    sw.Write(plainText);
                }
                return Convert.ToBase64String(ms.ToArray());
            }
        }
    }
}
Reuses encryption objects to avoid repeated costly setup, reducing CPU spikes and improving responsiveness.
📈 Performance GainReduces blocking time to under 10ms per call, improving input responsiveness (INP)
Encrypting user data frequently during gameplay
Unity
using System.Security.Cryptography;

public string EncryptData(string plainText, byte[] key, byte[] iv) {
    using (Aes aes = Aes.Create()) {
        aes.Key = key;
        aes.IV = iv;
        ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
        using (MemoryStream ms = new MemoryStream()) {
            using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
                using (StreamWriter sw = new StreamWriter(cs)) {
                    sw.Write(plainText);
                }
                return Convert.ToBase64String(ms.ToArray());
            }
        }
    }
}
Creates new encryption objects and streams every time, causing CPU spikes and blocking the main thread.
📉 Performance CostBlocks main thread for 50-100ms per call, causing input lag (INP) during gameplay
Performance Comparison
PatternCPU UsageMain Thread BlockingResponsiveness ImpactVerdict
Creating new encryptor each callHigh CPU spikesBlocks main thread 50-100msCauses input lag (INP)[X] Bad
Reusing encryptor instanceLower CPU spikesBlocks main thread <10msSmooth input responsiveness[OK] Good
Rendering Pipeline
Encryption runs on the CPU and can block the main thread if done synchronously, delaying UI updates and input handling.
JavaScript Execution (if WebGL)
Main Thread CPU
Input Handling
⚠️ BottleneckMain Thread CPU blocking during encryption calls
Core Web Vital Affected
INP
This affects the time it takes to encrypt and decrypt data, impacting app responsiveness and load times when handling secure data.
Optimization Tips
1Avoid creating new encryption objects on every call to reduce CPU spikes.
2Perform encryption asynchronously or off the main thread to keep UI responsive.
3Reuse encryption instances to minimize setup overhead and improve input responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue when creating new encryption objects every time you encrypt data in Unity?
AIt causes high CPU usage and blocks the main thread, leading to input lag.
BIt increases GPU load and slows down rendering.
CIt reduces memory usage but increases network latency.
DIt improves security but slows down disk access.
DevTools: Unity Profiler
How to check: Open Unity Profiler, record during encryption calls, look at CPU usage and main thread spikes.
What to look for: High CPU spikes and main thread blocking during encryption indicate poor performance.