Unity object path query tool

Unity object path query tool


foreword

Hello everyone, I am Orange, and today I will bring you a tutorial on the progress bar of Unity scene switching.
★,♫◦★,♫◦★,♫◦★,-------------------Gorgeous dividing line------------ --------♫◦★,♫◦★,♫◦★,♫◦★,♫◦

Recently, some novice friends came to consult Orange, and used transform.Find or GameObject.Find to query the objects in the Hierarchy window, and reported a null pointer. The friends were very puzzled. It was clearly written correctly, why did it report an error?

After several inquiries, it turned out that the query path misspelled a letter, so I thought I had time to write an object path query tool, which is convenient for everyone to use, and also allows novice Xiaobai to avoid low-level mistakes and improve learning or work. efficiency.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Tool introduction

Create a new Editor folder in Unity, there may be questions from friends here? Why create a new Editor folder?
insert image description here

Here are some introductions to the Editor folder:

Editor

The Edilor folder can be in the root directory or in a subdirectory, as long as the name is Editor. For example, the directory: hox.sooEditor is the same as /Editor, no matter how many folders are called Editor. All resource files or script files placed under Edlior will not be included in the release package, and scripts can only be used during editing. Generally, some tool scripts will be placed here, or some DLLs used for editing. For example, if we want to create a similar skill editor now, it would be better to put the code of the editor here, because we only need the files generated by the editor during actual operation, not the core code of the editor.

My understanding is that the scripts in the folder are only run in the Unity editor, and will not be typed into the package, nor can they be used in the package.

Two, the code

1. Copy code

The code is as follows: Remember to keep the script name of the new script consistent with mine!

using UnityEditor;
using UnityEngine;

public class ObjPathCopyTool : ScriptableObject
{
    
    
    [MenuItem("Custom/复制选中物体的路径 %Q")]//自定义快捷键
    static void CopyPath()
    {
    
    
        Object[] objs = Selection.objects;
        if (objs.Length < 1)
            return;

        GameObject obj = objs[0] as GameObject;
        if (!obj)
            return;

        string path = obj.name;
        Transform parent = obj.transform.parent;
        while (parent)
        {
    
    
            path = string.Format("{0}/{1}", parent.name, path);
            parent = parent.parent;
        }

        Debug.Log(path);
        CopyString(path);
    }

    //将字符串赋值到剪切板
    static void CopyString(string str)
    {
    
    
        TextEditor te = new TextEditor();
        te.text = str;
        te.SelectAll();
        te.Copy();
    }
}

2. Put the code into the Editor folder

insert image description here

After putting it in, wait for Unity to load.
At this time, if you look at the menu bar, there will be an additional Custom
insert image description here


Three, use

In the Hierarchy window, select an object at random.

I chose an object with more layers.

Then press Ctrl+Q or you can go to the menu bar and click the button under Custom.
insert image description here
The system will automatically print out the path, and then copy the path to find it.
If you find it troublesome, you can go to the link to download https://download.csdn.net/download/weixin_45375968/85168252

Four. Summary

This is the end here. The above is all the content of Orange today. This article only briefly introduces what the Editor folder is. Using small tools, we can quickly obtain the path of the object in the Hierarchy window, allowing us to find the name of the object more quickly. Thereby improving study or work efficiency.

5. Conclusion

Less than a silicon step, nothing can reach thousands of miles.
If there is no accumulation of small currents, there will be no rivers and seas.
Improve a little every day Thank you for watching.

If you think it is helpful to you, welcome to follow, bookmark, and forward! see you next time

Guess you like

Origin blog.csdn.net/weixin_45375968/article/details/124244406