Unity Editor编辑器代码修改物体属性(大小、组件、图片)

using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;

public class ScaleOne : EditorWindow
{
    //设置gameobject的Scale
    [MenuItem("Shen/ScaleOne")]
    public static void SetScale()
    {
        foreach (var go in Selection.gameObjects)
        {
            Transform[] gos = go.GetComponentsInChildren<Transform>(true);
            int i = 0;
            foreach (Transform goo in gos)
            {
                if (goo.transform.localScale != Vector3.one)
                {
                    Debug.Log("<color=darkblue>" + goo.name + "</color>" + i);
                    goo.transform.localScale = Vector3.one;
                    i++;
                }
            }
        }
    }

    //移除掉Rigibody组件
    [MenuItem("Shen/RemoveRigibody")]
    public static void RemoveRigibody()
    {
        foreach (var go in Selection.gameObjects)
        {
            Transform[] gos = go.GetComponentsInChildren<Transform>(true);
            int i = 0;
            foreach (Transform goo in gos)
            {
                if (goo.GetComponent<Rigidbody>() != null)
                {
                    Debug.Log("Destroy" + " Rigibody " + "<color=red>"
                        + goo.parent.parent.parent.name + "/"
                        + goo.parent.parent.name + "/"
                        + goo.parent.name + "/"
                        + go.name + "</color>" + i);
                    DestroyImmediate(goo.GetComponent<Rigidbody>());
                    i++;
                }
            }
        }
    }

    //设置list位置(设置我专门创建的物体的位置)
    [MenuItem("Shen/SetListPos")]
    public static void SetListPos()
    {
        foreach (GameObject go in Selection.gameObjects)
        {
            if (go.name == "button_list")
            {
                go.transform.localPosition = new Vector3(-477, 209, 0);
                int count = go.transform.childCount;
                for (int i = 0; i < count; i++)
                {
                    go.transform.GetChild(i).localPosition = new Vector3(0, -i * 62, 0);
                    go.transform.GetChild(i).gameObject.AddComponent<BoxCollider>();
                    go.transform.GetChild(i).GetComponent<BoxCollider>().center = new Vector3(0, 0, 0);
                    go.transform.GetChild(i).GetComponent<BoxCollider>().size = new Vector3(182, 62, 0);
                    if (go.transform.GetChild(i).GetComponent<UIPanel>() != null)
                    {
                        DestroyImmediate(go.transform.GetChild(i).GetComponent<UIPanel>());
                    }

                    if (go.transform.GetChild(i).childCount == 0)
                    {
                        UIAtlas atlaser = AssetDatabase.LoadAssetAtPath(@"Assets/res/atlas/shared/CommonAtlas.prefab", typeof(UIAtlas)) as UIAtlas;

                        for (int k = 0; k < 3; k++)
                        {
                            GameObject go1 = InstantiateEmpetyGameObject(go.transform.GetChild(i));
                            go1.name = k == 0 ? "Sprite0" : (k == 1 ? "Sprite1" : "effect");
                            go1.AddComponent<UISprite>();
                            go1.GetComponent<UISprite>().atlas = atlaser;
                            go1.GetComponent<UISprite>().spriteName = k == 0 ? "phtyys_001" : (k == 1 ? "phtyys_002" : "ty_js_iconred");
                            go1.GetComponent<UISprite>().MakePixelPerfect();
                            go1.GetComponent<UISprite>().depth = k == 0 ? 0 : (k == 1 ? 2 : 4);
                            go1.transform.localPosition = k == 0 ? Vector3.one : (k == 1 ? Vector3.one : new Vector3(80, 20, 0));
                        }

                        for (int j = 0; j < 2; j++)
                        {
                            GameObject gogo = InstantiateEmpetyGameObject(go.transform.GetChild(i).GetChild(j));
                            gogo.name = "Label";
                            gogo.transform.localPosition = new Vector3((j == 0) ? -14 : 0, 0, 0);
                            gogo.AddComponent<UILabel>();
                            Font fonter = AssetDatabase.LoadAssetAtPath(@"Assets/res/font/myfont.TTF", typeof(Font)) as Font;
                            gogo.GetComponent<UILabel>().trueTypeFont = fonter;
                            gogo.GetComponent<UILabel>().text = "123";
                            gogo.GetComponent<UILabel>().applyGradient = false;
                            gogo.GetComponent<UILabel>().overflowMethod = UILabel.Overflow.ResizeFreely;
                            gogo.GetComponent<UILabel>().effectStyle = UILabel.Effect.None;
                            gogo.GetComponent<UILabel>().fontSize = (j == 0) ? 20 : 22;
                            gogo.GetComponent<UILabel>().color = (j == 0) ?
                                new Color(Convert.ToInt32("0x60", 16) / 255.0f, Convert.ToInt32("0x7e", 16) / 255.0f, Convert.ToInt32("0x8f", 16) / 255.0f)
                                : Color.white;
                            gogo.GetComponent<UILabel>().depth = (j == 0) ? 1 : 3;
                        }
                    }
                }
            }
        }
    }

