Created
February 27, 2014 21:38
-
-
Save allanolivei/9260107 to your computer and use it in GitHub Desktop.
Unity 3d : Get Selected Folder in Project Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.IO; | |
public static class UnityUtil | |
{ | |
public static string GetSelectedPathOrFallback() | |
{ | |
string path = "Assets"; | |
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets)) | |
{ | |
path = AssetDatabase.GetAssetPath(obj); | |
if ( !string.IsNullOrEmpty(path) && File.Exists(path) ) | |
{ | |
path = Path.GetDirectoryName(path); | |
break; | |
} | |
} | |
return path; | |
} | |
} |
For extensive answer, please see my stackoverflow answer.
https://stackoverflow.com/questions/32318320/getting-path-of-right-click-in-unity-3d-editor/66381542#66381542
Stable cross-platform solution, supports folders and files. Default path is Application.dataPath
public static string GetProjectWindowFolder()
{
string projectPath = new DirectoryInfo(Application.dataPath).Parent.FullName;
string objectProjectPath = AssetDatabase.GetAssetPath(Selection.activeObject);
string objectAbsolutePath = string.IsNullOrEmpty(objectProjectPath) ? Application.dataPath : $"{projectPath}/{objectProjectPath}";
string objectCorrectAbsolutePath = objectAbsolutePath.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
string folderAbsolutePath = File.Exists(objectCorrectAbsolutePath) ? Path.GetDirectoryName(objectCorrectAbsolutePath) : objectCorrectAbsolutePath;
return Path.GetRelativePath(projectPath, folderAbsolutePath);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and any way to open this folder like EditorGUIUtility.PingObject?? I want fast jump to this folder on editor (because I'm searching file and want jump to this folder)