SetSpeechFrames.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 SetSpeechFrames : EditorWindow
  17. {
  18. //================================================================================
  19. //
  20. //================================================================================
  21. [MenuItem ("RR1ST Animation/Set speech frames (for [Character]_Speech object)")]
  22. static void WindowSetSpriteInChildren()
  23. {
  24. SetSpeechFrames window = (SetSpeechFrames)EditorWindow.GetWindow(typeof(SetSpeechFrames));
  25. }
  26. string m_FolderName = "";
  27. string m_BodyName = "";
  28. string m_FrameCount = "9";
  29. //================================================================================
  30. //
  31. //================================================================================
  32. void OnGUI()
  33. {
  34. EditorGUILayout.LabelField("1. Select the [Character]_Speech object (e.g. Characters->Reader->Reader_Speech).", EditorStyles.label);
  35. EditorGUILayout.LabelField("2. Set the folder name containing all the children frames in the text field below (e.g. 10050).", EditorStyles.label);
  36. m_FolderName = EditorGUILayout.TextField("Folder name", m_FolderName);
  37. EditorGUILayout.LabelField("3. Set the folder name containing the character body sprite in the text field below (e.g. 10051).", EditorStyles.label);
  38. m_BodyName = EditorGUILayout.TextField("Folder (body) name", m_BodyName);
  39. EditorGUILayout.LabelField("4. Set how many frames there are for the speech animation in the text field below (e.g. 9).", EditorStyles.label);
  40. m_FrameCount = EditorGUILayout.TextField("Frame count", m_FrameCount);
  41. EditorGUILayout.LabelField("5. Hit the button below.", EditorStyles.label);
  42. if (GUILayout.Button("Set Speech Frames"))
  43. SetChildren();
  44. }
  45. //================================================================================
  46. //
  47. //================================================================================
  48. void SetChildren()
  49. {
  50. // Get scene name, based on scene.unity filename.
  51. string sceneName = System.IO.Path.GetFileName(EditorApplication.currentScene);
  52. sceneName = sceneName.Substring(0, sceneName.Length - ".unity".Length);
  53. Transform parent = Selection.activeTransform;
  54. SpeechAnimation speechAnimation = parent.GetComponent<SpeechAnimation>();
  55. int frameCount = int.Parse(m_FrameCount);
  56. speechAnimation.SpriteFrames = new Sprite[frameCount];
  57. for (int i = 0; i < frameCount; ++i)
  58. {
  59. speechAnimation.SpriteFrames[i] = AssetDatabase.LoadAssetAtPath("Assets/Sprites/" + sceneName + "/" + m_FolderName + "/" + m_FolderName + "_" + i + ".png", typeof(Sprite)) as Sprite;
  60. }
  61. int index = 0;
  62. string oldName = "";
  63. SpriteRenderer spriteRenderer = null;
  64. string folderName = "";
  65. foreach (Transform child in parent.transform)
  66. {
  67. if (index == 0)
  68. {
  69. folderName = m_BodyName;
  70. }
  71. else if (index == 1)
  72. {
  73. folderName = m_FolderName;
  74. }
  75. // Rename children with prefix and add _0 at the end.
  76. oldName = child.name;
  77. child.name = folderName + "_0";
  78. KishiTechUnity.KishiTechDebug.KTDebug.Instance().Log("Renamed child \'" + oldName + "\' to \'" + child.name + "\'");
  79. // Associate the prefix_0.png sprite filename to the object's SpriteRenderer.
  80. spriteRenderer = child.GetComponent<SpriteRenderer>();
  81. spriteRenderer.sprite = AssetDatabase.LoadAssetAtPath("Assets/Sprites/" + sceneName + "/" + folderName + "/" + folderName + "_0.png", typeof(Sprite)) as Sprite;
  82. ++index;
  83. }
  84. ShowNotification(new GUIContent("Done."));
  85. }
  86. } // public class SetSpeechFrames : EditorWindow
  87. } // namespace KishiTechUnity