Writing extensions for unity components

Time:2022-5-16

Recently, I encountered a requirement to quickly obtain the path of a GameObject in the hierarchy panel. Search left and right on Google, but you can’t find it. The keywords used are hierarchy, GameObject, path, relationship, etc. Later, I remembered that I saw a small tip on Xuan Yusong’s microblog a few days ago, attachedlink, thanks again to Yusong dada.

These lines of code, written in an editor file, can print out the path of a game object when the mouse selects it. Let me show you the code!

using UnityEngine;
using UnityEditor;
using System.Collections;

public class ToolKit : EditorWindow {

    // Use this for initialization
    [InitializeOnLoadMethod]
    static void Start () {
        Selection.selectionChanged = delegate {
            Debug.Log(Selection.activeGameObject.transform.GetPath());
            //I made changes here
        };
    }
}

Crape myrtle, please wait a minute= ε=ε=ε= ┏(゜ロ゜;) ┛
Why don’t I have one hereGetPath()Function?
Ho ho ho, this is my extension!

I used to use a lot of extension functions of enumeration. For example, I need to define all skills with enumeration. Each skill has a name, which I usually write as extension.

public enum Skill{
    Fuck
}

static class SkillMethods{
    public static string GetName(this Skill type){
        Return "I'll do it";
    }
}

So I can use it directly when I need itSkill.Fuck.GetName()It’s OK. I think it’s very convenient… It’s very maintainable.

Then let’s take another lookGetPathFunction requirements, although transform cannot obtain the path, it can find the parent node and root, which is very convenient. Just connect all names with \ from bottom to top ~ make your own clothes and food!

Code!

public static class TransformExtension {
    public static string GetPath(this Transform trans){
        Transform current = trans;
        StringBuilder path = new StringBuilder(current.name);

        while(true){
            if(current != trans.root){
                current = current.parent;
                path.Insert(0, current.name + "/");
            }else{
                break;
            }
        }

        return path.ToString();
    }
}

There are several places that need to be explained to people who write extensions for the first time.

  1. This is the format of extension. The extension method of component name plus extension suffix is different from that of enumeration. The function names and variables inside should also be written in the format in my code.

  2. StringBuilder is the recommended string splicing method, which requiresusing System.Text;, it can improve the string splicing performance. If there are a lot of operations, there may be catons with the + sign. Although I don’t have a lot of operations here, I use StringBuilder in order to use the insert function.

  3. Insert here is to insert the name of the parent node in front of the path, because we find it from the bottom up, but the final result needs the string with the parent node in front and the child node in the back.

  4. Use a pointer to point to the current node. If it is not the root node, point the parent node to the current node, splice the name, and then use the loop until the root node is found.

Just sauce!