Skip to content

Instantly share code, notes, and snippets.

@Hafune
Created July 25, 2025 11:23
Show Gist options
  • Save Hafune/a042019b1ee6720183f0bd212b3932ab to your computer and use it in GitHub Desktop.
Save Hafune/a042019b1ee6720183f0bd212b3932ab to your computer and use it in GitHub Desktop.
using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace Core
{
public static class CreateReferencePathsForSelected
{
[MenuItem("Auto/Create reference paths for selected", false, 1)]
private static void CreateMany()
{
//https://discussions.unity.com/t/how-to-get-path-from-the-current-opened-folder-in-the-project-window-in-unity-editor/226209
var projectWindowUtilType = typeof(ProjectWindowUtil);
var getActiveFolderPath =
projectWindowUtilType.GetMethod("GetActiveFolderPath", BindingFlags.Static | BindingFlags.NonPublic)!;
var obj = getActiveFolderPath.Invoke(null, Array.Empty<object>());
var pathToCurrentFolder = obj.ToString();
foreach (var go in Selection.gameObjects)
CreateReferencePath(go, pathToCurrentFolder);
}
private static void CreateReferencePath(GameObject go, string pathToCurrentFolder)
{
var stage = PrefabStageUtility.GetCurrentPrefabStage();
if (!stage)
return;
var refPath = ScriptableObject.CreateInstance<ReferencePath>();
refPath.SetPrefabPart(AssetDatabase.LoadAssetAtPath<GameObject>(stage.assetPath), ReferencePath.GetPath(go));
AssetDatabase.CreateAsset(refPath, pathToCurrentFolder + "/Path_" + go.name + ".asset");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment