이 툴은?

Unity3d 에서 Hierarchy 와 Project(Assets) 상에서의 경로를 보여주는 툴이다.

더보기

개발환경 - MAC

Unity3d 버전 - 2017.4.25f1

테스트 버전 - 2017.4.25f1 (MAC)


선택한 Object의 정보를 보여준다.

Name : Object의 이름

Type : 선택한 Object의 종류

Prefab Path : Hierarchy창에 배치된 (Game에 배치된..) Prefab의 경로를 보여준다.

Hierarchy Path : Hierarchy창에 배치된 위치의 경로를 보여준다.

 

 

 


사용법

Unity3d 프로젝트에 아래 스크립트들을 Assets/Editor 폴더 안에 넣은 후 사용한다.

ViewWindow 사용법 -> Window - ESTool - HierarchyPath View 를 누른 후 사용한다.

 

CopyToPath 사용법 -> Hierarchy 나 Project 창에서 특정 Object 를 오른쪽 마우스 버튼으로 누르면 자동으로 클립보드에 저장된다.

Hierarchy 창에서...
Project 창에서...


소스코드

HierarchyPathViewWindow.cs

using System.IO;
using UnityEditor;
using UnityEngine;

public class HierarchyPathViewWindow : EditorWindow
{
	private SerializedObject obj;

    private const string TOPOBJECT_NAME = "ESTool";
	private const string HIPATH_NAME = "HierarchyPath View";
    private const string COPY_HAN = "복사";

    // 선택한 게임 오브젝트
    public static Object selectObj = null;

    // 윈도우 설정 초기화
    [MenuItem("Window/ESTool/HierarchyPath View")]
    internal static void Init()
	{
		var window = (HierarchyPathViewWindow)GetWindow(typeof(HierarchyPathViewWindow), false, "HiPath View");
		window.position = new Rect(window.position.xMin + 100f, window.position.yMin + 100f, 200f, 400f);
	}

    void OnGUI()
	{
        EditorGUILayout.LabelField("선택한 Object", EditorStyles.boldLabel);
        EditorGUILayout.Space();

        if (Selection.activeObject != null)
        {
            // 이름
            CreateSelectLabel("Name ", Selection.activeObject.name);

            // type
            var type = Selection.activeObject.GetType().ToString();
            CreateSelectLabel("Type", type);

            // 프리팹인지 아닌지 체크
            if (PrefabUtility.GetPrefabParent(Selection.activeObject))
            {
                // Prefab이면 Project 창에서의 경로 표시
                CreateSelectLabel("Prefab Path",
                    AssetDatabase.GetAssetPath(PrefabUtility.GetPrefabParent(Selection.activeObject)),
                    true, COPY_HAN, new System.Action<string>((string str) =>
                    {
                        CopyToClipboard(str);
                    }));

                CreateSelectLabel("Hierarchy Path", GetHiFullPath(Selection.activeGameObject.transform),
                true, COPY_HAN, new System.Action<string>((string str) =>
                {
                    CopyToClipboard(str);
                }));
            }
            // Hierarchy 창에 존재하는 object 인지 체크
            else if (Selection.activeTransform)
            {
                CreateSelectLabel("Hierarchy Path", GetHiFullPath(Selection.activeGameObject.transform),
                true, COPY_HAN, new System.Action<string>((string str) =>
                {
                    CopyToClipboard(str);
                }));
            }
            else
            {
                CreateSelectLabel("Project Path", AssetDatabase.GetAssetPath(Selection.activeObject),
                true, COPY_HAN, new System.Action<string>((string str) =>
                {
                    CopyToClipboard(str);
                }));
            }
        }
        else
        {
            return;
        }
	}

    // Hoerarchy Object 클릭
    public void OnSelectionChange()
    {
        if (!Selection.activeObject)
        {
            return;
        }

        Repaint();
    }

    /// <summary>
    /// Selectlabel 텍스트 제작 1개
    /// </summary>
    private void CreateSelectLabel(string title, string content, bool btnCreate = false, string btnName = "", System.Action<string> a = null)
    {
        //EditorGUILayout.BeginHorizontal();

        EditorGUILayout.LabelField(title, EditorStyles.boldLabel);

        // 높이 재수정
        Rect position = EditorGUILayout.GetControlRect(false, 15f);
        EditorGUI.SelectableLabel(position, string.Format("{0}", content));
        
        if (btnCreate)
        {
            // 클립보드에 복사
            if (GUILayout.Button(btnName))
            {
                a(content);
            }
        }

        EditorGUILayout.Space();
        //EditorGUILayout.EndHorizontal();
    }

    // 하이라키창에서 오브젝트 전체 경로 얻어오기
    public static string GetHiFullPath(Transform trans)
    {
        string path = "/" + trans.name;
        while(trans.transform.parent != null)
        {
            trans = trans.parent;
            path = "/" + trans.name + path;
        }

        path = path.Substring(1);

        return path;
    }

    // 클립보드에다가 복사하기
    public static void CopyToClipboard(string str)
    {
        var textEditor = new TextEditor();
        textEditor.text = str;
        textEditor.SelectAll();
        textEditor.Copy();

        Debug.Log("Clipboard Copy : " + str);
    }
}

 

HierarchyPathMenuItem.cs

using UnityEditor;
using UnityEngine;
using System.Runtime.Remoting.Contexts;

public class HierarchyPathMenuItem
{
	// 경로
    private const string HIERARCHY_MENU_NAME = "GameObject/CopyToPath";
    private const string PROJECT_MENU_NAME = "Assets/CopyToPath";

    // Hierarchy 창에서의 특정 gameObject의 경로를 클립보드에 저장한다.
    [MenuItem(HIERARCHY_MENU_NAME, false, -10)]
    static void MainCopyToPath()
    {
        if (Selection.activeGameObject != null)
        HierarchyPathViewWindow.CopyToClipboard
            (HierarchyPathViewWindow.GetHiFullPath(Selection.activeGameObject.transform));
    }

    //[MenuItem(HIERARCHY_MENU_NAME, false, 10)]
    //static bool CopyToPath()
    //{
    //    return Selection.activeGameObject != null;
    //}

    // Asset Project 창에서 특정 파일의 경로를 저장한다.
    [MenuItem(PROJECT_MENU_NAME)]
    static void MainCopyToProjectPath()
    {
        HierarchyPathViewWindow.CopyToClipboard(
            AssetDatabase.GetAssetPath(Selection.activeObject));
    }

    [MenuItem(PROJECT_MENU_NAME, true, -10)]
    static bool CopyToProjectPath()
    {
        return Selection.activeObject != null;
    }
}

 

'Programming > Unity3D' 카테고리의 다른 글

Unity3d 오답노트  (0) 2020.06.04
AssetBundle 관련 정리  (0) 2020.04.23
How To Draw Line On Screen In Unity (C#)  (2) 2015.09.21
UForm (Unity3D + WinForm) 에 대한 고찰...  (0) 2015.07.27
AssetBundle 관하여... #1  (0) 2015.07.23
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기