Skip to content

Commit d5e987c

Browse files
committed
Added some useful attributes from Longbow.
1 parent 7156ecf commit d5e987c

12 files changed

+353
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using UnityEngine;
4+
using System;
5+
6+
namespace Microsoft.MixedReality.Toolkit
7+
{
8+
/// <summary>
9+
/// A PropertyAttribute for showing a collapsable Help section.
10+
/// </summary>
11+
/// <seealso cref="UnityEngine.PropertyAttribute" />
12+
[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property, AllowMultiple = false)]
13+
public class HelpAttribute : PropertyAttribute
14+
{
15+
/// <summary>
16+
/// The help text
17+
/// </summary>
18+
public string Text;
19+
20+
/// <summary>
21+
/// The help header foldout text
22+
/// </summary>
23+
public string Header;
24+
25+
/// <summary>
26+
/// Constructor
27+
/// </summary>
28+
/// <param name="helpText">The help text to display</param>
29+
/// <param name="helpHeader">The help header foldout text</param>
30+
public HelpAttribute(string helpText, string helpHeader="Help")
31+
{
32+
Text = helpText;
33+
Header = helpHeader;
34+
}
35+
}
36+
}

Assets/MixedRealityToolkit/Attributes/HelpAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using System;
4+
using UnityEngine;
5+
6+
namespace Microsoft.MixedReality.Toolkit
7+
{
8+
/// <summary>
9+
/// Attribute to mark up an int field to be drawn using the
10+
/// ScenePickPropertyDrawer
11+
/// This allows the UI to display a dropdown instead of a
12+
/// numeric entry field.
13+
/// </summary>
14+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
15+
public class ScenePickAttribute : PropertyAttribute
16+
{
17+
// Nothing to see Here, This only acts as a marker to help the editor.
18+
}
19+
}

Assets/MixedRealityToolkit/Attributes/ScenePickAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using UnityEngine;
4+
using System;
5+
6+
namespace Microsoft.MixedReality.Toolkit
7+
{
8+
/// <summary>
9+
/// A PropertyAttribute for Unity tags (a string field).
10+
/// </summary>
11+
/// <seealso cref="UnityEngine.PropertyAttribute" />
12+
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
13+
public class TagPropertyAttribute : PropertyAttribute
14+
{
15+
// Do nothing
16+
}
17+
}

Assets/MixedRealityToolkit/Attributes/TagPropertyAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace Microsoft.MixedReality.Toolkit.Editor
7+
{
8+
/// <summary>
9+
/// Custom property drawer to show a foldout help section in the Inspector
10+
/// </summary>
11+
/// <example>
12+
/// <code>
13+
/// [Help("This is a multiline collapsable help section.\n • Great for providing simple instructions in Inspector.\n • Easy to use.\n • Saves space.")]
14+
/// </code>
15+
/// </example>
16+
[CustomPropertyDrawer(typeof(HelpAttribute))]
17+
public class HelpDrawer : DecoratorDrawer
18+
{
19+
/// <summary>
20+
/// Unity calls this function to draw the GUI
21+
/// </summary>
22+
/// <param name="position">Rectangle to display the GUI in</param>
23+
public override void OnGUI(Rect position)
24+
{
25+
HelpAttribute help = attribute as HelpAttribute;
26+
27+
HelpFoldOut = EditorGUI.Foldout(position, HelpFoldOut, help.Header);
28+
if (HelpFoldOut)
29+
{
30+
EditorGUI.HelpBox(position, help.Text, MessageType.Info);
31+
}
32+
}
33+
34+
/// <summary>
35+
/// Gets the height of the decorator
36+
/// </summary>
37+
/// <returns></returns>
38+
public override float GetHeight()
39+
{
40+
HelpAttribute help = attribute as HelpAttribute;
41+
42+
GUIStyle helpStyle = EditorStyles.helpBox;
43+
Vector2 size = helpStyle.CalcSize(new GUIContent(help.Text));
44+
float lines = size.y / helpStyle.lineHeight;
45+
return helpStyle.margin.top + helpStyle.margin.bottom + helpStyle.lineHeight * (HelpFoldOut ? lines : 1.0f);
46+
}
47+
48+
#region Private
49+
50+
/// <summary>
51+
/// The "help" foldout state
52+
/// </summary>
53+
private bool HelpFoldOut = false;
54+
55+
#endregion
56+
}
57+
}

