123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEditor;
- using UnityEditor.Events;
- using UnityEditor.Experimental;
- using UnityEditor.SceneManagement;
- using UnityEditor.VersionControl;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using Random = System.Random;
- namespace Plugins.CxShine.Editor
- {
- [CustomEditor(typeof(SpriteRenderer))]
- public class EditorImageToButton : UnityEditor.Editor
- {
- public static List<string> SelectedPath()
- {
- //支持多选
- List<string> paths = new List<string>();
- string[] guids = Selection.assetGUIDs; //获取当前选中的asset的GUID
- for (int i = 0; i < guids.Length; i++)
- {
- string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]); //通过GUID获取路径
- // Debug.Log(assetPath);
- paths.Add(assetPath);
- }
- return paths;
- }
- static void CreateImages()
- {
- foreach (var guid in SelectedPath())
- {
- PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
- if (prefabStage != null)
- {
- AddImageToRoot(guid, prefabStage.prefabContentsRoot);
- }
- else
- {
- foreach (var rootGameObject in SceneManager.GetActiveScene().GetRootGameObjects())
- {
- foreach (Canvas canvas in rootGameObject.GetComponentsInChildren<Canvas>())
- {
- AddImageToRoot(guid, canvas.gameObject);
- return;
- }
- }
- }
- }
- }
- private static void AddImageToRoot(string path, GameObject root)
- {
-
-
-
-
- string objName = CreateDuYiWuErName(root, "ttt", 0);
- GameObject child = new GameObject(objName);
- child.AddComponent<RectTransform>();
- child.GetComponent<RectTransform>().position = new Vector3(0, 0, 0);
- child.AddComponent<Image>();
- Debug.Log("ssss:"+path);
- Image imageComp = child.GetOrAddComponent<Image>();
- imageComp.sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
- imageComp.SetNativeSize();
- Button buttonComp = child.AddComponent<Button>();
- UnityEventTools.AddBoolPersistentListener(buttonComp.onClick, root.SetActive, root.gameObject);
- child.transform.SetParent(root.transform);
- }
- private static string CreateDuYiWuErName(GameObject root, string name, int times)
- {
- string targetName = name + "" + times;
- if (root.transform.Find(targetName) != null)
- {
- return CreateDuYiWuErName(root, name, times + 1);
- }
- else
- {
- return targetName;
- }
- }
- [InitializeOnLoadMethod]
- static void Start()
- {
- // Action OnEvent = delegate
- // {
- //
- // };
- // EditorApplication.hierarchyWindowItemOnGUI = delegate(int instanceID, Rect selectionRect) { OnEvent(); };
- EditorApplication.projectWindowItemOnGUI = delegate(string guid, Rect selectionRect)
- {
- Event e = Event.current;
- switch (e.type)
- {
- case EventType.KeyUp:
- if (Event.current.keyCode == KeyCode.Space)
- {
- Debug.Log(guid + "" + selectionRect.ToString());
- CreateImages();
- e.Use();
- }
- break;
- }
- };
- }
- }
- }
|