Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ImportUtilites.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using Unity.Collections;
  6. using UnityEngine;
  7. namespace UnityEditor.U2D.PSD
  8. {
  9. class UniqueNameGenerator
  10. {
  11. List<int> m_NameHash = new List<int>();
  12. public bool ContainHash(int i)
  13. {
  14. return m_NameHash.Contains(i);
  15. }
  16. public void AddHash(int i)
  17. {
  18. m_NameHash.Add(i);
  19. }
  20. public void AddHash(string name)
  21. {
  22. m_NameHash.Add(GetStringHash(name));
  23. }
  24. public string GetUniqueName(string name, bool logNewNameGenerated = false, UnityEngine.Object context = null)
  25. {
  26. return GetUniqueName(name, m_NameHash);
  27. }
  28. static string GetUniqueName(string name, List<int> stringHash, bool logNewNameGenerated = false, UnityEngine.Object context = null)
  29. {
  30. var sanitizedName = string.Copy(SanitizeName(name));
  31. string uniqueName = sanitizedName;
  32. int index = 1;
  33. while (true)
  34. {
  35. var hash = GetStringHash(uniqueName);
  36. if (!stringHash.Contains(hash))
  37. {
  38. stringHash.Add(hash);
  39. if (logNewNameGenerated && sanitizedName != uniqueName)
  40. Debug.Log(string.Format("Asset name {0} is changed to {1} to ensure uniqueness", name, uniqueName), context);
  41. return uniqueName;
  42. }
  43. uniqueName = string.Format("{0}_{1}", sanitizedName, index);
  44. ++index;
  45. }
  46. }
  47. static string SanitizeName(string name)
  48. {
  49. name = name.Replace('\0', ' ');
  50. string newName = null;
  51. // We can't create asset name with these name.
  52. if ((name.Length == 2 && name[0] == '.' && name[1] == '.')
  53. || (name.Length == 1 && name[0] == '.')
  54. || (name.Length == 1 && name[0] == '/'))
  55. newName += name + "_";
  56. if (!string.IsNullOrEmpty(newName))
  57. {
  58. Debug.LogWarning(string.Format("File contains layer with invalid name for generating asset. {0} is renamed to {1}", name, newName));
  59. return newName;
  60. }
  61. return name;
  62. }
  63. static int GetStringHash(string str)
  64. {
  65. MD5 md5Hasher = MD5.Create();
  66. var hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(str));
  67. return BitConverter.ToInt32(hashed, 0);
  68. }
  69. }
  70. class GameObjectCreationFactory : UniqueNameGenerator
  71. {
  72. public GameObject CreateGameObject(string name, params System.Type[] components)
  73. {
  74. var newName = GetUniqueName(name);
  75. return new GameObject(newName, components);
  76. }
  77. }
  78. internal static class ImportUtilities
  79. {
  80. public static string SaveToPng(NativeArray<Color32> buffer, int width, int height)
  81. {
  82. if (!buffer.IsCreated ||
  83. buffer.Length == 0 ||
  84. width == 0 ||
  85. height == 0)
  86. return "No .png generated.";
  87. var texture2D = new Texture2D(width, height);
  88. texture2D.SetPixels32(buffer.ToArray());
  89. var png = texture2D.EncodeToPNG();
  90. var path = Application.dataPath + $"/tex_{System.Guid.NewGuid().ToString()}.png";
  91. var fileStream = System.IO.File.Create(path);
  92. fileStream.Write(png);
  93. fileStream.Close();
  94. UnityEngine.Object.DestroyImmediate(texture2D);
  95. return path;
  96. }
  97. }
  98. }