using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; #if UNITY_EDITOR using UnityEditor; #endif public class UIParamCopyTool : MonoBehaviour { private List copyObjects = new List(); public List copyTargets = new List(); public void Add() { var _obejct = new UIObject(); _obejct.type = UIType.None; copyObjects.Add(_obejct); } public UIObject this[int _index] { get { if (_index < copyObjects.Count && _index >= 0) { return copyObjects[_index]; } return null; } } public void Remove(int _index) { var _object = copyObjects[_index]; copyObjects.RemoveAt(_index); _object = null; } public int Count { get { return copyObjects.Count; } } public void FindTarget(string _parentName) { copyTargets.Clear(); FindTarget(transform, _parentName); } public void Paste() { foreach (var _object in copyObjects) { foreach (var _target in copyTargets) { _target.Paste(_object); } } } public void FindTarget(Transform _parent, string _parentName) { if(_parent == null|| _parent.childCount == 0) { return; } foreach (Transform _tran in _parent) { if (_tran.name == _parentName) { var _target = new UITarget() { Object = _tran.gameObject, ok = true, }; _target.Init(); copyTargets.Add(_target); continue; } FindTarget(_tran, _parentName); } } public class UIObject { public Graphic Object; public UIType type; public string name = string.Empty; } public class UITarget { public GameObject Object; public bool ok = true; public List images = new List(); public List texts = new List(); public void Init() { Object.GetComponentsInChildren(true, images); Object.GetComponentsInChildren(true, texts); } public void Paste(UIObject _object) { switch (_object.type) { case UIType.None: break; case UIType.Image: PasteImage(_object); break; case UIType.Text: PasteText(_object); break; } } public void PasteRectTransform(RectTransform _target, RectTransform _paste) { _target.pivot = _paste.pivot; _target.anchorMax = _paste.anchorMax; _target.anchorMin = _paste.anchorMin; _target.sizeDelta = _paste.sizeDelta; _target.localPosition = _paste.localPosition; } public void PasteImage(UIObject _object) { var _imageArray = images.FindAll((x) => { return x.name == _object.name; }); if (_imageArray == null) { return; } for (int i = 0; i < _imageArray.Count; i++) { var _image = _imageArray[i]; var _paste = _object.Object as Image; _image.sprite = _paste.sprite; _image.color = _paste.color; _image.raycastTarget = _paste.raycastTarget; PasteRectTransform(_image.rectTransform, _paste.rectTransform); } } public void PasteText(UIObject _object) { var _textArray = texts.FindAll((x) => { return x.name == _object.name; }); if (_textArray == null) { return; } for (int i = 0; i < _textArray.Count; i++) { var _text = _textArray[i]; var _paste = _object.Object as Text; _text.color = _paste.color; _text.fontSize = _paste.fontSize; _text.font = _paste.font; _text.raycastTarget = _paste.raycastTarget; _text.alignment = _paste.alignment; _text.horizontalOverflow = _paste.horizontalOverflow; _text.verticalOverflow = _paste.verticalOverflow; PasteRectTransform(_text.rectTransform, _paste.rectTransform); } } } public enum UIType { None, Image, Text, } } #if UNITY_EDITOR [CustomEditor(typeof(UIParamCopyTool))] public class UIParamCopyToolEditor : Editor { private Vector2 scrollposition; private Vector2 scrollposition1; private string targetName = string.Empty; public override void OnInspectorGUI() { var _target = target as UIParamCopyTool; if (GUILayout.Button("+")) { _target.Add(); } scrollposition = GUILayout.BeginScrollView(scrollposition); for (int i = 0; i < _target.Count; i++) { GUILayout.BeginHorizontal(); EditorGUILayout.EnumPopup(_target[i].type); if (_target[i].Object == null) { _target[i].Object = (Graphic)EditorGUILayout.ObjectField("Component:", _target[i].Object, typeof(Graphic), true); } else if (_target[i].Object is Image) { _target[i].name = _target[i].Object.name; _target[i].type = UIParamCopyTool.UIType.Image; _target[i].Object = (Image)EditorGUILayout.ObjectField(_target[i].name, _target[i].Object, typeof(Image), true); } else if (_target[i].Object is Text) { _target[i].name = _target[i].Object.name; _target[i].type = UIParamCopyTool.UIType.Text; _target[i].Object = (Text)EditorGUILayout.ObjectField(_target[i].name, _target[i].Object, typeof(Text), true); } if (GUILayout.Button("-")) { _target.Remove(i); i--; } GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); GUILayout.BeginHorizontal(); targetName = EditorGUILayout.TextField("目标名:", targetName); if (GUILayout.Button("Find")) { if (targetName != string.Empty) { _target.FindTarget(targetName); } } GUILayout.EndHorizontal(); scrollposition1 = GUILayout.BeginScrollView(scrollposition1); for (int i = 0; i < _target.copyTargets.Count; i++) { GUILayout.BeginHorizontal(); var _copyTarget = _target.copyTargets[i]; _copyTarget.ok = EditorGUILayout.Toggle("是否替换", _copyTarget.ok, GUILayout.Width(40)); EditorGUILayout.LabelField(StringUtility.Contact("Image", _copyTarget.images.Count)); EditorGUILayout.LabelField(StringUtility.Contact("Text", _copyTarget.texts.Count)); GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); if (GUILayout.Button("Paste")) { _target.Paste(); } } } #endif