Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. public class ReplacePrefab : EditorWindow {
  4.  
  5. [MenuItem("GameEditor/ReplacePrefab")]
  6. public static void ShowWindow()
  7. {
  8. EditorWindow.GetWindow(typeof(ReplacePrefab));
  9. }
  10.  
  11. static public GameObject source;
  12.  
  13. public void OnGUI()
  14. {
  15. if (GUILayout.Button("Replace the selected GameObjects with your prefab"))
  16. {
  17. ReplaceSelected();
  18. }
  19.  
  20. EditorGUILayout.BeginHorizontal();
  21. source = EditorGUILayout.ObjectField("Prefab", source, typeof(GameObject), true) as GameObject;
  22. EditorGUILayout.EndHorizontal();
  23.  
  24. if (GUILayout.Button("Rename delete (clone)"))
  25. {
  26. Rename();
  27. }
  28. }
  29. private static void ReplaceSelected()
  30. {
  31. GameObject[] go = Selection.gameObjects;
  32. int go_count = 0;
  33. foreach (GameObject g in go)
  34. {
  35. go_count++;
  36. Transform trans = g.transform;
  37. Transform parent = trans.parent;
  38.  
  39. GameObject instance = Instantiate(source) as GameObject;
  40. instance.transform.parent = parent;
  41. instance.transform.localPosition = trans.localPosition;
  42. instance.transform.localRotation = trans.localRotation;
  43. instance.transform.localScale = trans.localScale;
  44.  
  45. GameObject.DestroyImmediate(g);
  46. string name = instance.name;
  47. instance.name = name.Substring(0, name.Length - 7);
  48. }
  49.  
  50. Debug.Log(string.Format("Replaced {0} GameObjects", go_count));
  51. }
  52.  
  53. private static void Rename()
  54. {
  55. GameObject[] go = Selection.gameObjects;
  56. int go_count = 0, components_count = 0, missing_count = 0;
  57. foreach (GameObject g in go)
  58. {
  59. go_count++;
  60. string name = g.name;
  61. g.name = name.Substring(0, name.Length - 7);
  62. }
  63.  
  64. Debug.Log(string.Format("Replaced {0} GameObjects", go_count));
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement