"Assets/Create/U# Script" still fails if destination file is outside project root
SenkyDragon
https://feedback.vrchat.com/sdk-bug-reports/p/assets-create-u-script-fails-if-destination-file-is-outside-project-root was marked as fixed in sdk 3.10.5, but the fix was not comprehensive.
Packages added from disk during development (click "Add package from disk" in unity's Package Manager tab) are not inside the project root, and now result in "UdonSharp scripts must be created inside the project's Assets or Packages folder" which is not correct.
If validation is still needed, you can check if an absolute path is inside one of these "outside of root" package folders by using
!Path.IsPathRooted(FileUtil.GetLogicalPath(scriptFilePath))
, and then using FileUtil.GetLogicalPath(scriptFilePath)
as the projectRelativeScriptPath
The
Unity's CreateAsset() cannot create assets in Packages
comment in the current code is also incorrect. Unity can definitely create assets within Packages, the current implementation just doesn't deduce the relative path properly.The entire method can be replaced by this implementation, which solves all of those issues:
internal static string CreateUSharpScript(string folderPath, bool createProgramAsset)
{
string chosenFilePath = EditorUtility.SaveFilePanel("Save UdonSharp File", folderPath, string.Empty, "cs");
if (chosenFilePath.Length > 0)
{
string chosenDirectory = Path.GetDirectoryName(chosenFilePath)!;
string sanitizedFileName = UdonSharpSettings.SanitizeName(Path.GetFileNameWithoutExtension(chosenFilePath));
string scriptFilePath = Path.Combine(chosenDirectory, $"{sanitizedFileName}.cs").Replace('\\', '/');
string assetFilePath = Path.Combine(chosenDirectory, $"{sanitizedFileName}.asset").Replace('\\', '/');
string projectRelativeScriptPath = Path.IsPathRooted(FileUtil.GetLogicalPath(scriptFilePath))
? FileUtil.GetProjectRelativePath(scriptFilePath)
: FileUtil.GetLogicalPath(scriptFilePath);
string projectRelativeAssetPath = Path.IsPathRooted(FileUtil.GetLogicalPath(assetFilePath))
? FileUtil.GetProjectRelativePath(assetFilePath)
: FileUtil.GetLogicalPath(assetFilePath);
if (projectRelativeScriptPath.Length == 0)
{
EditorUtility.DisplayDialog("Invalid path", "UdonSharp scripts must be created inside the project's Assets or Packages folders.", "Ok");
return null;
}
if (createProgramAsset)
{
if (AssetDatabase.LoadAssetAtPath<UdonSharpProgramAsset>(projectRelativeAssetPath) != null)
{
if (!EditorUtility.DisplayDialog("File already exists", $"Corresponding asset file '{projectRelativeAssetPath}' already found for new UdonSharp script. Overwrite?", "Ok", "Cancel"))
{
return null;
}
}
}
// Commit to creation from this point
string fileContents = UdonSharpSettings.GetProgramTemplateString(sanitizedFileName);
File.WriteAllText(scriptFilePath, fileContents, System.Text.Encoding.UTF8);
AssetDatabase.ImportAsset(projectRelativeScriptPath, ImportAssetOptions.ForceSynchronousImport);
if (createProgramAsset)
{
MonoScript newScript = AssetDatabase.LoadAssetAtPath<MonoScript>(projectRelativeScriptPath);
UdonSharpProgramAsset newProgramAsset = CreateInstance<UdonSharpProgramAsset>();
newProgramAsset.sourceCsScript = newScript;
AssetDatabase.CreateAsset(newProgramAsset, projectRelativeAssetPath);
}
AssetDatabase.Refresh();
return projectRelativeScriptPath;
}
return null;
}
Log In
SenkyDragon
This was re-harmony patched in VRCFury 1.1427.