Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Snap Zone generator now handles invalid characters TRNG-1045 #35

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions Editor/Properties/SnappablePropertyEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Innoactive.Creator.XRInteraction;
using Innoactive.Creator.XRInteraction.Properties;
Expand Down Expand Up @@ -37,8 +38,8 @@ private void CreateSnapZone(SnappableProperty snappable)

SetHighlightMaterial(snapZoneBlueprint, settings.HighlightMaterial);
GameObject snapZonePrefab = SaveSnapZonePrefab(snapZoneBlueprint);

GameObject snapObject = new GameObject($"{snappable.name}SnapZone");
GameObject snapObject = new GameObject($"{CleanName(snappable.name)}_SnapZone");
Undo.RegisterCreatedObjectUndo(snapObject, $"Create {snapObject.name}");
EditorUtility.CopySerialized(snappable.transform, snapObject.transform);

Expand All @@ -63,7 +64,7 @@ private void CreateSnapZone(SnappableProperty snappable)

private GameObject DuplicateObject(GameObject originalObject, Transform parent = null)
{
GameObject cloneObject = new GameObject($"{originalObject.name}_Clone");
GameObject cloneObject = new GameObject($"{CleanName(originalObject.name)}_Highlight.prefab");

if (parent != null)
{
Expand Down Expand Up @@ -144,8 +145,28 @@ private GameObject SaveSnapZonePrefab(GameObject snapZoneBlueprint)
snapZoneBlueprint.transform.position = Vector3.zero;
snapZoneBlueprint.transform.rotation = Quaternion.identity;

string prefabName = snapZoneBlueprint.name.Replace("_Clone", "Highlight.prefab");
return PrefabUtility.SaveAsPrefabAsset(snapZoneBlueprint, $"{PrefabPath}/{prefabName}");
string prefabPath = $"{PrefabPath}/{snapZoneBlueprint.name}";
GameObject prefab = PrefabUtility.SaveAsPrefabAsset(snapZoneBlueprint, prefabPath);

if (prefab != null)
{
Debug.LogWarningFormat("A new highlight prefab was saved at {0}", prefabPath);
}

return prefab;
}

private string CleanName(string originalName)
{
// Unity replaces invalid characters with '_' when creating new prefabs in the editor.
// We try to simulate that behavior.
foreach (char invalidCharacter in Path.GetInvalidFileNameChars())
{
originalName = originalName.Replace(invalidCharacter, '_');
}

// Non windows systems consider '\' as a valid file name.
return originalName.Replace('\\', '_');
}
}
}