SetSpriteInChildren.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //================================================================================
  2. //
  3. //================================================================================
  4. using UnityEngine;
  5. using UnityEditor;
  6. using System.Collections;
  7. using System.IO;
  8. //================================================================================
  9. //
  10. //================================================================================
  11. namespace ReaderRabbit
  12. {
  13. //================================================================================
  14. //
  15. //================================================================================
  16. public class SpriteChildren : EditorWindow
  17. {
  18. //================================================================================
  19. //
  20. //================================================================================
  21. [MenuItem ("RR1ST Animation/Set sprite in children (for [Character]_Animator child objects)")]
  22. static void WindowSetSpriteInChildren()
  23. {
  24. SpriteChildren window = (SpriteChildren)EditorWindow.GetWindow(typeof(SpriteChildren));
  25. }
  26. string m_ParentFolderName = "";
  27. string m_FolderName = "";
  28. //================================================================================
  29. //
  30. //================================================================================
  31. void OnGUI()
  32. {
  33. EditorGUILayout.LabelField("1. Select the child of [Character]_Animator (e.g. Characters->Reader->Reader_Animator->Blink).", EditorStyles.label);
  34. EditorGUILayout.LabelField("2. Set the folder name containing all the children frames in the text field below (e.g. 10055).", EditorStyles.label);
  35. m_ParentFolderName = EditorGUILayout.TextField("Parent folder name (leave blank for default)", m_ParentFolderName);
  36. m_FolderName = EditorGUILayout.TextField("Folder name", m_FolderName);
  37. EditorGUILayout.LabelField("3. Hit the button below.", EditorStyles.label);
  38. if (GUILayout.Button("Set Sprite In Children"))
  39. SetChildren();
  40. }
  41. //================================================================================
  42. //
  43. //================================================================================
  44. void SetChildren()
  45. {
  46. // Get scene name, based on scene.unity filename.
  47. string sceneName = "";
  48. if (string.IsNullOrEmpty(m_ParentFolderName))
  49. {
  50. sceneName = System.IO.Path.GetFileName(EditorApplication.currentScene);
  51. sceneName = sceneName.Substring(0, sceneName.Length - ".unity".Length);
  52. }
  53. else
  54. {
  55. sceneName = m_ParentFolderName;
  56. }
  57. Transform parent = Selection.activeTransform;
  58. string oldName = "";
  59. SpriteRenderer spriteRenderer = null;
  60. int index = 0;
  61. foreach (Transform child in parent.transform)
  62. {
  63. // Rename children with prefix and add _N at the end.
  64. oldName = child.name;
  65. child.name = m_FolderName + "_" + index;
  66. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Renamed child \'" + oldName + "\' to \'" + child.name + "\'");
  67. // Associate the prefix_N.png sprite filename to the object's SpriteRenderer.
  68. spriteRenderer = child.GetComponent<SpriteRenderer>();
  69. spriteRenderer.sprite = AssetDatabase.LoadAssetAtPath("Assets/Sprites/" + sceneName + "/" + m_FolderName + "/" + m_FolderName + "_" + index + ".png", typeof(Sprite)) as Sprite;
  70. ++index;
  71. }
  72. ShowNotification(new GUIContent("Done."));
  73. }
  74. } // public class SpriteChildren : EditorWindow
  75. } // namespace KishiTechUnity