FirearmMuzzleResolver.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using Assets.HeroEditor4D.Common.Scripts.Data;
  6. using UnityEngine;
  7. namespace Assets.HeroEditor4D.Common.Scripts.Editor
  8. {
  9. public static class FirearmMuzzleResolver
  10. {
  11. private static Texture2D _texture;
  12. public static void Resolve(ItemSprite sprite)
  13. {
  14. sprite.MetaDict = new Dictionary<string, string> { { "Muzzle", ResolveMuzzle(sprite.Path).ToString() } };
  15. }
  16. public static int ResolveMuzzle(string path)
  17. {
  18. path = Path.Combine(Environment.CurrentDirectory, path);
  19. _texture ??= new Texture2D(2, 2);
  20. if (File.Exists(path))
  21. {
  22. var bytes = File.ReadAllBytes(path);
  23. _texture.LoadImage(bytes);
  24. var pixels = _texture.GetPixels32();
  25. var x = _texture.width / 2;
  26. var height = _texture.height - 64;
  27. for (var y = height - 1; y >= 0; y--)
  28. {
  29. if (pixels[x + y * _texture.width].a > 0)
  30. {
  31. return 100 * y / height;
  32. }
  33. }
  34. }
  35. return 0;
  36. }
  37. }
  38. }