initial commit

This commit is contained in:
Jo 2025-01-07 02:06:59 +01:00
parent 6715289efe
commit 788c3389af
37645 changed files with 2526849 additions and 80 deletions

View file

@ -0,0 +1,90 @@
using System.Linq;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
namespace Samples
{
/// <summary>
/// Simple build processor that makes sure that any custom configuration that the user creates is
/// correctly passed on to the provider implementation at runtime.
///
/// Custom configuration instances that are stored in EditorBuildSettings are not copied to the target build
/// as they are considered unreferenced assets. In order to get them to the runtime side of things, they need
/// to be serialized to the build app and deserialized at runtime. Previously this would be a manual process
/// requiring the implementor to manually serialize to some location that can then be read from to deserialize
/// at runtime. With the new PlayerSettings Preloaded Assets API we can now just add our asset to the preloaded
/// list and have it be instantiated at app launch.
///
/// Note that the preloaded assets are only notified with Awake, so anything you want or need to do with the
/// asset after launch needs to be handled there.
///
/// More info on APIs used here:
/// * &lt;a href="https://docs.unity3d.com/ScriptReference/EditorBuildSettings.html"&gt;EditorBuildSettings&lt;/a&gt;
/// * &lt;a href="https://docs.unity3d.com/ScriptReference/PlayerSettings.GetPreloadedAssets.html&gt;PlayerSettings.GetPreloadedAssets&lt;/a&gt;
/// * &lt;a href="https://docs.unity3d.com/ScriptReference/PlayerSettings.SetPreloadedAssets.html"&gt;PlayerSettings.SetPreloadedAssets&lt;/a&gt;
/// </summary>
public class SampleBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
/// <summary>Override of <see cref="IPreprocessBuildWithReport"/> and <see cref="IPostprocessBuildWithReport"/></summary>
public int callbackOrder
{
get { return 0; }
}
void CleanOldSettings()
{
UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
if (preloadedAssets == null)
return;
var oldSettings = from s in preloadedAssets
where s != null && s.GetType() == typeof(SampleSettings)
select s;
if (oldSettings != null && oldSettings.Any())
{
var assets = preloadedAssets.ToList();
foreach (var s in oldSettings)
{
assets.Remove(s);
}
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}
}
/// <summary>Override of <see cref="IPreprocessBuildWithReport"/></summary>
/// <param name="report">Build report.</param>
public void OnPreprocessBuild(BuildReport report)
{
// Always remember to cleanup preloaded assets after build to make sure we don't
// dirty later builds with assets that may not be needed or are out of date.
CleanOldSettings();
SampleSettings settings = null;
EditorBuildSettings.TryGetConfigObject(SampleConstants.k_SettingsKey, out settings);
if (settings == null)
return;
UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
if (!preloadedAssets.Contains(settings))
{
var assets = preloadedAssets.ToList();
assets.Add(settings);
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}
}
/// <summary>Override of <see cref="IPostprocessBuildWithReport"/></summary>
/// <param name="report">Build report.</param>
public void OnPostprocessBuild(BuildReport report)
{
// Always remember to cleanup preloaded assets after build to make sure we don't
// dirty later builds with assets that may not be needed or are out of date.
CleanOldSettings();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9216d372fe0de4086af31dce05526406
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,85 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.XR.Management;
using UnityEditor.XR.Management.Metadata;
namespace Samples
{
class SamplePackage : IXRPackage
{
class SampleLoaderMetadata : IXRLoaderMetadata
{
public string loaderName { get; set; }
public string loaderType { get; set; }
public List<BuildTargetGroup> supportedBuildTargets { get; set; }
}
class SamplePackageMetadata : IXRPackageMetadata
{
public string packageName { get; set; }
public string packageId { get; set; }
public string settingsType { get; set; }
public List<IXRLoaderMetadata> loaderMetadata { get; set; }
}
static IXRPackageMetadata s_Metadata = new SamplePackageMetadata() {
packageName = "Sample Package <SAMPLE ONLY YOU MUST REIMPLEMENT>",
packageId = "com.unity.xr.samplespackage",
settingsType = typeof(SampleSettings).FullName,
loaderMetadata = new List<IXRLoaderMetadata>() {
new SampleLoaderMetadata() {
loaderName = "Sample Loader One <SAMPLE ONLY YOU MUST REIMPLEMENT>",
loaderType = typeof(SampleLoader).FullName,
supportedBuildTargets = new List<BuildTargetGroup>() {
BuildTargetGroup.Standalone,
BuildTargetGroup.WSA
}
},
new SampleLoaderMetadata() {
loaderName = "Sample Loader Two <SAMPLE ONLY YOU MUST REIMPLEMENT>",
loaderType = typeof(SampleLoader).FullName,
supportedBuildTargets = new List<BuildTargetGroup>() {
BuildTargetGroup.Android,
BuildTargetGroup.iOS,
#if !UNITY_2021_2_OR_NEWER
BuildTargetGroup.Lumin
#endif
}
}
}
};
const string k_PackageNotificationTooltip =
@"This loader is purely a sample and will not load any XR Device.
This message is a part of sample code to show how to register a loader that might contain issues or require additonal
context. One example could be that the package that contains this loader is being deprecated and any user who intends to
use the package needs to be aware of deprecation.
Click this icon to be taken to the XR Plug-in Management documentation home page.";
const string k_PackageNotificationIcon = "console.warnicon.sml";
const string k_PackageNotificationManagementDocsURL = @"https://docs.unity3d.com/Packages/com.unity.xr.management@latest/index.html";
public IXRPackageMetadata metadata
{
get
{
// Register package notification information anytime the metadata is asked requested.
var packageNotificationInfo = new PackageNotificationInfo(
EditorGUIUtility.IconContent(k_PackageNotificationIcon),
k_PackageNotificationTooltip,
k_PackageNotificationManagementDocsURL);
PackageNotificationUtils.RegisterPackageNotificationInformation(s_Metadata.packageId, packageNotificationInfo);
return s_Metadata;
}
}
public bool PopulateNewSettingsInstance(ScriptableObject obj)
{
return true;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 096597fc41af0774aaedec0171d8566b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,60 @@
using UnityEditor;
using UnityEngine;
namespace Samples
{
/// <summary>
/// Simple custom editor used to show how to enable custom UI for XR Management
/// configuraton data.
/// </summary>
[CustomEditor(typeof(SampleSettings))]
public class SampleSettingsEditor : Editor
{
static string k_RequiresProperty = "m_RequiresItem";
static string k_RuntimeToggleProperty = "m_RuntimeToggle";
static GUIContent k_ShowBuildSettingsLabel = new GUIContent("Build Settings");
static GUIContent k_RequiresLabel = new GUIContent("Item Requirement");
static GUIContent k_ShowRuntimeSettingsLabel = new GUIContent("Runtime Settings");
static GUIContent k_RuntimeToggleLabel = new GUIContent("Should I stay or should I go?");
bool m_ShowBuildSettings = true;
bool m_ShowRuntimeSettings = true;
SerializedProperty m_RequiesItemProperty;
SerializedProperty m_RuntimeToggleProperty;
/// <summary>Override of Editor callback.</summary>
public override void OnInspectorGUI()
{
if (serializedObject == null || serializedObject.targetObject == null)
return;
if (m_RequiesItemProperty == null) m_RequiesItemProperty = serializedObject.FindProperty(k_RequiresProperty);
if (m_RuntimeToggleProperty == null) m_RuntimeToggleProperty = serializedObject.FindProperty(k_RuntimeToggleProperty);
serializedObject.Update();
m_ShowBuildSettings = EditorGUILayout.Foldout(m_ShowBuildSettings, k_ShowBuildSettingsLabel);
if (m_ShowBuildSettings)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_RequiesItemProperty, k_RequiresLabel);
EditorGUI.indentLevel--;
}
EditorGUILayout.Space();
m_ShowRuntimeSettings = EditorGUILayout.Foldout(m_ShowRuntimeSettings, k_ShowRuntimeSettingsLabel);
if (m_ShowRuntimeSettings)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_RuntimeToggleProperty, k_RuntimeToggleLabel);
EditorGUI.indentLevel--;
}
serializedObject.ApplyModifiedProperties();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 81f90f56b259f4aba826980c947f4140
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,103 @@
using System;
using UnityEditor;
using UnityEditor.XR.Management;
using UnityEngine;
namespace Samples
{
/// <summary>
/// Sample loader UI demonstrating how to provide your own loader selection UI for the
/// loader selection list.
/// </summary>
[XRCustomLoaderUI("Samples.SampleLoader", BuildTargetGroup.Standalone)]
public class SampleStandaloneLoaderUI : IXRCustomLoaderUI
{
static readonly string[] features = new string[]{
"Feature One",
"Feature Two",
"Feature Three"
};
struct Content
{
public static readonly GUIContent k_LoaderName = new GUIContent("Sample Loader One Custom <SAMPLE ONLY YOU MUST REIMPLEMENT>");
public static readonly GUIContent k_Download = new GUIContent("Download");
public static readonly GUIContent k_WarningIcon = EditorGUIUtility.IconContent("console.warnicon.sml");
}
float renderLineHeight = 0;
/// <inheritdoc />
public bool IsLoaderEnabled { get; set; }
/// <inheritdoc />
public string[] IncompatibleLoaders => new string[] { "UnityEngine.XR.WindowsMR.WindowsMRLoader" };
/// <inheritdoc />
public float RequiredRenderHeight { get; private set; }
/// <inheritdoc />
public void SetRenderedLineHeight(float height)
{
renderLineHeight = height;
RequiredRenderHeight = height;
if (IsLoaderEnabled)
{
RequiredRenderHeight += features.Length * height;
}
}
/// <inheritdoc />
public BuildTargetGroup ActiveBuildTargetGroup { get; set; }
/// <inheritdoc />
public void OnGUI(Rect rect)
{
var size = EditorStyles.toggle.CalcSize(Content.k_LoaderName);
var labelRect = new Rect(rect);
labelRect.width = size.x;
labelRect.height = renderLineHeight;
IsLoaderEnabled = EditorGUI.ToggleLeft(labelRect, Content.k_LoaderName, IsLoaderEnabled);
// The following shows how to make draw an icon with a tooltip
size = EditorStyles.label.CalcSize(Content.k_WarningIcon);
var imageRect = new Rect(rect);
imageRect.xMin = labelRect.xMax + 1;
imageRect.width = size.y;
imageRect.height = renderLineHeight;
var iconWithTooltip = new GUIContent("", Content.k_WarningIcon.image, "Warning: This is a sample to show how to draw a custom icon with a tooltip!");
EditorGUI.LabelField(imageRect, iconWithTooltip);
if (IsLoaderEnabled)
{
EditorGUI.indentLevel++;
var featureRect = new Rect(rect);
featureRect.yMin = labelRect.yMax + 1;
featureRect.height = renderLineHeight;
foreach (var feature in features)
{
var buttonSize = EditorStyles.toggle.CalcSize(Content.k_Download);
var featureLabelRect = new Rect(featureRect);
featureLabelRect.width -= buttonSize.x;
EditorGUI.ToggleLeft(featureLabelRect, feature, false);
var buttonRect = new Rect(featureRect);
buttonRect.xMin = featureLabelRect.xMax + 1;
buttonRect.width = buttonSize.x;
if (GUI.Button(buttonRect, Content.k_Download))
{
Debug.Log($"{feature} download button pressed. Do something here!");
}
featureRect.yMin += renderLineHeight;
featureRect.height = renderLineHeight;
}
EditorGUI.indentLevel--;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f20344b9494eebc47b9860adf04f6667
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,14 @@
{
"name": "Unity.XR.Management.Samples.Editor",
"references": [
"Unity.XR.Management.Editor",
"Unity.XR.Management.Samples",
"Unity.XR.Management"
],
"optionalUnityReferences": [
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": []
}

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 20d89efd2cc1043e5ac2c8f870d4e8fa
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: