ImageTrackerEditor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. namespace Imagine.WebAR.Editor
  6. {
  7. [CustomEditor(typeof(ImageTracker))]
  8. public class ImageTrackerEditor : UnityEditor.Editor
  9. {
  10. private ImageTracker _target;
  11. private void OnEnable()
  12. {
  13. _target = (ImageTracker)target;
  14. }
  15. public override void OnInspectorGUI()
  16. {
  17. //base.OnInspectorGUI();
  18. EditorGUILayout.PropertyField(serializedObject.FindProperty("trackerCam"));
  19. EditorGUILayout.PropertyField(serializedObject.FindProperty("imageTargets"));
  20. EditorGUILayout.PropertyField(serializedObject.FindProperty("trackerOrigin"));
  21. EditorGUILayout.Space(20);
  22. // var overrideTrackerSettingsProp = serializedObject.FindProperty("overrideTrackerSettings");
  23. // EditorGUILayout.PropertyField(overrideTrackerSettingsProp);
  24. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  25. // var overrideTrackerSettingsPropVal = overrideTrackerSettingsProp.boolValue;
  26. // if (!overrideTrackerSettingsPropVal)
  27. // {
  28. // GUI.enabled = false;
  29. // }
  30. EditorGUI.indentLevel++;
  31. var trackerSettingsProp = serializedObject.FindProperty("trackerSettings");
  32. EditorGUILayout.PropertyField(trackerSettingsProp, true);
  33. // //smoothing
  34. // EditorGUILayout.Space(20);
  35. // var useExtraSmoothingProp = trackerSettingsProp.FindPropertyRelative("useExtraSmoothing");
  36. // EditorGUILayout.PropertyField(useExtraSmoothingProp);
  37. // if(useExtraSmoothingProp.boolValue){
  38. // EditorGUI.indentLevel++;
  39. // EditorGUILayout.PropertyField(trackerSettingsProp.FindPropertyRelative("smoothenFactor"));
  40. // EditorGUI.indentLevel--;
  41. // }
  42. EditorGUI.indentLevel--;
  43. EditorGUILayout.Space(20);
  44. //settings templates
  45. EditorGUILayout.LabelField("Auto-set from the following templates", EditorStyles.boldLabel);
  46. var templates = ImageTrackerGlobalSettings.Instance.settingsTemplates;
  47. foreach(var t in templates){
  48. GUI.color = t.color;
  49. if(GUILayout.Button(new GUIContent(t.label, t.description))){
  50. if(EditorUtility.DisplayDialog(
  51. "Confirm settings overwrite",
  52. "Are you sure you want to set your tracker settings to " + t.label + "?\n\n" +
  53. t.description + "\n\n" +
  54. "This will overwrite your current tracker settings"
  55. , "Proceed", "Cancel")){
  56. var tso = new SerializedObject(t);
  57. var tSettingsProp = tso.FindProperty("settings");//.FindPropertyRelative("advancedSettings");
  58. CopyTrackerSettings(
  59. tSettingsProp,
  60. trackerSettingsProp);
  61. EditorUtility.DisplayDialog("Copy Settings Finished", "Your tracker settings set to " + t.label, "Okay");
  62. }
  63. }
  64. }
  65. GUI.color = Color.white;
  66. GUI.enabled = true;
  67. EditorGUILayout.EndVertical();
  68. EditorGUILayout.Space(20);
  69. EditorGUILayout.PropertyField(serializedObject.FindProperty("dontDeactivateOnLost"));
  70. EditorGUILayout.Space(20);
  71. EditorGUILayout.PropertyField(serializedObject.FindProperty("OnImageFound"));
  72. EditorGUILayout.PropertyField(serializedObject.FindProperty("OnImageLost"));
  73. EditorGUILayout.Space(20);
  74. EditorGUILayout.PropertyField(serializedObject.FindProperty("startStopOnEnableDisable"));
  75. EditorGUILayout.PropertyField(serializedObject.FindProperty("stopOnDestroy"));
  76. DrawEditorDebugger();
  77. serializedObject.ApplyModifiedProperties();
  78. }
  79. bool showKeyboardCameraControls = false;
  80. void DrawEditorDebugger(){
  81. //Editor Runtime Debugger
  82. EditorGUILayout.Space();
  83. EditorGUILayout.LabelField("Editor Debug Mode");
  84. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  85. if(Application.IsPlaying(_target)){
  86. //Enable Disable
  87. var imageTargetsProp = serializedObject.FindProperty("imageTargets");
  88. var trackedIdsProp = serializedObject.FindProperty("trackedIds");
  89. var trackedIds = new List<string>();
  90. if(trackedIdsProp != null){
  91. for(var i = 0; i < trackedIdsProp.arraySize; i++){
  92. trackedIds.Add(trackedIdsProp.GetArrayElementAtIndex(i).stringValue);
  93. }
  94. }
  95. for(var i = 0; i < imageTargetsProp.arraySize; i++){
  96. EditorGUILayout.BeginHorizontal();
  97. var imageTargetProp = imageTargetsProp.GetArrayElementAtIndex(i);
  98. var id = imageTargetProp.FindPropertyRelative("id").stringValue;
  99. EditorGUILayout.LabelField(id);
  100. var imageFound = trackedIds.Contains(id);
  101. GUI.enabled = !imageFound;
  102. if(GUILayout.Button("Found")){
  103. _target.SendMessage("OnTrackingFound",id);
  104. var imageTargetTransform = ((Transform)imageTargetProp.FindPropertyRelative("transform").objectReferenceValue);
  105. var cam = ((ARCamera)serializedObject.FindProperty("trackerCam").objectReferenceValue).transform;
  106. cam.transform.position = imageTargetTransform.position + imageTargetTransform.forward * -3;
  107. cam.LookAt(imageTargetTransform);
  108. }
  109. GUI.enabled = imageFound;
  110. if(GUILayout.Button("Lost")){
  111. _target.SendMessage("OnTrackingLost",id);
  112. }
  113. GUI.enabled = true;
  114. EditorGUILayout.EndHorizontal();
  115. }
  116. }
  117. else{
  118. GUI.color = Color.yellow;
  119. EditorGUILayout.LabelField("Enter Play-mode to Debug In Editor");
  120. GUI.color = Color.white;
  121. }
  122. EditorGUILayout.Space();
  123. //keyboard camera controls
  124. showKeyboardCameraControls = EditorGUILayout.Toggle ("Show Keyboard Camera Controls", showKeyboardCameraControls);
  125. if(showKeyboardCameraControls){
  126. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  127. EditorGUILayout.LabelField("W", "Move Forward (Z)");
  128. EditorGUILayout.LabelField("S", "Move Backward (Z)");
  129. EditorGUILayout.LabelField("A", "Move Left (X)");
  130. EditorGUILayout.LabelField("D", "Move Right (X)");
  131. EditorGUILayout.LabelField("R", "Move Up (Y)");
  132. EditorGUILayout.LabelField("F", "Move Down (Y)");
  133. EditorGUILayout.Space();
  134. EditorGUILayout.LabelField("Up Arrow", "Tilt Up (along X-Axis)");
  135. EditorGUILayout.LabelField("Down Arrow", "Tilt Down (along X-Axis)");
  136. EditorGUILayout.LabelField("Left Arrow", "Tilt Left (along Y-Axis)");
  137. EditorGUILayout.LabelField("Right Arrow", "Tilt Right (Along Y-Axis)");
  138. EditorGUILayout.LabelField("Period", "Tilt Clockwise (Along Z-Axis)");
  139. EditorGUILayout.LabelField("Comma", "Tilt Counter Clockwise (Along Z-Axis)");
  140. EditorGUILayout.Space(40);
  141. EditorGUILayout.PropertyField(serializedObject.FindProperty("debugCamMoveSensitivity"));
  142. EditorGUILayout.PropertyField(serializedObject.FindProperty("debugCamTiltSensitivity"));
  143. EditorGUILayout.EndVertical();
  144. }
  145. EditorGUILayout.EndVertical();
  146. }
  147. public void CopyTrackerSettings(SerializedProperty srcProp, SerializedProperty dstProp)
  148. {
  149. SerializedProperty currentProperty = srcProp.Copy();
  150. SerializedProperty nextSiblingProperty = srcProp.Copy();
  151. {
  152. nextSiblingProperty.Next(false);
  153. }
  154. if (currentProperty.Next(true))
  155. {
  156. do
  157. {
  158. if (SerializedProperty.EqualContents(currentProperty, nextSiblingProperty))
  159. break;
  160. Debug.Log("Copying " + currentProperty.name + " (" + currentProperty.propertyType + ")");
  161. var dstChildProp = dstProp.FindPropertyRelative(currentProperty.name);
  162. if(currentProperty.hasChildren){
  163. CopyTrackerSettings(currentProperty, dstChildProp);
  164. }
  165. else{
  166. if(currentProperty.propertyType == SerializedPropertyType.Integer ||
  167. currentProperty.propertyType == SerializedPropertyType.Enum){
  168. dstChildProp.intValue = currentProperty.intValue;
  169. }
  170. else if(currentProperty.propertyType == SerializedPropertyType.Boolean){
  171. dstChildProp.boolValue = currentProperty.boolValue;
  172. }
  173. else if(currentProperty.propertyType == SerializedPropertyType.Float){
  174. dstChildProp.floatValue = currentProperty.floatValue;
  175. }
  176. else if(currentProperty.propertyType == SerializedPropertyType.String){
  177. dstChildProp.stringValue = currentProperty.stringValue;
  178. }
  179. else{
  180. Debug.LogError("Failed to copy property: " + currentProperty.name + "(" + currentProperty.propertyType + ")");
  181. }
  182. }
  183. }
  184. while (currentProperty.Next(false));
  185. }
  186. }
  187. object GetValue(SerializedProperty property)
  188. {
  189. switch (property.propertyType)
  190. {
  191. case SerializedPropertyType.Integer:
  192. return property.intValue;
  193. case SerializedPropertyType.Boolean:
  194. return property.boolValue;
  195. case SerializedPropertyType.Float:
  196. return property.floatValue;
  197. case SerializedPropertyType.String:
  198. return property.stringValue;
  199. // Add more cases as needed for other property types
  200. default:
  201. return null;
  202. }
  203. }
  204. }
  205. }