    static GameObject InstantiateEmpetyGameObject(Transform parenter)
    {
        GameObject gogo = new GameObject("Empty");
        gogo.transform.parent = parenter;
        return gogo;
    }

    //设置位置从float型变为int型
    [MenuItem("Shen/SetPosInt")]
    public static void SetPosInt()
    {
        foreach (Transform tran in Selection.transforms)
        {
            Transform[] trans = tran.GetComponentsInChildren<Transform>();
            foreach (Transform tr in trans)
            {
                if (tr.localPosition.x % 1 != 0 || tr.localPosition.y % 1 != 0)
                {
                    float x = tr.localPosition.x;
                    float y = tr.localPosition.y;
                    Debug.Log(tr.name + " localPosition:" + tr.localPosition);
                    tr.localPosition = new Vector3((float)Math.Round(x), (float)Math.Round(y), 0);
                }
            }
        }
    }

    //设置UILabel上透明属性
    [MenuItem("Shen/CheckLan")]
    public static void CheckLan()
    {
        foreach (GameObject go in Selection.gameObjects)
        {
            UILabel[] labels = go.GetComponentsInChildren<UILabel>();
            foreach (UILabel label in labels)
            {
                label.alpha = 1f;
            }
        }
    }

    //设置图片的精灵sprite
    [MenuItem("Shen/ChangeBtnPicture")]
    public static void ChangeBtnPicture()
    {
        foreach (Transform tran in Selection.transforms)
        {
            UISprite[] sprites = tran.GetComponentsInChildren<UISprite>();
            foreach (UISprite sprite in sprites)
            {
                if (sprite.spriteName == "abc")
                {
                    sprite.spriteName = "cba";
                    Debug.Log("Change Picture: " + sprite.transform.parent.name + "/" + sprite.name);
                }
                else if (sprite.spriteName == "nih")
                {
                    sprite.spriteName = "hin";
                    Debug.Log("Change Picture: " + sprite.transform.parent.name + "/" + sprite.name);
                }
            }
        }
    }

    //移除UIWidget组件的锚点
    [MenuItem("Shen/RemoveAnchor")]
    public static void RemoveAnchor()
    {
        foreach (GameObject tran in Selection.gameObjects)
        {
            UIWidget[] widgets = tran.GetComponentsInChildren<UIWidget>();
            foreach (UIWidget widget in widgets)
            {
                widget.SetAnchor(null, 0, 0, 0, 0);
            }
        }
    }

    //设置UISprite图片的大小变为原本大小
    [MenuItem("Shen/PcitureSnap")]
    public static void PcitureSnap()
    {
        foreach (GameObject tran in Selection.gameObjects)
        {
            UISprite[] sprites = tran.GetComponentsInChildren<UISprite>();
            foreach (UISprite sprite in sprites)
            {
                sprite.MakePixelPerfect();
            }
        }
    }


    public string fullPath = "Assets/prefabs/";
    MonoScript scriptObj = null;
    int loopCount = 0;
    List<Transform> results = new List<Transform>();
    static ScaleOne window;
    string m_msg = "";
    string m_find_sprite = "";
    string m_find_atlas_sprite = "";
    GameObject m_find_obj;

    //打开editor窗口
    [MenuItem("Shen/Find Script")]
    static void Execute()
    {
        if (window == null)
        {
            window = (ScaleOne)GetWindow(typeof(ScaleOne));
        }
        window.Show();
    }
    private void OnGUI()
    {
        //---------------------------------------------------
        GUILayout.Label("查找所有带此脚本的预制体。拖入脚本:");
        scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj, typeof(MonoScript), true);
        if (GUILayout.Button("Find"))
        {
            results.Clear();
            loopCount = 0;
            Debug.Log("开始查找.");
            FindScript();
        }

