EditorImageToButton.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEditor;
  5. using UnityEditor.Events;
  6. using UnityEditor.Experimental;
  7. using UnityEditor.SceneManagement;
  8. using UnityEditor.VersionControl;
  9. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. using UnityEngine.UI;
  12. using Random = System.Random;
  13. namespace Plugins.CxShine.Editor
  14. {
  15. [CustomEditor(typeof(SpriteRenderer))]
  16. public class EditorImageToButton : UnityEditor.Editor
  17. {
  18. public static List<string> SelectedPath()
  19. {
  20. //支持多选
  21. List<string> paths = new List<string>();
  22. string[] guids = Selection.assetGUIDs; //获取当前选中的asset的GUID
  23. for (int i = 0; i < guids.Length; i++)
  24. {
  25. string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]); //通过GUID获取路径
  26. // Debug.Log(assetPath);
  27. paths.Add(assetPath);
  28. }
  29. return paths;
  30. }
  31. static void CreateImages()
  32. {
  33. foreach (var guid in SelectedPath())
  34. {
  35. PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
  36. if (prefabStage != null)
  37. {
  38. AddImageToRoot(guid, prefabStage.prefabContentsRoot);
  39. }
  40. else
  41. {
  42. foreach (var rootGameObject in SceneManager.GetActiveScene().GetRootGameObjects())
  43. {
  44. foreach (Canvas canvas in rootGameObject.GetComponentsInChildren<Canvas>())
  45. {
  46. AddImageToRoot(guid, canvas.gameObject);
  47. return;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. private static void AddImageToRoot(string path, GameObject root)
  54. {
  55. string objName = CreateDuYiWuErName(root, "ttt", 0);
  56. GameObject child = new GameObject(objName);
  57. child.AddComponent<RectTransform>();
  58. child.GetComponent<RectTransform>().position = new Vector3(0, 0, 0);
  59. child.AddComponent<Image>();
  60. Debug.Log("ssss:"+path);
  61. Image imageComp = child.GetOrAddComponent<Image>();
  62. imageComp.sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
  63. imageComp.SetNativeSize();
  64. Button buttonComp = child.AddComponent<Button>();
  65. UnityEventTools.AddBoolPersistentListener(buttonComp.onClick, root.SetActive, root.gameObject);
  66. child.transform.SetParent(root.transform);
  67. }
  68. private static string CreateDuYiWuErName(GameObject root, string name, int times)
  69. {
  70. string targetName = name + "" + times;
  71. if (root.transform.Find(targetName) != null)
  72. {
  73. return CreateDuYiWuErName(root, name, times + 1);
  74. }
  75. else
  76. {
  77. return targetName;
  78. }
  79. }
  80. [InitializeOnLoadMethod]
  81. static void Start()
  82. {
  83. // Action OnEvent = delegate
  84. // {
  85. //
  86. // };
  87. // EditorApplication.hierarchyWindowItemOnGUI = delegate(int instanceID, Rect selectionRect) { OnEvent(); };
  88. EditorApplication.projectWindowItemOnGUI = delegate(string guid, Rect selectionRect)
  89. {
  90. Event e = Event.current;
  91. switch (e.type)
  92. {
  93. case EventType.KeyUp:
  94. if (Event.current.keyCode == KeyCode.Space)
  95. {
  96. Debug.Log(guid + "" + selectionRect.ToString());
  97. CreateImages();
  98. e.Use();
  99. }
  100. break;
  101. }
  102. };
  103. }
  104. }
  105. }