Skip to content

Use Animator.StringToHash for repeated Animator method calls#439

Merged
sailro merged 1 commit intount0041from
copilot/sub-pr-437
Jan 29, 2026
Merged

Use Animator.StringToHash for repeated Animator method calls#439
sailro merged 1 commit intount0041from
copilot/sub-pr-437

Conversation

Copy link
Contributor

Copilot AI commented Jan 29, 2026

Partially fixes #49

Checklist

  • I have read the Contribution Guide ;
  • There is an approved issue describing the change when contributing a new analyzer or suppressor ;
  • I have added tests that prove my fix is effective or that my feature works ;
  • I have added necessary documentation (if appropriate) ;

Short description of what this resolves:

Implements analyzer UNT0041 to detect Animator method calls with string literals that have int (hash) overloads, suggesting performance optimization via Animator.StringToHash.

Changes proposed in this pull request:

  • Analyzer: Detects Animator methods (Play, SetBool, SetTrigger, GetFloat, etc.) called with string literals when int overload exists
  • Code Fix: Extracts string literal to private static readonly int field initialized with Animator.StringToHash(), replaces call site with field reference
  • Field Naming: Generates PascalCase field names from literal values (e.g., "Attack" → AttackHash, "Attack-Heavy 01" → AttackHeavy01Hash)
  • Deduplication: Reuses existing hash fields when present, avoiding duplicates
  • Tests: 14 test cases covering common scenarios, edge cases with special characters, and validation that non-Animator types and variables aren't flagged
  • Documentation: Added UNT0041.md with rationale and examples

Example:

// Before
_animator.Play("Attack");
_animator.SetBool("IsRunning", true);

// After applying fix
private static readonly int AttackHash = Animator.StringToHash("Attack");
private static readonly int IsRunningHash = Animator.StringToHash("IsRunning");

_animator.Play(AttackHash);
_animator.SetBool(IsRunningHash, true);

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix repeated Animator method calls using StringToHash Use Animator.StringToHash for repeated Animator method calls Jan 29, 2026
Copilot AI requested a review from sailro January 29, 2026 20:28
@sailro sailro marked this pull request as ready for review January 29, 2026 20:47
@sailro sailro merged commit 5254c62 into unt0041 Jan 29, 2026
2 checks passed
@sailro sailro deleted the copilot/sub-pr-437 branch January 29, 2026 20:47
sailro added a commit that referenced this pull request Jan 30, 2026
* Use Animator.StringToHash for repeated Animator method calls

* Initial plan (#439)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants