Initialer Upload neues Unity-Projekt
This commit is contained in:
134
Assets/Oculus/Interaction/Editor/AutoWiring/AutoWiring.cs
Normal file
134
Assets/Oculus/Interaction/Editor/AutoWiring/AutoWiring.cs
Normal file
@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Oculus.Interaction.Editor
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public static class AutoWiring
|
||||
{
|
||||
static AutoWiring()
|
||||
{
|
||||
UnityObjectAddedBroadcaster.WhenComponentAdded += (component) =>
|
||||
{
|
||||
MonoBehaviour monoBehaviour = component as MonoBehaviour;
|
||||
if (monoBehaviour == null) return;
|
||||
|
||||
if (!_configs.TryGetValue(component.GetType(), out ComponentWiringStrategyConfig[] configs))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (ComponentWiringStrategyConfig config in configs)
|
||||
{
|
||||
AutoWireField(monoBehaviour, config.FieldName, config.Methods);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, ComponentWiringStrategyConfig[]> _configs = new
|
||||
Dictionary<Type, ComponentWiringStrategyConfig[]>();
|
||||
|
||||
public static void Register(Type type, ComponentWiringStrategyConfig[] fieldConfigs)
|
||||
{
|
||||
_configs.Add(type, fieldConfigs);
|
||||
}
|
||||
|
||||
public static void Unregister(Type type)
|
||||
{
|
||||
_configs.Remove(type);
|
||||
}
|
||||
|
||||
public static bool AutoWireField(MonoBehaviour monoBehaviour,
|
||||
string fieldName,
|
||||
FieldWiringStrategy[] wiringMethods)
|
||||
{
|
||||
FieldInfo field = FindField(fieldName, monoBehaviour.GetType());
|
||||
if (field == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
UnityEngine.Object value = field.GetValue(monoBehaviour) as UnityEngine.Object;
|
||||
if (value != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Undo.RecordObject(monoBehaviour, "Autowiring");
|
||||
|
||||
var interfaceAttribute = field.GetCustomAttribute<InterfaceAttribute>();
|
||||
var wirableTypes = interfaceAttribute != null ?
|
||||
interfaceAttribute.Types :
|
||||
new[] { field.FieldType };
|
||||
|
||||
if (wirableTypes != null)
|
||||
{
|
||||
foreach (var method in wiringMethods)
|
||||
{
|
||||
foreach (Type type in wirableTypes)
|
||||
{
|
||||
if (method.Invoke(monoBehaviour, field, type))
|
||||
{
|
||||
Component component = field.GetValue(monoBehaviour) as Component;
|
||||
Debug.Log("Auto-wiring succeeded: " + monoBehaviour.gameObject.name + "::" +
|
||||
monoBehaviour.GetType().Name + "." + field.Name +
|
||||
" was linked to " +
|
||||
component.gameObject.name + "::" + component.GetType().Name,
|
||||
monoBehaviour);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (field.GetCustomAttribute<OptionalAttribute>() == null)
|
||||
{
|
||||
Debug.LogWarning("Auto-wiring failed: no suitable targets for " +
|
||||
monoBehaviour.gameObject.name + "::" + monoBehaviour.GetType().Name +
|
||||
"." + field.Name + " could be found.",
|
||||
monoBehaviour);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static FieldInfo FindField(string fieldName, Type type)
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FieldInfo field = type.GetField(fieldName,
|
||||
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
if (field == null)
|
||||
{
|
||||
return FindField(fieldName, type.BaseType);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 87e14328c5481cd458e1749181aaa1f5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Oculus.Interaction.Editor
|
||||
{
|
||||
public delegate Boolean FieldWiringStrategy(MonoBehaviour monoBehaviour, FieldInfo fieldInfo, Type type);
|
||||
|
||||
public struct ComponentWiringStrategyConfig
|
||||
{
|
||||
public string FieldName { get; }
|
||||
public FieldWiringStrategy[] Methods { get; }
|
||||
|
||||
public ComponentWiringStrategyConfig(string fieldName, FieldWiringStrategy[] methods)
|
||||
{
|
||||
FieldName = fieldName;
|
||||
Methods = methods;
|
||||
}
|
||||
}
|
||||
|
||||
public class FieldWiringStrategies
|
||||
{
|
||||
public static bool WireFieldToAncestors(MonoBehaviour monoBehaviour, FieldInfo field, Type targetType)
|
||||
{
|
||||
for (var transform = monoBehaviour.transform.parent; transform != null; transform = transform.parent)
|
||||
{
|
||||
var component = transform.gameObject.GetComponent(targetType);
|
||||
if (component)
|
||||
{
|
||||
field.SetValue(monoBehaviour, component);
|
||||
EditorUtility.SetDirty(monoBehaviour);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool WireFieldToSceneComponent(MonoBehaviour monoBehaviour, FieldInfo field, Type targetType)
|
||||
{
|
||||
var rootObjs = SceneManager.GetActiveScene().GetRootGameObjects();
|
||||
foreach (var rootGameObject in rootObjs)
|
||||
{
|
||||
var component = rootGameObject.GetComponentInChildren(targetType, true);
|
||||
if (component != null)
|
||||
{
|
||||
field.SetValue(monoBehaviour, component);
|
||||
EditorUtility.SetDirty(monoBehaviour);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 789d3096184f6294e9fd9fe812cfdf19
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using Oculus.Interaction.Input;
|
||||
using Oculus.Interaction.PoseDetection;
|
||||
using UnityEditor;
|
||||
|
||||
namespace Oculus.Interaction.Editor
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public static class InteractionAutoWiring
|
||||
{
|
||||
static InteractionAutoWiring()
|
||||
{
|
||||
AutoWiring.Register(
|
||||
typeof(HandRef),
|
||||
new[] {
|
||||
new ComponentWiringStrategyConfig("_hand", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
AutoWiring.Register(
|
||||
typeof(ControllerRef),
|
||||
new[]
|
||||
{
|
||||
new ComponentWiringStrategyConfig("_controller", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
AutoWiring.Register(
|
||||
typeof(FingerFeatureStateProvider),
|
||||
new[] {
|
||||
new ComponentWiringStrategyConfig("_hand", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
AutoWiring.Register(
|
||||
typeof(TransformFeatureStateProvider),
|
||||
new[] {
|
||||
new ComponentWiringStrategyConfig("_hand", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
}),
|
||||
new ComponentWiringStrategyConfig("_trackingToWorldTransformer", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
}),
|
||||
new ComponentWiringStrategyConfig("_hmd", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors,
|
||||
FieldWiringStrategies.WireFieldToSceneComponent
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
AutoWiring.Register(
|
||||
typeof(JointDeltaProvider),
|
||||
new[] {
|
||||
new ComponentWiringStrategyConfig("_hand", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
#region HandGrab
|
||||
|
||||
AutoWiring.Register(
|
||||
typeof(HandGrab.HandGrabInteractable),
|
||||
new[] {
|
||||
new ComponentWiringStrategyConfig("_rigidbody", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
}),
|
||||
new ComponentWiringStrategyConfig("_pointableElement", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
}),
|
||||
new ComponentWiringStrategyConfig("_physicsGrabbable", new FieldWiringStrategy[]
|
||||
{
|
||||
FieldWiringStrategies.WireFieldToAncestors
|
||||
})
|
||||
}
|
||||
);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cda6aee7f7301f34d88ea873bdf0e310
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,193 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Licensed under the Oculus SDK License Agreement (the "License");
|
||||
* you may not use the Oculus SDK except in compliance with the License,
|
||||
* which is provided at the time of installation or download, or which
|
||||
* otherwise accompanies this software in either electronic or hard copy form.
|
||||
*
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://developer.oculus.com/licenses/oculussdk/
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, the Oculus SDK
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Oculus.Interaction.Editor
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
public static class UnityObjectAddedBroadcaster
|
||||
{
|
||||
public static event Action<GameObject> WhenGameObjectHierarchyAdded = (_) => {};
|
||||
public static event Action<Component> WhenComponentAdded = (_) => {};
|
||||
private static int _objectAddedUndoNestingCounter = 0;
|
||||
private static int _objectAddedUndoGroupId = -1;
|
||||
|
||||
static UnityObjectAddedBroadcaster()
|
||||
{
|
||||
HashSet<int> knownIds = new HashSet<int>();
|
||||
|
||||
EditorSceneManager.SceneOpenedCallback handleSceneOpened = (scene, mode) =>
|
||||
{
|
||||
UnityObjectAddedBroadcaster.HandleSceneOpened(scene, mode, knownIds);
|
||||
};
|
||||
|
||||
Action handleHierarchyChanged = () =>
|
||||
{
|
||||
UnityObjectAddedBroadcaster.HandleHierarchyChanged(knownIds);
|
||||
};
|
||||
|
||||
Action<Component> handleComponentWasAdded = (component) =>
|
||||
{
|
||||
UnityObjectAddedBroadcaster.HandleComponentWasAdded(component);
|
||||
};
|
||||
|
||||
AssemblyReloadEvents.AssemblyReloadCallback handleBeforeAssemblyReload = null;
|
||||
handleBeforeAssemblyReload = () =>
|
||||
{
|
||||
UnityObjectAddedBroadcaster.WhenGameObjectHierarchyAdded = (_) => { };
|
||||
UnityObjectAddedBroadcaster.WhenComponentAdded = (_) => { };
|
||||
|
||||
EditorSceneManager.sceneOpened -= handleSceneOpened;
|
||||
EditorApplication.hierarchyChanged -= handleHierarchyChanged;
|
||||
ObjectFactory.componentWasAdded -= handleComponentWasAdded;
|
||||
AssemblyReloadEvents.beforeAssemblyReload -= handleBeforeAssemblyReload;
|
||||
};
|
||||
|
||||
EditorSceneManager.sceneOpened += handleSceneOpened;
|
||||
EditorApplication.hierarchyChanged += handleHierarchyChanged;
|
||||
ObjectFactory.componentWasAdded += handleComponentWasAdded;
|
||||
AssemblyReloadEvents.beforeAssemblyReload += handleBeforeAssemblyReload;
|
||||
|
||||
for (int idx = 0; idx < SceneManager.loadedSceneCount; ++idx)
|
||||
{
|
||||
handleSceneOpened(EditorSceneManager.GetSceneAt(idx), OpenSceneMode.Additive);
|
||||
}
|
||||
}
|
||||
|
||||
private static void HandleSceneOpened(Scene scene, OpenSceneMode mode, HashSet<int> knownIds)
|
||||
{
|
||||
if (mode == OpenSceneMode.Single)
|
||||
{
|
||||
knownIds.Clear();
|
||||
}
|
||||
|
||||
AddInstanceIdsFromSubHierarchyToCache(knownIds, scene.GetRootGameObjects());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires signals for GameObjects and Components added through the addition of a prefab,
|
||||
/// checking whether the selected GameObject at the moment of a hierarchy change is
|
||||
/// unfamiliar (i.e., has an instance ID which is not already in known IDs) and signaling
|
||||
/// appropriately. Note that this will NOT signal GameObjects or Components added to the
|
||||
/// scene through prefab updating, which are added without modifying the Editor's
|
||||
/// selection variable, upon which this handler relies.
|
||||
/// </summary>
|
||||
/// <param name="knownIds">Cache of known GameObject instance IDs</param>
|
||||
private static void HandleHierarchyChanged(HashSet<int> knownIds)
|
||||
{
|
||||
if (EditorApplication.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var selection = Selection.activeGameObject;
|
||||
|
||||
if (selection == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!knownIds.Contains(selection.GetInstanceID()))
|
||||
{
|
||||
AddInstanceIdsFromSubHierarchyToCache(knownIds, selection);
|
||||
|
||||
StartUndoGroup();
|
||||
UnityObjectAddedBroadcaster.WhenGameObjectHierarchyAdded(selection);
|
||||
|
||||
// ObjectFactory.componentWasAdded is not called for components added to the scene
|
||||
// as part of a prefab, so we manually iterate them here so that
|
||||
// Signaler.WhenComponentAdded presents a more complete picture of activity in
|
||||
// the scene.
|
||||
var addedComponents = selection.GetComponentsInChildren<Component>(true);
|
||||
foreach (var component in addedComponents)
|
||||
{
|
||||
UnityObjectAddedBroadcaster.WhenComponentAdded(component);
|
||||
}
|
||||
EndUndoGroup();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires signals for Components added to existing GameObjects. Note that this will
|
||||
/// NOT signal Components added to the scene through prefab updating, which are added
|
||||
/// without triggering the ObjectFactory, upon which this handler relies.
|
||||
/// </summary>
|
||||
/// <param name="component">The component added to the scene</param>
|
||||
private static void HandleComponentWasAdded(Component component)
|
||||
{
|
||||
if (EditorApplication.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StartUndoGroup();
|
||||
UnityObjectAddedBroadcaster.WhenComponentAdded(component);
|
||||
EndUndoGroup();
|
||||
}
|
||||
|
||||
public static void HandleObjectWasAdded(GameObject gameObject)
|
||||
{
|
||||
StartUndoGroup();
|
||||
var addedComponents = gameObject.GetComponentsInChildren<Component>(true);
|
||||
foreach (var component in addedComponents)
|
||||
{
|
||||
UnityObjectAddedBroadcaster.WhenComponentAdded(component);
|
||||
}
|
||||
EndUndoGroup();
|
||||
}
|
||||
|
||||
private static void AddInstanceIdsFromSubHierarchyToCache(HashSet<int> cache, params GameObject[] subHierarchyRoots)
|
||||
{
|
||||
foreach (var gameObject in subHierarchyRoots)
|
||||
{
|
||||
cache.Add(gameObject.GetInstanceID());
|
||||
for (int idx = 0; idx < gameObject.transform.childCount; ++idx)
|
||||
{
|
||||
AddInstanceIdsFromSubHierarchyToCache(cache, gameObject.transform.GetChild(idx).gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void StartUndoGroup()
|
||||
{
|
||||
if (_objectAddedUndoNestingCounter == 0)
|
||||
{
|
||||
_objectAddedUndoGroupId = Undo.GetCurrentGroup() - 1;
|
||||
}
|
||||
_objectAddedUndoNestingCounter++;
|
||||
}
|
||||
|
||||
private static void EndUndoGroup()
|
||||
{
|
||||
_objectAddedUndoNestingCounter--;
|
||||
if (_objectAddedUndoNestingCounter == 0)
|
||||
{
|
||||
Undo.FlushUndoRecordObjects();
|
||||
Undo.CollapseUndoOperations(_objectAddedUndoGroupId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6713358c2c4d39e4ba0eae4312ee2580
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user