        //---------------------------------------------------
        GUILayout.Label("\n查找选择物体及子物体脚本里的物体");
        m_find_obj = EditorGUILayout.ObjectField(m_find_obj, typeof(GameObject), true) as GameObject;
        GUILayout.BeginHorizontal();
        bool isToFind = false;
        isToFind = GUILayout.Button("查找", GUILayout.Width(120f));
        GUILayout.EndHorizontal();
        if (isToFind)
        {
            if (Selection.gameObjects == null)
            {
                Debug.LogError("没有选中任何物体!");
                return;
            }
            bool isFinded = false;
            foreach (GameObject select_obj in Selection.gameObjects)
            {
                Base[] all_scripts = select_obj.GetComponents<Base>();
                if (all_scripts == null || all_scripts.Length == 0)
                {
                    Debug.LogError("此物体及所有子物体没有附带脚本!");
                    return;
                }
                foreach (var one_script in all_scripts)
                {
                    int i = 0;
                    foreach (System.Reflection.FieldInfo pi in one_script.GetType().GetFields())
                    {
                        i++;

                        if (pi.GetValue(one_script) == null || !pi.GetValue(one_script).Equals(m_find_obj))
                        {
                            continue;
                        }
                        else
                        {
                            Debug.LogError("找到了 \"" + m_find_obj.name + "\" ! \n在 " + one_script + " 脚本:第" + i + "个:" + pi.Name.Split(' ')[0]);
                            isFinded = true;
                        }
                    }
                }
            }
            if (!isFinded)
            {
                Debug.LogError("Opps,没有找到 \" => m_find_obj.name \"");
            }
        }

        //----------------------------------------------------
        GUILayout.Label("\n查找所有带有此Sprite名字的预制");
        m_find_sprite = GUILayout.TextArea(m_find_sprite);
        if (GUILayout.Button("查找") && !string.IsNullOrEmpty(m_find_sprite))
        {
            Debug.Log("开始查找.");
            loopCount = 0;
            results.Clear();
            FindSprite(m_find_sprite);
        }

