Initial Commit

This commit is contained in:
2025-07-04 20:33:06 +03:00
commit 8f09347ae0
9219 changed files with 2447903 additions and 0 deletions
@@ -0,0 +1,269 @@
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
https://developer.oculus.com/licenses/oculussdk/
Unless required by applicable law or agreed to in writing, the Utilities 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.Linq;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace Oculus.Interaction.InterfaceSupport
{
/// <summary>
/// This property drawer is the meat of the interface support implementation. When
/// the value of field with this attribute is modified, the new value is tested
/// against the interface expected. If the component matches, the new value is
/// accepted. Otherwise, the old value is maintained.
/// </summary>
[CustomPropertyDrawer(typeof(InterfaceAttribute))]
public class InterfaceDrawer : PropertyDrawer
{
private int _filteredObjectPickerID;
private static readonly Type[] _singleMonoBehaviourType = new Type[1] { typeof(MonoBehaviour) };
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
_filteredObjectPickerID = GUIUtility.GetControlID(FocusType.Passive);
if (property.serializedObject.isEditingMultipleObjects) return;
if (property.propertyType != SerializedPropertyType.ObjectReference)
{
EditorGUI.LabelField(position, label.text, "InterfaceAttribute can only " +
"be used with Object Reference fields.");
return;
}
EditorGUI.BeginProperty(position, label, property);
Type[] attTypes = GetInterfaceTypes(property);
// Pick a specific component
UnityEngine.Object oldObject = property.objectReferenceValue;
GameObject temporaryGameObject = null;
string oldObjectName = "";
if (Event.current.type == EventType.Repaint)
{
string attTypesName = GetTypesName(attTypes);
if (oldObject == null)
{
temporaryGameObject = new GameObject("None" + " (" + attTypesName + ")");
temporaryGameObject.hideFlags = HideFlags.HideAndDontSave;
oldObject = temporaryGameObject;
}
else
{
oldObjectName = oldObject.name;
oldObject.name = oldObjectName + " (" + attTypesName + ")";
}
}
UnityEngine.Object candidateObject =
EditorGUI.ObjectField(position, label, oldObject, typeof(UnityEngine.Object), true);
int objectPickerID = GUIUtility.GetControlID(FocusType.Passive) - 1;
ReplaceObjectPickerForControl(attTypes, objectPickerID);
if (Event.current.commandName == "ObjectSelectorUpdated" &&
EditorGUIUtility.GetObjectPickerControlID() == _filteredObjectPickerID)
{
candidateObject = EditorGUIUtility.GetObjectPickerObject();
}
if (Event.current.type == EventType.Repaint)
{
if (temporaryGameObject != null)
GameObject.DestroyImmediate(temporaryGameObject);
else
oldObject.name = oldObjectName;
}
UnityEngine.Object matchingObject = null;
if (candidateObject != null)
{
// Make sure the assigned object it is the interface we are looking for.
if (IsAssignableFromTypes(candidateObject.GetType(), attTypes))
{
matchingObject = candidateObject;
}
else if (candidateObject is GameObject gameObject)
{
// If assigned component is a GameObject, find all matching components
// on it and if there are multiple, open the picker window.
List<MonoBehaviour> monos = new List<MonoBehaviour>();
monos.AddRange(gameObject.GetComponents<MonoBehaviour>().
Where((mono) => IsAssignableFromTypes(mono.GetType(), attTypes)));
if (monos.Count > 1)
{
EditorApplication.delayCall += () => InterfacePicker.Show(property, monos);
}
else
{
matchingObject = monos.Count == 1 ? monos[0] : null;
}
}
}
if (candidateObject == null || matchingObject != null)
{
property.objectReferenceValue = matchingObject;
}
EditorGUI.EndProperty();
}
private bool IsAssignableFromTypes(Type source, Type[] targets)
{
foreach (Type t in targets)
{
if (!IsAssignableTo(source, t))
{
return false;
}
}
return true;
}
private static string GetTypesName(Type[] attTypes)
{
if (attTypes.Length == 1)
{
return GetTypeName(attTypes[0]);
}
string typesString = "";
for (int i = 0; i < attTypes.Length; i++)
{
if (i > 0)
{
typesString += ", ";
}
typesString += GetTypeName(attTypes[i]);
}
return typesString;
}
private static string GetTypeName(Type attType)
{
if (!attType.IsGenericType)
{
return attType.Name;
}
var genericTypeNames = attType.GenericTypeArguments.Select(GetTypeName);
return $"{attType.Name}<{string.Join(", ", genericTypeNames)}>";
}
private Type[] GetInterfaceTypes(SerializedProperty property)
{
InterfaceAttribute att = (InterfaceAttribute)attribute;
Type[] t = att.Types;
if (!String.IsNullOrEmpty(att.TypeFromFieldName))
{
var thisType = property.serializedObject.targetObject.GetType();
while (thisType != null)
{
var referredFieldInfo = thisType.GetField(att.TypeFromFieldName,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (referredFieldInfo != null)
{
t = new Type[1] { referredFieldInfo.FieldType };
break;
}
var referredPropertyInfo = thisType.GetProperty(att.TypeFromFieldName,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (referredPropertyInfo != null)
{
t = new Type[1] { referredPropertyInfo.PropertyType };
break;
}
thisType = thisType.BaseType;
}
}
return t ?? _singleMonoBehaviourType;
}
void ReplaceObjectPickerForControl(Type[] attTypes, int replacePickerID)
{
var currentObjectPickerID = EditorGUIUtility.GetObjectPickerControlID();
if (currentObjectPickerID != replacePickerID)
{
return;
}
var derivedTypes = TypeCache.GetTypesDerivedFrom(attTypes[0]);
HashSet<Type> validTypes = new HashSet<Type>(derivedTypes);
for (int i = 1; i < attTypes.Length; i++)
{
var derivedTypesIntersect = TypeCache.GetTypesDerivedFrom(attTypes[i]);
validTypes.IntersectWith(derivedTypesIntersect);
}
//start filter with a long empty area to allow for easy clicking and typing
var filterBuilder = new System.Text.StringBuilder(" ");
foreach (Type type in validTypes)
{
if (type.IsGenericType)
{
continue;
}
filterBuilder.Append("t:" + type.FullName + " ");
}
string filter = filterBuilder.ToString();
EditorGUIUtility.ShowObjectPicker<Component>(null, true, filter, _filteredObjectPickerID);
}
private bool IsAssignableTo(Type fromType, Type toType)
{
// is open interface
if (toType.IsGenericType && toType.IsGenericTypeDefinition)
{
return IsAssignableToGenericType(fromType, toType);
}
return toType.IsAssignableFrom(fromType);
}
private bool IsAssignableToGenericType(Type fromType, Type toType)
{
var interfaceTypes = fromType.GetInterfaces();
foreach (var it in interfaceTypes)
{
if (it.IsGenericType && it.GetGenericTypeDefinition() == toType)
{
return true;
}
}
if (fromType.IsGenericType && fromType.GetGenericTypeDefinition() == toType)
{
return true;
}
Type baseType = fromType.BaseType;
if (baseType == null)
{
return false;
}
return IsAssignableToGenericType(baseType, toType);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b2fd211401b34f246897eb204ff3ca43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,160 @@
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Your use of this SDK or tool is subject to the Oculus SDK License Agreement, available at
https://developer.oculus.com/licenses/oculussdk/
Unless required by applicable law or agreed to in writing, the Utilities 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.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Oculus.Interaction.InterfaceSupport
{
public class InterfacePicker : EditorWindow
{
private class MonoInspector
{
public readonly MonoBehaviour Mono;
public readonly Editor Editor;
public MonoInspector(MonoBehaviour mono)
{
Mono = mono;
Editor = Editor.CreateEditor(mono);
}
public void Destroy()
{
DestroyImmediate(Editor);
}
}
private static class GUIStyles
{
public static readonly GUIStyle Default;
public static readonly GUIStyle Window;
public static readonly GUIStyle Inspector;
private static readonly RectOffset padding =
new RectOffset(EDGE_PADDING_PX,
EDGE_PADDING_PX,
EDGE_PADDING_PX,
EDGE_PADDING_PX);
static GUIStyles()
{
Default = new GUIStyle();
Window = new GUIStyle(Default);
Window.padding = padding;
Inspector = new GUIStyle(GUI.skin.window);
Inspector.padding = padding;
}
}
private const float SELECT_BUTTON_HEIGHT_PX = 32f;
private const float LABEL_COLUMN_RATIO = 0.4f;
private const int EDGE_PADDING_PX = 8;
public static bool AnyOpen => HasOpenInstances<InterfacePicker>();
private Object _target;
private string _propertyPath;
private List<MonoInspector> _monoInspectors;
private Vector2 _scrollPos = Vector2.zero;
public static void Show(SerializedProperty prop, List<MonoBehaviour> monos)
{
if (monos == null ||
monos.Count == 0 ||
prop == null)
{
return;
}
InterfacePicker picker = GetWindow<InterfacePicker>(true);
picker._propertyPath = prop.propertyPath;
picker._target = prop.serializedObject.targetObject;
picker._monoInspectors?.ForEach((mi) => mi.Destroy());
picker._monoInspectors = new List<MonoInspector>();
picker.titleContent = new GUIContent(monos[0].gameObject.name);
monos.ForEach((m) => picker._monoInspectors.Add(new MonoInspector(m)));
picker.ShowUtility();
}
private void OnGUI()
{
if (_target == null)
{
Close();
return;
}
Prune();
DrawAll();
}
private void OnDestroy()
{
_monoInspectors.ForEach((mi) => mi.Destroy());
}
private void Prune()
{
_monoInspectors.FindAll((m) => m.Mono == null).ForEach((mi) =>
{
_monoInspectors.Remove(mi);
mi.Destroy();
});
}
private void DrawAll()
{
_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos, GUIStyles.Window);
foreach (var monoInspector in _monoInspectors)
{
EditorGUILayout.Separator();
EditorGUILayout.BeginVertical(GUIStyles.Inspector);
DrawHeader(monoInspector);
EditorGUILayout.Separator();
DrawComponent(monoInspector);
EditorGUILayout.EndVertical();
}
GUILayout.FlexibleSpace();
EditorGUILayout.EndScrollView();
}
private void DrawHeader(MonoInspector monoInspector)
{
if (GUILayout.Button($"{monoInspector.Mono.GetType().Name}",
GUILayout.Height(SELECT_BUTTON_HEIGHT_PX)))
{
Apply(monoInspector.Mono);
Close();
}
}
private void DrawComponent(MonoInspector monoInspector)
{
GUI.enabled = false;
EditorGUIUtility.labelWidth = position.width * LABEL_COLUMN_RATIO;
monoInspector.Editor.OnInspectorGUI();
GUI.enabled = true;
}
private void Apply(MonoBehaviour mono)
{
SerializedProperty property =
new SerializedObject(_target).FindProperty(_propertyPath);
property.objectReferenceValue = mono;
property.serializedObject.ApplyModifiedProperties();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 13ee802452fb40949acff6a643c6d06a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,17 @@
{
"name": "Oculus.Interaction.InterfaceSupport",
"references": [
"GUID:2a230cb87a1d3ba4a98bdc0ddae76e6c"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 62f0806efa855234fa40d98ad397e1ca
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: