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

Add simulator to xr rig list TRNG-1398 #82

Merged
merged 8 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions Editor/Innoactive.CreatorEditor.XRInteraction.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
"name": "com.unity.xr.interaction.toolkit",
"expression": "0.10.0-preview",
"define": "XRIT_0_10_OR_NEWER"
},
{
"name": "com.unity.xr.interaction.toolkit",
"expression": "1.0.0-pre.2",
"define": "XRIT_1_0_OR_NEWER"
}
],
"noEngineReferences": false
Expand Down
2 changes: 1 addition & 1 deletion Editor/PackageDependencies/XRInteractionPackageEnabler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Adds Unity's XR-Interaction-Toolkit package as a dependency and sets specified symbol for script compilation.
/// </summary>
public class XRInteractionPackageEnabler : Dependency
public class XRInteractionPackageEnabler : Dependency
{
/// <inheritdoc/>
public override string Package { get; } = "com.unity.xr.interaction.toolkit";
Expand Down
8 changes: 8 additions & 0 deletions Editor/SceneSetup.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
using UnityEditor;
using UnityEngine;
using Innoactive.CreatorEditor.BasicInteraction;
using Innoactive.CreatorEditor.PackageManager.XRInteraction;

namespace Innoactive.CreatorEditor.XRInteraction
{

/// <summary>
/// Scene setup for XR-Interaction.
/// </summary>
public class XRInteractionSceneSetup : InteractionFrameworkSceneSetup
{
private const string Title = "Obsolete XR Ring detected";

private const string Message = "Creator changed the Rig loading to a new dynamic system, you still have the old XR_Setup in the current scene, do you want to delete it?";

/// <inheritdoc />
public override string Key { get; } = "XRInteractionSetup";

/// <inheritdoc />
public override void Setup()
{
DeleteStaticObject("[XR_Setup]");

#if XRIT_1_0_OR_NEWER
XRSimulatorImporter simulatorImporter = new XRSimulatorImporter();

if (string.IsNullOrEmpty(simulatorImporter.SimulatorRigPath) || AssetDatabase.GetMainAssetTypeAtPath(simulatorImporter.SimulatorRigPath) == null)
{
simulatorImporter.ImportSimulatorRig();
}
#endif
}

private void DeleteStaticObject(string objectName)
Expand All @@ -26,9 +36,9 @@ private void DeleteStaticObject(string objectName)

if (objectToDelete != null)
{
string Message = $"Creator changed the XR Rig loading to a new dynamic system, you have a static {objectName} in the current scene, do you want to delete it?";
string message = $"Creator changed the XR Rig loading to a new dynamic system, you have a static {objectName} in the current scene, do you want to delete it?";

if (EditorUtility.DisplayDialog(Title, Message, "Delete", "Skip"))
if (EditorUtility.DisplayDialog(Title, message, "Delete", "Skip"))
{
EditorUtility.SetDirty(objectToDelete);
Object.DestroyImmediate(objectToDelete);
Expand Down
66 changes: 66 additions & 0 deletions Editor/SceneSetup/XRSimulatorImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#if XRIT_1_0_OR_NEWER
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Innoactive.CreatorEditor.PackageManager.XRInteraction
{
/// <summary>
/// Helper class for generate a new XR Simulator Rig out of the `[XR_Setup_Action_Based]` and the `XR Device Simulator` sample.
Gusinuhe marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
internal class XRSimulatorImporter
{
public string SimulatorRigPath { get; } = null;

private const string SimulatorPathKey = "SimulatorRigPath";
private const string SamplePrefabName = "XR Device Simulator";
private const string ActionRigName = "[XR_Setup_Action_Based]";
private const string SimulatorPrefabName = "[XR_Setup_Simulator]";

public XRSimulatorImporter()
{
SimulatorRigPath = EditorPrefs.GetString(SimulatorPathKey);
}

/// <summary>
/// Imports a new `[XR_Setup_Simulator]` prefab based on the `[XR_Setup_Action_Based]` and the `XR Device Simulator` prefabs.
/// </summary>
/// <remarks>The generated prefab is imported into the `XR Interaction Component`’s `Resources` folder.</remarks>
public void ImportSimulatorRig()
{
GameObject simulator = LoadPrefab(SamplePrefabName, "Samples", out string simulatorRigPath);
GameObject actionRig = LoadPrefab(ActionRigName, "Innoactive", out string actionRigPath);

if (simulator == null || actionRig == null)
{
Debug.LogError($"{SimulatorPrefabName} could not be generated. {(simulator == null ? SamplePrefabName : ActionRigName)} is missing.");
return;
}

simulatorRigPath = $"{Path.GetDirectoryName(actionRigPath)}/{SimulatorPrefabName}.prefab";

simulator.transform.SetParent(actionRig.transform);
PrefabUtility.SaveAsPrefabAsset(actionRig, simulatorRigPath);

EditorPrefs.SetString(SimulatorPathKey, simulatorRigPath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Editor Prefs are for the editor not the project. Is this on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this only meant to use if the XR Simulator is not existing and the user sets a training scene from the innoactive menu.

PrefabUtility.UnloadPrefabContents(simulator);
}

private GameObject LoadPrefab(string prefabName, string searchFolder, out string assetPath)
{
string filter = $"t:Prefab {prefabName}";
string prefabGUID = AssetDatabase.FindAssets(filter, new[] {$"Assets/{searchFolder}"}).FirstOrDefault();

if (string.IsNullOrEmpty(prefabGUID) == false)
{
assetPath = AssetDatabase.GUIDToAssetPath(prefabGUID);
return PrefabUtility.LoadPrefabContents(assetPath);
}

assetPath = string.Empty;
return null;
}
}
}
#endif
11 changes: 11 additions & 0 deletions Editor/SceneSetup/XRSimulatorImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Runtime/Rigs/XRSimulatorSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Innoactive.Creator.Components.Runtime.Rigs
{
/// <summary>
/// Setup for XR Device Simulator.
/// </summary>
public class XRSimulatorSetup : XRSetupBase
{
/// <inheritdoc />
public override string Name { get; } = "XR Simulator";

/// <inheritdoc />
public override string PrefabName { get; } = "[XR_Setup_Simulator]";

/// <inheritdoc />
public override bool CanBeUsed()
{
#if ENABLE_INPUT_SYSTEM && XRIT_1_0_OR_NEWER
return IsEventManagerInScene() == false;
#else
return false;
#endif
}

/// <inheritdoc />
public override string GetSetupTooltip()
{
#if !XRIT_1_0_OR_NEWER
return "Please upgrade the XR Interaction Toolkit from the Package Manager to the latest available version.";
#elif ENABLE_INPUT_SYSTEM
return "Can't be used while there is already a XRInteractionManager in the scene.";
#else
return "Enable the new input system to allow using this rig.";
#endif
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Rigs/XRSimulatorSetup.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.