        //---------------------------------------------------
        GUILayout.Label("\n查找所有带此图片的图集");
        m_find_atlas_sprite = GUILayout.TextArea(m_find_atlas_sprite);
        if (GUILayout.Button("查找") && !string.IsNullOrEmpty(m_find_atlas_sprite))
        {
            Debug.Log("开始查找.");
            loopCount = 0;
            results.Clear();
            FindAtlasSprite(m_find_atlas_sprite);
        }
    }



    void FindScript()
    {
        List<string> prefabs_names = get_res_all_ui();

        if (scriptObj != null)
        {
            for (int i = 0; i < prefabs_names.Count; i++)
            {
                loopCount++;

                string[] str1 = AssetDatabase.GetAssetPathsFromAssetBundle(prefabs_names[i]);
                if (str1.Length < 1)
                {
                    Debug.Log("bundle no asset: " + prefabs_names[i]);
                    continue;
                }
                GameObject go = AssetDatabase.LoadAssetAtPath(str1[0], typeof(System.Object)) as GameObject;
                if (go != null)
                {
                    var thetype = scriptObj.GetClass();
                    Component[] co = go.GetComponentsInChildren(thetype, true);
                    foreach (var _child in co)
                    {
                        Debug.Log("<color=darkblue>Find it: " + go.name + "--->>" + _child.name + "</color>");

                        results.Add(_child.transform);
                    }
                }
            }
        }
        if (results.Count <= 0)
        {
            Debug.LogError("Oops: Cant find that you want !");
        }
    }

    void FindSprite(string sprite_name)
    {
        List<string> prefabs_names = get_res_all_ui();

        for (int i = 0; i < prefabs_names.Count; i++)
        {
            loopCount++;

            string[] str1 = AssetDatabase.GetAssetPathsFromAssetBundle(prefabs_names[i]);
            if (str1.Length < 1)
            {
                Debug.Log("bundle no asset: " + prefabs_names[i]);
                continue;
            }
            GameObject go = AssetDatabase.LoadAssetAtPath(str1[0], typeof(System.Object)) as GameObject;
            if (go != null)
            {
                UISprite[] co = go.GetComponentsInChildren<UISprite>(true);
                foreach (UISprite _child in co)
                {
                    if (_child.spriteName == sprite_name)
                    {
                        Debug.Log("<color=darkblue>Find it: " + go.name + "--->>" + _child.name + "</color>");

                        results.Add(_child.transform);
                    }
                }
            }
        }
        if (results.Count <= 0)
        {
            Debug.LogError("Oops: Cant find that you want !");
        }
    }

    void FindAtlasSprite(string _name)
    {
        string path = "Assets/atlas/";
        List<string> prefabs_names = new List<string>();
        if (Directory.Exists(path))
        {
            DirectoryInfo di = new DirectoryInfo(path);
            FileInfo[] files = di.GetFiles("*.prefab", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                string ab_name = "atlas/";
                if (files[i].Directory.Name != "atlas")
                {
                    ab_name += files[i].Directory.Name.ToLower() + "/";
                }
                prefabs_names.Add(ab_name + files[i].Name.Split('.')[0].ToLower());
            }
        }

        for (int i = 0; i < prefabs_names.Count; i++)
        {
            loopCount++;

            string[] str1 = AssetDatabase.GetAssetPathsFromAssetBundle(prefabs_names[i]);
            if (str1.Length < 1)
            {
                Debug.Log("bundle no asset: " + prefabs_names[i]);
                continue;
            }
            GameObject go = AssetDatabase.LoadAssetAtPath(str1[0], typeof(System.Object)) as GameObject;
            if (go != null)
            {
                UIAtlas[] co = go.GetComponentsInChildren<UIAtlas>();
                foreach (UIAtlas _child in co)
                {
                    for (int j = 0; j < _child.spriteList.Count; j++)
                    {
                        if (_child.spriteList[j].name == _name)
                        {
                            Debug.Log("<color=darkblue>Find it: " + go.name + "--->>" + _child.name + "</color>");

                            results.Add(_child.transform);
                        }
                    }
                }
            }
        }
        if (results.Count <= 0)
        {
            Debug.LogError("Oops: Cant find that you want !");
        }
    }

    List<string> get_res_all_ui()
    {
        List<string> prefabs_names = new List<string>();
        //获取指定路径下面的所有资源文件
        if (Directory.Exists(fullPath))
        {
            DirectoryInfo direction = new DirectoryInfo(fullPath);
            FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.EndsWith(".prefab"))
                {
                    string ab_name = "cctv/";
                    if (files[i].Directory.Name != "cctv")
                    {
                        ab_name += files[i].Directory.Name + "/";
                    }
                    prefabs_names.Add(ab_name + files[i].Name.Split('.')[0]);
                }
            }
        }
        return prefabs_names;
    }
    
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Unity 3D运行时编辑器插件Runtime Editor 3.5.0是一款非常优秀的工具,它可以让开发者在游戏运行时对游戏进行修改和编辑,使游戏开发更加便捷和高效。该插件支持在Unity编辑器中实时编辑、预览和调试游戏,而且还有非常友好的界面,易于上手使用。此外,它也支持一系列常用操作,如:拖拽物体、创建物体、编辑材质、修改脚本、编辑动画等等。总之,使用这款插件可以充分发挥开发者的想象力和创造力,大大提升游戏开发的效率和质量。同时,它也是一款开放源代码的插件,在社区得到了广泛的应用和支持。唯一需要注意的是,在使用该插件时需要注意版本的适配性,不同版本的Unity可能会有一定的兼容性问题。总体来说,对于有一定开发经验的游戏开发者来说,Runtime Editor 3.5.0是一款非常实用的工具,可以让他们更加专注于游戏的创意和实现,而不是一些繁琐的操作。 ### 回答2: Unity 3D 运行时编辑器插件 Runtime Editor 3.5.0 是一个强大的工具,它为 Unity 3D 用户提供了更加灵活、高效的编辑体验。该插件可以让用户在运行游戏的过程中随时对场景进行编辑,添加、删除和调整物体组件、材质等元素,以及实时预览游戏效果。 Runtime Editor 3.5.0 提供了许多实用的功能和工具,使得用户能够更快速、方便地完成场景的制作。其中一些重要的功能包括: 1. 选择工具:用户可以通过选择工具来选择物体组件、材质等元素,并可以对其进行编辑和调整。 2. 创建工具:用户可以通过创建工具来创建新的物体组件、材质等元素,并可以对其进行定制化的设置和编辑。 3. 面板工具:用户可以在编辑器面板中访问和编辑该插件提供的所有功能和工具,并可以根据需要自定义和布置面板。 4. 预设工具:用户可以通过预设工具来快速添加和调整场景中的预设,以及对其进行设置和编辑。 另外,该插件还包括了许多其他实用的功能和工具,如实时查看场景数据、调整游戏场景视角、导出场景数据等,大大提高了 Unity 3D 用户的工作效率和创作体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值