Prefer using SetPixels32() over SetPixels()
Color uses floating point [0.0,1.0] per component; Color32 uses a byte [0,255]: 4 times less memory and much faster to work with.
How to fix
If 32bit-RGBA is compatible with your scenario, use SetPixels32() instead.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Prefer using SetPixels32() over SetPixels()</Name>
// <Id>ND3207:PreferUsingSetPixels32OverSetPixels</Id>
warnif count > 0
let setPixelMethods = Types.WithFullName("UnityEngine.Texture2D")
.ChildMethods().WithSimpleNameIn("SetPixel","SetPixels").ToHashSetEx()
from m in Application.Methods.UsingAny(setPixelMethods)
let shouldntCall = m.MethodsCalled.Intersect(setPixelMethods)
select new {
m,
shouldntCall,
Debt = 15.ToMinutes().ToDebt(),
Severity = Severity.Major
}
//<Expl>
// The *{0}* shouldn't call *{1}* but instead
// should call *SetPixels32()* for performance reasons.
//</Expl>
//<Desc>
// *Color* uses floating point [0.0,1.0] per component; *Color32* uses a
// byte [0,255]: 4 times less memory and much faster to work with.
//</Desc>
//<HowToFix>
// If 32bit-RGBA is compatible with your scenario, use
// *SetPixels32()* instead.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
API Usage / Unity: all 9 items
- Classes tagged with InitializeOnLoad should have a static constructor ND3200
- Avoid using non-generic GetComponent ND3201
- Avoid empty Unity message ND3202
- Avoid using Time.fixedDeltaTime with Update ND3203
- Use CreateInstance to create a scriptable object ND3204
- The SerializeField attribute is redundant on public fields ND3205
- InitializeOnLoadMethod should tag only static and parameterless methods ND3206
- Prefer using SetPixels32() over SetPixels() ND3207
- Don't use System.Reflection in performance critical messages ND3208