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 SelectedPath() { //支持多选 List paths = new List(); 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()) { 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(); child.GetComponent().position = new Vector3(0, 0, 0); child.AddComponent(); Debug.Log("ssss:"+path); Image imageComp = child.GetOrAddComponent(); imageComp.sprite = AssetDatabase.LoadAssetAtPath(path); imageComp.SetNativeSize(); Button buttonComp = child.AddComponent