Assets/MixedRealityToolkit/Inspectors/PropertyDrawers/HelpDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace Microsoft.MixedReality.Toolkit.Editor
7+
{
8+
/// <summary>
9+
/// Creates a custom picker based on the list of scene in the build settings.
10+
/// </summary>
11+
/// <example>
12+
/// <code>
13+
/// [ScenePick]
14+
/// public int SceneId;
15+
/// </code>
16+
/// </example>
17+
[CustomPropertyDrawer(typeof(ScenePickAttribute))]
18+
public class ScenePickPropertyDrawer : PropertyDrawer
19+
{
20+
/// <summary>
21+
/// List of Options extracted from the Editor
22+
/// </summary>
23+
private static GUIContent[] Options;
24+
25+
/// <summary>
26+
/// List of Scene GUIDS for the scenes
27+
/// </summary>
28+
private static string[] PropertyData;
29+
30+
/// <summary>
31+
/// Select this option to remove the event string
32+
/// </summary>
33+
private static readonly string UnselectedText = "-- None --";
34+
35+
/// <summary>
36+
/// Text to display when an entry is missing
37+
/// </summary>
38+
private static readonly string MissingText = "-- Missing --";
39+
40+
/// <summary>
41+
/// Function called by unity to draw the GUI for this property
42+
/// We are replacing the int value of the backing field with a dropdown list of scene names
43+
/// </summary>
44+
/// <param name="position">See base class</param>
45+
/// <param name="property">See base class</param>
46+
/// <param name="label">See base class</param>
47+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
48+
{
49+
BuildOptions();
50+
51+
var currentGuid = property.stringValue.Split(';')[0];
52+
53+
var currentId = System.Array.FindIndex(PropertyData, (x) => x.Contains(currentGuid));
54+
55+
if (currentId == -1)
56+
{
57+
// Not found, display the missing text
58+
currentId = Options.Length - 1;
59+
}
60+
else if (currentId > 0 && property.stringValue != PropertyData[currentId])
61+
{
62+
// If the string has changed, update the property.
63+
// This will happen if the scene is renamed.
64+
property.stringValue = PropertyData[currentId];
65+
EditorUtility.SetDirty(property.serializedObject.targetObject);
66+
}
67+
68+
EditorGUI.BeginProperty(position, new GUIContent(property.name), property);
69+
var newId = EditorGUI.Popup(position, label, currentId, Options);
70+
71+
if (newId != currentId)
72+
{
73+
property.stringValue = PropertyData[newId];
74+
EditorUtility.SetDirty(property.serializedObject.targetObject);
75+
}
76+
77+
EditorGUI.EndProperty();
78+
79+
}
80+
81+
/// <summary>
82+
/// Build the list of scene names
83+
/// Note: Scene 0 is the no-scene option.
84+
/// </summary>
85+
private static void BuildOptions()
86+
{
87+
var scenes = EditorBuildSettings.scenes;
88+
89+
if (scenes.Length > 0)
90+
{
91+
Options = new GUIContent[scenes.Length + 2];
92+
PropertyData = new string[scenes.Length + 2];
93+
94+
Options[0] = new GUIContent(UnselectedText);
95+
PropertyData[0] = string.Empty;
96+
97+
for (int i = 0; i < scenes.Length; i++)
98+
{
99+
// Right, replace '/' with '\' otherwise the list displays like a menu where '/' denotes a sub-menu.
100+
Options[i + 1] = new GUIContent(scenes[i].path.Replace("/", "\\"));
101+
PropertyData[i + 1] = scenes[i].guid.ToString() + ";" + scenes[i].path;
102+
}
103+
104+
Options[scenes.Length + 1] = new GUIContent(MissingText);
105+
PropertyData[scenes.Length + 1] = MissingText;
106+
}
107+
else
108+
{
109+
Options = new GUIContent[2];
110+
PropertyData = new string[2];
111+
112+
Options[0] = new GUIContent(UnselectedText);
113+
PropertyData[0] = string.Empty;
114+
Options[1] = new GUIContent(MissingText);
115+
PropertyData[1] = MissingText;
116+
}
117+
}
118+
}
119+
}

Assets/MixedRealityToolkit/Inspectors/PropertyDrawers/ScenePickPropertyDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)