Advertisement
Guest User

Untitled

a guest
Dec 12th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using Assets.Scripts.Attributes;
  2. using Assets.Scripts.Util.PropertyAttributes;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. namespace Assets.Scripts.Editor.Drawers
  7. {
  8.     /// <summary>
  9.     ///     Represents property drawer for SpriteAttribute
  10.     /// </summary>
  11.     [CustomPropertyDrawer(typeof(SpriteAttribute)), UsedImplicitly]
  12.     public class SpriteDrawer : PropertyDrawer
  13.     {
  14.         private const int PROPERTY_HEIGHT = 16;
  15.         private const int PREFIX_BUTTON_WIDTH = 75;
  16.         private const int BUTTON_WIDTH = 45;
  17.         private const int SPACING = 2;
  18.  
  19.         /// <summary>
  20.         ///     Gets the height of the property.
  21.         /// </summary>
  22.         /// <returns>The property height.</returns>
  23.         /// <param name="property">Property.</param>
  24.         /// <param name="label">Label.</param>
  25.         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  26.         {
  27.             int lines = 3;
  28.             return (lines * PROPERTY_HEIGHT) + (lines * SPACING);
  29.         }
  30.  
  31.         /// <summary>
  32.         ///     Override this method to make your own GUI for the property
  33.         /// </summary>
  34.         /// <param name="position">Position</param>
  35.         /// <param name="prop">Property</param>
  36.         /// <param name="label">Label</param>
  37.         public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
  38.         {
  39.             SerializedObject serializedObject = prop.serializedObject;
  40.  
  41.             position.height = PROPERTY_HEIGHT;
  42.  
  43.             // Draw the label
  44.             label = EditorGUI.BeginProperty(position, label, prop);
  45.             EditorGUI.PrefixLabel(position, label);
  46.  
  47.             EditorGUI.indentLevel++;
  48.             position = EditorGUI.IndentedRect(position);
  49.  
  50.             // Draw the atlas
  51.             position.y += position.height + SPACING;
  52.  
  53.             SerializedProperty atlas = prop.FindPropertyRelative("m_Atlas");
  54.  
  55.             if (GUI.Button(new Rect(position.x, position.y,
  56.                                     PREFIX_BUTTON_WIDTH, position.height), "Atlas"))
  57.             {
  58.                 ComponentSelector.Show<UIAtlas>((Object atlasObj) => OnSelectAtlas(serializedObject,
  59.                                                                                    atlas,
  60.                                                                                    atlasObj));
  61.             }
  62.  
  63.             EditorGUI.PropertyField(new Rect(position.x + BUTTON_WIDTH + SPACING,
  64.                                              position.y,
  65.                                              position.width - (BUTTON_WIDTH + SPACING) * 2,
  66.                                              position.height), atlas, GUIContent.none);
  67.  
  68.             if (GUI.Button(new Rect((position.x + position.width) - BUTTON_WIDTH,
  69.                                     position.y, BUTTON_WIDTH, position.height), "Edit"))
  70.             {
  71.                 if (atlas != null)
  72.                 {
  73.                     UIAtlas atl = atlas.objectReferenceValue as UIAtlas;
  74.                     NGUISettings.atlas = atl;
  75.                     NGUIEditorTools.Select(atl.gameObject);
  76.                 }
  77.             }
  78.  
  79.             // Draw the sprite
  80.             position.y += position.height + SPACING;
  81.  
  82.             SerializedProperty sp = prop.FindPropertyRelative("m_SpriteName");
  83.  
  84.             if (GUI.Button(new Rect(position.x, position.y,
  85.                                     PREFIX_BUTTON_WIDTH, position.height), "Sprite"))
  86.             {
  87.                 NGUISettings.atlas = atlas.objectReferenceValue as UIAtlas;
  88.                 NGUISettings.selectedSprite = sp.stringValue;
  89.                 SpriteSelector.Show((string spriteName) => SelectSprite(serializedObject,
  90.                                                                         sp, spriteName));
  91.             }
  92.  
  93.             EditorGUI.LabelField(new Rect(position.x + BUTTON_WIDTH + SPACING,
  94.                                           position.y,
  95.                                           position.width - (BUTTON_WIDTH + SPACING) * 2,
  96.                                           position.height), GUIContent.none, new GUIContent(sp.stringValue));
  97.            
  98.             if (GUI.Button(new Rect((position.x + position.width) - BUTTON_WIDTH,
  99.                                     position.y, BUTTON_WIDTH, position.height), "Edit"))
  100.             {
  101.                 if (atlas != null)
  102.                 {
  103.                     UIAtlas atl = atlas.objectReferenceValue as UIAtlas;
  104.                     NGUISettings.atlas = atl;
  105.                     NGUIEditorTools.Select(atl.gameObject);
  106.                 }
  107.             }
  108.  
  109.             EditorGUI.indentLevel--;
  110.  
  111.             EditorGUI.EndProperty();
  112.         }
  113.  
  114.         /// <summary>
  115.         ///     Atlas selection callback.
  116.         /// </summary>
  117.         private void OnSelectAtlas(SerializedObject serializedObject, SerializedProperty serializedProperty,
  118.                                    Object obj)
  119.         {
  120.             serializedObject.Update();
  121.             serializedProperty.objectReferenceValue = obj;
  122.             serializedObject.ApplyModifiedProperties();
  123.             NGUISettings.atlas = obj as UIAtlas;
  124.         }
  125.        
  126.         /// <summary>
  127.         ///     Sprite selection callback function.
  128.         /// </summary>
  129.         private void SelectSprite(SerializedObject serializedObject, SerializedProperty serializedProperty,
  130.                                   string spriteName)
  131.         {
  132.             serializedObject.Update();
  133.             serializedProperty.stringValue = spriteName;
  134.             serializedObject.ApplyModifiedProperties();
  135.             NGUISettings.selectedSprite = spriteName;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement