Tanoda
InteractionButtonEditor.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEditor;
10using UnityEngine;
11
12namespace Leap.Unity.Interaction {
13
14 [CanEditMultipleObjects]
15 [CustomEditor(typeof(InteractionButton), editorForChildClasses: true)]
17
18 protected override void OnEnable() {
19 base.OnEnable();
20
21 specifyConditionalDrawing(() => false, "graspedMovementType");
22 }
23
24 public override void OnInspectorGUI() {
26
27 bool nonzeroRotation = button.transform.localRotation != Quaternion.identity;
28 bool isRoot = button.transform == button.transform.root;
29 var isPrefabAsset = Utils.IsObjectPartOfPrefabAsset(button.gameObject);
30
31 EditorGUILayout.BeginHorizontal();
32 if ((nonzeroRotation || isRoot) && !isPrefabAsset) {
33 if (isRoot) {
34 EditorGUILayout.HelpBox("This button has no parent! Buttons do not work without a parent transform.", MessageType.Warning);
35 } else if (nonzeroRotation) {
36 EditorGUILayout.HelpBox("It looks like this button's local rotation is non-zero; would you like to add a parent transform so it depresses along its z-axis?", MessageType.Warning);
37 }
38
39 if (GUILayout.Button("Add Button\nParent Transform")) {
40 GameObject buttonBaseTransform = new GameObject(button.gameObject.name + " Base");
41 Undo.RegisterCreatedObjectUndo(buttonBaseTransform, "Created Button Base for "+ button.gameObject.name);
42 Undo.SetTransformParent(buttonBaseTransform.transform, button.transform.parent, "Child "+ button.gameObject.name+ "'s Base to " + button.gameObject.name + "'s Parent");
43
44 Undo.RecordObject(buttonBaseTransform, "Set "+target.gameObject.name+"'s Base's Transform's Properties");
45 buttonBaseTransform.transform.localPosition = button.transform.localPosition;
46 buttonBaseTransform.transform.localRotation = button.transform.localRotation;
47 buttonBaseTransform.transform.localScale = button.transform.localScale;
48
49 Undo.SetTransformParent(button.transform, buttonBaseTransform.transform, "Child " + button.gameObject.name + " to its Base");
50 }
51 }
52
53 EditorGUILayout.EndHorizontal();
54
55 EditorGUILayout.BeginHorizontal();
56
57 if (!isRoot) {
58 bool isUniform = (button.transform.parent.lossyScale.x.NearlyEquals(button.transform.parent.lossyScale.y) &&
59 button.transform.parent.lossyScale.y.NearlyEquals(button.transform.parent.lossyScale.z) &&
60 button.transform.parent.lossyScale.x.NearlyEquals(button.transform.parent.lossyScale.z));
61 if (!isUniform) {
62 EditorGUILayout.HelpBox("This button exists within a non-uniformly scaled space! Please check the parent transforms for non-uniform scale...", MessageType.Warning);
63 }
64 }
65
66 EditorGUILayout.EndHorizontal();
67
68 Rigidbody currentBody = button.GetComponent<Rigidbody>();
69 RigidbodyConstraints constraints = currentBody.constraints;
70
71 EditorGUILayout.BeginHorizontal();
72 if (constraints != RigidbodyConstraints.FreezeRotation) {
73 EditorGUILayout.HelpBox("It looks like this button can freely rotate around one or more axes; would you like to constrain its rotation?", MessageType.Warning);
74 if (GUILayout.Button("Freeze\nRotation")) {
75 Undo.RecordObject(currentBody, "Set " + target.gameObject.name + "'s Rigidbody's Rotation Constraints to be frozen");
76 currentBody.constraints = RigidbodyConstraints.FreezeRotation;
77 }
78 }
79 EditorGUILayout.EndHorizontal();
80
81 base.OnInspectorGUI();
82 }
83 }
84}
void specifyConditionalDrawing(string conditionalName, params string[] dependantProperties)
Specify a list of properties that should only be displayed if the conditional property has a value of...
A physics-enabled button. Activated by physically pressing the button, with events for press and unpr...