Advertisement
caLLowCreation

InputAxesPickerDrawer

Sep 12th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.48 KB | None | 0 0
  1. #region Author
  2. /*
  3.      REQUIRES: InputAxesPickerAttribute.cs at http://pastebin.com/Hg7KKXie
  4.     Watch Tutorials about InputAxes and DLL:
  5.         https://www.youtube.com/playlist?list=PLTEnuRWq5Edbna2JRXf__p1PftIFpIx-Q
  6.         https://www.youtube.com/playlist?list=PLTEnuRWq5EdZ_g3IYxduBzbL3QNj42Xc9
  7.      Jones St. Lewis Cropper (caLLow)
  8.      
  9.      Another caLLowCreation
  10.      
  11.      Visit us on Google+ and other social media outlets @caLLowCreation
  12.      
  13.      Thanks for using our product.
  14.      
  15.      Send questions/comments/concerns/requests to
  16.       e-mail: caLLowCreation@gmail.com
  17.       subject: InputAxes
  18.      
  19.     Source:
  20.     https://bitbucket.org/caLLowCreation/inputaxespopup
  21.     git@bitbucket.org:caLLowCreation/inputaxespopup.git
  22.    
  23.     MIT License (MIT)
  24. */
  25. #endregion
  26.  
  27. using InputAxes;
  28. using System.Linq;
  29. using UnityEditor;
  30. using UnityEngine;
  31.  
  32. namespace InputAxesEditor
  33. {
  34.     /// <summary>
  35.     /// Draws popup to pick an axes from the InputManager
  36.     /// </summary>
  37.     [CustomPropertyDrawer(typeof(InputAxesPickerAttribute))]
  38.     [CanEditMultipleObjects]
  39.     public sealed class InputAxesPickerDrawer : PropertyDrawer
  40.     {
  41.         const string ASSET_PATH = "ProjectSettings/InputManager.asset";
  42.         const string HELP_FORMAT = "({0}) {1} in script {2} must a string.";
  43.         const int NONE_SELECTED = -1;  //Nothing selected in the popup
  44.  
  45.         string[] m_AxesNames = null;
  46.         int m_CurrentIndex = NONE_SELECTED;
  47.         InputAxesPickerAttribute m_Attribute = null;
  48.         bool? m_IsValid = null;
  49.  
  50.         //Cache field validity
  51.         bool isValid
  52.         {
  53.             get { return m_IsValid ?? (m_IsValid = fieldInfo.FieldType.Equals(typeof(string))).Value; }
  54.         }
  55.  
  56.         //Cache attribute
  57.         InputAxesPickerAttribute attr
  58.         {
  59.             get { return m_Attribute ?? (m_Attribute = attribute as InputAxesPickerAttribute); }
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Gets height to draw property
  64.         /// </summary>
  65.         /// <param name="property">Property field to draw</param>
  66.         /// <param name="label">Label text and tooltip for property</param>
  67.         /// <returns>Height to draw property</returns>
  68.         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  69.         {
  70.             if (!isValid) return base.GetPropertyHeight(property, label) + EditorGUIUtility.singleLineHeight + 2;
  71.  
  72.             return base.GetPropertyHeight(property, label);
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Draws property to the inspector
  77.         /// </summary>
  78.         /// <param name="position">Where to draw the property</param>
  79.         /// <param name="property">Property field to draw</param>
  80.         /// <param name="label">Label text and tooltip for property</param>
  81.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  82.         {
  83.             if (!isValid)
  84.             {
  85.                 EditorGUI.HelpBox(position,
  86.                     string.Format(HELP_FORMAT, fieldInfo.FieldType.Name, fieldInfo.Name, fieldInfo.DeclaringType.Name),
  87.                     MessageType.Error);
  88.                 return;
  89.             }
  90.  
  91.             if (m_AxesNames == null)
  92.             {
  93.                 m_AxesNames = InputAxesPickerDrawer.GetInputAxesNames();
  94.                 m_CurrentIndex = m_AxesNames.ToList().FindIndex(x => x.Equals(property.stringValue));
  95.             }
  96.  
  97.             m_CurrentIndex = EditorGUI.Popup(position, attr.label ?? label.text, m_CurrentIndex, m_AxesNames);
  98.  
  99.             if (GUI.changed && m_CurrentIndex != NONE_SELECTED)
  100.             {
  101.                 property.stringValue = m_AxesNames[m_CurrentIndex];
  102.             }
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Gets an array of the Input Axis names accessible from the InputManager
  107.         /// </summary>
  108.         /// <returns>An array Axis names</returns>
  109.         public static string[] GetInputAxesNames()
  110.         {
  111.             SerializedProperty axesProperty =
  112.                 new SerializedObject(AssetDatabase.LoadAllAssetsAtPath(ASSET_PATH)[0])
  113.                 .FindProperty("m_Axes");
  114.  
  115.             var axesNames = new string[axesProperty.arraySize];
  116.             for (int i = 0; i < axesProperty.arraySize; i++)
  117.             {
  118.                 axesNames[i] = axesProperty
  119.                     .GetArrayElementAtIndex(i)
  120.                     .FindPropertyRelative("m_Name")
  121.                     .stringValue;
  122.             }
  123.  
  124.             return axesNames;
  125.         }
  126.  
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement