Tanoda
AttachmentHandsEditor.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 System.Collections;
10using System.Collections.Generic;
11using UnityEditor;
12using UnityEngine;
13
15
16 [CustomEditor(typeof(AttachmentHands))]
17 public class AttachmentHandsEditor : CustomEditorBase<AttachmentHands> {
18
19 private Texture _handTex;
20 private Rect _handTexRect;
21
22 protected override void OnEnable() {
23 base.OnEnable();
24
25 _handTex = EditorResources.Load<Texture2D>("HandTex");
26
27 this.specifyCustomDrawer("_attachmentPoints", drawAttachmentPointsEditor);
28 }
29
30 private void drawAttachmentPointsEditor(SerializedProperty property) {
31
32 // Set up the draw rect space based on the image and available editor space.
33
34 EditorGUILayout.Space();
35 EditorGUILayout.LabelField("Attachment Transforms", EditorStyles.boldLabel);
36
37 // Determine whether the target object is a prefab. AttachmentPoints cannot be edited on prefabs.
38 var isTargetPrefab = Utils.IsObjectPartOfPrefabAsset(target.gameObject);
39
40 if (isTargetPrefab) {
41 EditorGUILayout.HelpBox("Drag the prefab into the scene to make changes to attachment points.", MessageType.Info, true);
42 }
43
44 _handTexRect = EditorGUILayout.BeginVertical(GUILayout.MinWidth(EditorGUIUtility.currentViewWidth),
45 GUILayout.MinHeight(EditorGUIUtility.currentViewWidth * (_handTex.height / (float)_handTex.width)),
46 GUILayout.MaxWidth(_handTex.width),
47 GUILayout.MaxHeight(_handTex.height));
48
49 Rect imageContainerRect = _handTexRect; imageContainerRect.width = EditorGUIUtility.currentViewWidth - 30F;
50 EditorGUI.DrawRect(imageContainerRect, new Color(0.2F, 0.2F, 0.2F));
51 imageContainerRect.x += 1; imageContainerRect.y += 1; imageContainerRect.width -= 2; imageContainerRect.height -= 2;
52 EditorGUI.DrawRect(imageContainerRect, new Color(0.6F, 0.6F, 0.6F));
53 imageContainerRect.x += 1; imageContainerRect.y += 1; imageContainerRect.width -= 2; imageContainerRect.height -= 2;
54 EditorGUI.DrawRect(imageContainerRect, new Color(0.2F, 0.2F, 0.2F));
55
56 _handTexRect = new Rect(_handTexRect.x + (imageContainerRect.center.x - _handTexRect.center.x),
57 _handTexRect.y,
58 _handTexRect.width,
59 _handTexRect.height);
60 EditorGUI.DrawTextureTransparent(_handTexRect, _handTex);
61 EditorGUILayout.Space();
62
63
64 // Draw the toggles for the attachment points.
65
66 EditorGUI.BeginDisabledGroup(isTargetPrefab);
67
68 makeAttachmentPointsToggle("Palm", new Vector2( 0.100F, 0.160F));
69 makeAttachmentPointsToggle("Wrist", new Vector2( 0.080F, 0.430F));
70
71 makeAttachmentPointsToggle("ThumbProximalJoint", new Vector2(-0.190F, 0.260F));
72 makeAttachmentPointsToggle("ThumbDistalJoint", new Vector2(-0.310F, 0.170F));
73 makeAttachmentPointsToggle("ThumbTip", new Vector2(-0.390F, 0.110F));
74
75 makeAttachmentPointsToggle("IndexKnuckle", new Vector2(-0.040F, -0.040F));
76 makeAttachmentPointsToggle("IndexMiddleJoint", new Vector2(-0.060F, -0.170F));
77 makeAttachmentPointsToggle("IndexDistalJoint", new Vector2(-0.075F, -0.280F));
78 makeAttachmentPointsToggle("IndexTip", new Vector2(-0.080F, -0.380F));
79
80 makeAttachmentPointsToggle("MiddleKnuckle", new Vector2( 0.080F, -0.050F));
81 makeAttachmentPointsToggle("MiddleMiddleJoint", new Vector2( 0.080F, -0.190F));
82 makeAttachmentPointsToggle("MiddleDistalJoint", new Vector2( 0.080F, -0.310F));
83 makeAttachmentPointsToggle("MiddleTip", new Vector2( 0.090F, -0.420F));
84
85 makeAttachmentPointsToggle("RingKnuckle", new Vector2( 0.195F, -0.020F));
86 makeAttachmentPointsToggle("RingMiddleJoint", new Vector2( 0.220F, -0.150F));
87 makeAttachmentPointsToggle("RingDistalJoint", new Vector2( 0.230F, -0.270F));
88 makeAttachmentPointsToggle("RingTip", new Vector2( 0.245F, -0.380F));
89
90 makeAttachmentPointsToggle("PinkyKnuckle", new Vector2( 0.295F, 0.040F));
91 makeAttachmentPointsToggle("PinkyMiddleJoint", new Vector2( 0.340F, -0.050F));
92 makeAttachmentPointsToggle("PinkyDistalJoint", new Vector2( 0.380F, -0.130F));
93 makeAttachmentPointsToggle("PinkyTip", new Vector2( 0.410F, -0.210F));
94
95 EditorGUI.EndDisabledGroup();
96
97 EditorGUILayout.EndVertical();
98 }
99
100 private void makeAttachmentPointsToggle(string attachmentFlagName, Vector2 offCenterPosImgSpace) {
101 AttachmentPointFlags attachmentPoints = target.attachmentPoints;
102
103 AttachmentPointFlags flag = (AttachmentPointFlags)System.Enum.Parse(typeof(AttachmentPointFlags), attachmentFlagName, true);
104
105 if (EditorGUI.Toggle(makeToggleRect(_handTexRect.center
106 + new Vector2(offCenterPosImgSpace.x * _handTexRect.width,
107 offCenterPosImgSpace.y * _handTexRect.height)),
108 (attachmentPoints & flag) == flag)) {
109
110 target.attachmentPoints = attachmentPoints | flag; // Set flag bit to 1.
111 }
112 else {
113 if (!wouldFlagDeletionDestroyData(target, flag)
114 || EditorUtility.DisplayDialog("Delete " + flag + " Attachment Point?",
115 "Deleting the " + flag + " attachment point will destroy "
116 + "its GameObject and any of its non-Attachment-Point children, "
117 + "and will remove any components attached to it.",
118 "Delete " + flag + " Attachment Point", "Cancel")) {
119
120 target.attachmentPoints = attachmentPoints & (~flag); // Set flag bit to 0.
121 }
122 }
123 }
124
125 private const float TOGGLE_SIZE = 15.0F;
126 private Rect makeToggleRect(Vector2 centerPos) {
127 return new Rect(centerPos.x - TOGGLE_SIZE / 2F, centerPos.y - TOGGLE_SIZE / 2F, TOGGLE_SIZE, TOGGLE_SIZE);
128 }
129
130 private static bool wouldFlagDeletionDestroyData(AttachmentHands target, AttachmentPointFlags flag) {
131 if (target.attachmentHands == null) return false;
132
133 foreach (var attachmentHand in target.attachmentHands) {
134 var point = attachmentHand.GetBehaviourForPoint(flag);
135
136 if (point == null) return false;
137 else {
138 // Data will be destroyed if this AttachmentPointBehaviour's Transform contains any children
139 // that are not themselves AttachmentPointBehaviours.
140 foreach (var child in point.transform.GetChildren()) {
141 if (child.GetComponent<AttachmentPointBehaviour>() == null) return true;
142 }
143
144 // Data will be destroyed if this AttachmentPointBehaviour contains any components
145 // that aren't constructed automatically.
146 foreach (var component in point.gameObject.GetComponents<Component>()) {
147 if (component is Transform) continue;
148 if (component is AttachmentPointBehaviour) continue;
149
150 return true;
151 }
152 }
153 }
154 return false;
155 }
156
157 }
158
159}
UnityEngine.Component Component
UnityEngine.Color Color
Definition: TestScript.cs:32
Add an GameObject with this script to your scene if you would like to have a Transform hierarchy that...
void specifyCustomDrawer(string propertyName, Action< SerializedProperty > propertyDrawer)
Specify a callback to be used to draw a specific named property. Should be called in OnEnable.
AttachmentPointFlags
Flags for attachment points on the hand.