Unity/C#

유니티 오브젝트 경로 관련

모카쨩 2024. 8. 23. 06:30


Root를 제외한 경로반환

v1 ~ v3

더보기


가령 Root/armature/Hips는 /armature/Hips가 된다

string ArmaturePath(Transform bone)
{
    var rootName = bone.transform.root.name;
    var hierarchyPath = bone.transform.GetHierarchyPath();
    return hierarchyPath.Substring(rootName.Length, hierarchyPath.Length - rootName.Length);
}

 

 


Root를 제외한 경로반환 v2 (상대경로)

인자를 하나 더 받고 안정성을 높였다

https://gist.github.com/ahzkwid/c89b626c0e7bace5cc317901b524672e

static string RelativePath(Transform target, Transform root = null)
{
    var rootName = "";
    var hierarchyPath = "";


    if (root != null)
    {
        try
        {
            rootName = SearchUtils.GetHierarchyPath(root.gameObject, false);

        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex);
            Debug.LogError(root.ToString() + "nothing root");
            //Debug.LogError(bone.name + "는 root가 없음");
            throw;
        }
    }
    try
    {
        //hierarchyPath = bone.GetHierarchyPath();
        hierarchyPath = SearchUtils.GetHierarchyPath(target.gameObject, false);
    }
    catch (System.Exception ex)
    {
        Debug.LogError(ex);
        Debug.LogError(target.ToString() + "nothing GetHierarchyPath");
        throw;
    }

    var startIndex = rootName.Length;

    return hierarchyPath.Substring(startIndex, hierarchyPath.Length - startIndex);
}

 

v3

static string RelativePath(Transform target, Transform root = null)
{
    var rootName = "";
    var hierarchyPath = "";


    if (root != null)
    {
        try
        {
            rootName = SearchUtils.GetHierarchyPath(root.gameObject, false);

        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex);
            Debug.LogError(root.ToString() + "nothing root");
            //Debug.LogError(bone.name + "는 root가 없음");
            throw;
        }
    }
    try
    {
        //hierarchyPath = bone.GetHierarchyPath();
        hierarchyPath = SearchUtils.GetHierarchyPath(target.gameObject, false);
    }
    catch (System.Exception ex)
    {
        Debug.LogError(ex);
        Debug.LogError(target.ToString() + "nothing GetHierarchyPath");
        throw;
    }

    var startIndex = rootName.Length;

    if (rootName.Length > hierarchyPath.Length)
    {
        Debug.LogWarning("rootName.Length > hierarchyPath.Length");
        Debug.LogWarning($"{rootName} > {hierarchyPath}");
        return null;
    }

    if (hierarchyPath.Substring(0, rootName.Length) != rootName)
    {
        Debug.LogWarning("hierarchyPath.Substring(0, rootName.Length) != rootName");
        Debug.LogWarning($"{rootName} != {hierarchyPath}.Substring(0, rootName.Length)");
        return null;
    }

    try
    {
        return hierarchyPath.Substring(startIndex, hierarchyPath.Length - startIndex);
    }
    catch (System.Exception ex)
    {
        Debug.LogError(ex);
        Debug.LogError($"{hierarchyPath} - {rootName}");
        throw;
    }
}

 

 

v4

경우에 따라 앞에 슬래시는 없애도 된다

var relativePath = "/"+Path.GetRelativePath(rootpath, path);

 

 

 

 

 

 

하이어라키 경로 반환

var path = SearchUtils.GetHierarchyPath(gameObject, false);

 

 

 

패스 병합

Replace 붙은 이유는 유니티는 '/'를 쓰는데 일부 로직에서 '\'를 인식 못하기 때문

var path = Path.Join(parentPath, childPath).Replace("\\", "/");