Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Assets.Scripts.Attributes;
- using Assets.Scripts.Util.PropertyAttributes;
- using UnityEditor;
- using UnityEngine;
- namespace Assets.Scripts.Editor.Drawers
- {
- /// <summary>
- /// Represents property drawer for SpriteAttribute
- /// </summary>
- [CustomPropertyDrawer(typeof(SpriteAttribute)), UsedImplicitly]
- public class SpriteDrawer : PropertyDrawer
- {
- private const int PROPERTY_HEIGHT = 16;
- private const int PREFIX_BUTTON_WIDTH = 75;
- private const int BUTTON_WIDTH = 45;
- private const int SPACING = 2;
- /// <summary>
- /// Gets the height of the property.
- /// </summary>
- /// <returns>The property height.</returns>
- /// <param name="property">Property.</param>
- /// <param name="label">Label.</param>
- public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
- {
- int lines = 3;
- return (lines * PROPERTY_HEIGHT) + (lines * SPACING);
- }
- /// <summary>
- /// Override this method to make your own GUI for the property
- /// </summary>
- /// <param name="position">Position</param>
- /// <param name="prop">Property</param>
- /// <param name="label">Label</param>
- public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
- {
- SerializedObject serializedObject = prop.serializedObject;
- position.height = PROPERTY_HEIGHT;
- // Draw the label
- label = EditorGUI.BeginProperty(position, label, prop);
- EditorGUI.PrefixLabel(position, label);
- EditorGUI.indentLevel++;
- position = EditorGUI.IndentedRect(position);
- // Draw the atlas
- position.y += position.height + SPACING;
- SerializedProperty atlas = prop.FindPropertyRelative("m_Atlas");
- if (GUI.Button(new Rect(position.x, position.y,
- PREFIX_BUTTON_WIDTH, position.height), "Atlas"))
- {
- ComponentSelector.Show<UIAtlas>((Object atlasObj) => OnSelectAtlas(serializedObject,
- atlas,
- atlasObj));
- }
- EditorGUI.PropertyField(new Rect(position.x + BUTTON_WIDTH + SPACING,
- position.y,
- position.width - (BUTTON_WIDTH + SPACING) * 2,
- position.height), atlas, GUIContent.none);
- if (GUI.Button(new Rect((position.x + position.width) - BUTTON_WIDTH,
- position.y, BUTTON_WIDTH, position.height), "Edit"))
- {
- if (atlas != null)
- {
- UIAtlas atl = atlas.objectReferenceValue as UIAtlas;
- NGUISettings.atlas = atl;
- NGUIEditorTools.Select(atl.gameObject);
- }
- }
- // Draw the sprite
- position.y += position.height + SPACING;
- SerializedProperty sp = prop.FindPropertyRelative("m_SpriteName");
- if (GUI.Button(new Rect(position.x, position.y,
- PREFIX_BUTTON_WIDTH, position.height), "Sprite"))
- {
- NGUISettings.atlas = atlas.objectReferenceValue as UIAtlas;
- NGUISettings.selectedSprite = sp.stringValue;
- SpriteSelector.Show((string spriteName) => SelectSprite(serializedObject,
- sp, spriteName));
- }
- EditorGUI.LabelField(new Rect(position.x + BUTTON_WIDTH + SPACING,
- position.y,
- position.width - (BUTTON_WIDTH + SPACING) * 2,
- position.height), GUIContent.none, new GUIContent(sp.stringValue));
- if (GUI.Button(new Rect((position.x + position.width) - BUTTON_WIDTH,
- position.y, BUTTON_WIDTH, position.height), "Edit"))
- {
- if (atlas != null)
- {
- UIAtlas atl = atlas.objectReferenceValue as UIAtlas;
- NGUISettings.atlas = atl;
- NGUIEditorTools.Select(atl.gameObject);
- }
- }
- EditorGUI.indentLevel--;
- EditorGUI.EndProperty();
- }
- /// <summary>
- /// Atlas selection callback.
- /// </summary>
- private void OnSelectAtlas(SerializedObject serializedObject, SerializedProperty serializedProperty,
- Object obj)
- {
- serializedObject.Update();
- serializedProperty.objectReferenceValue = obj;
- serializedObject.ApplyModifiedProperties();
- NGUISettings.atlas = obj as UIAtlas;
- }
- /// <summary>
- /// Sprite selection callback function.
- /// </summary>
- private void SelectSprite(SerializedObject serializedObject, SerializedProperty serializedProperty,
- string spriteName)
- {
- serializedObject.Update();
- serializedProperty.stringValue = spriteName;
- serializedObject.ApplyModifiedProperties();
- NGUISettings.selectedSprite = spriteName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement