Advertisement
Guest User

Untitled

a guest
Aug 8th, 2015
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7.  
  8. namespace VolumetricLines.Utils
  9. {
  10.     // All credits go to Mift and Venryx
  11.     // http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_SetOnlyWhenChanged
  12.     //
  13.     public static class ExposeProperties
  14.     {
  15.         public static void Expose(PropertyField[] properties)
  16.         {
  17.             var emptyOptions = new GUILayoutOption[0];
  18.             EditorGUILayout.BeginVertical(emptyOptions);
  19.             foreach (PropertyField field in properties)
  20.             {
  21.                 EditorGUILayout.BeginHorizontal(emptyOptions);
  22.                 if (field.Type == SerializedPropertyType.Integer)
  23.                 {
  24.                     var oldValue = (int)field.GetValue();
  25.                     var newValue = EditorGUILayout.IntField(field.Name, oldValue, emptyOptions);
  26.                     if (oldValue != newValue)
  27.                         field.SetValue(newValue);
  28.                 }
  29.                 else if (field.Type == SerializedPropertyType.Float)
  30.                 {
  31.                     var oldValue = (float)field.GetValue();
  32.                     var newValue = EditorGUILayout.FloatField(field.Name, oldValue, emptyOptions);
  33.                     if (oldValue != newValue)
  34.                         field.SetValue(newValue);
  35.                 }
  36.                 else if (field.Type == SerializedPropertyType.Boolean)
  37.                 {
  38.                     var oldValue = (bool)field.GetValue();
  39.                     var newValue = EditorGUILayout.Toggle(field.Name, oldValue, emptyOptions);
  40.                     if (oldValue != newValue)
  41.                         field.SetValue(newValue);
  42.                 }
  43.                 else if (field.Type == SerializedPropertyType.String)
  44.                 {
  45.                     var oldValue = (string)field.GetValue();
  46.                     var newValue = EditorGUILayout.TextField(field.Name, oldValue, emptyOptions);
  47.                     if (oldValue != newValue)
  48.                         field.SetValue(newValue);
  49.                 }
  50.                 else if (field.Type == SerializedPropertyType.Vector2)
  51.                 {
  52.                     var oldValue = (Vector2)field.GetValue();
  53.                     var newValue = EditorGUILayout.Vector2Field(field.Name, oldValue, emptyOptions);
  54.                     if (oldValue != newValue)
  55.                         field.SetValue(newValue);
  56.                 }
  57.                 else if (field.Type == SerializedPropertyType.Vector3)
  58.                 {
  59.                     var oldValue = (Vector3)field.GetValue();
  60.                     var newValue = EditorGUILayout.Vector3Field(field.Name, oldValue, emptyOptions);
  61.                     if (oldValue != newValue)
  62.                         field.SetValue(newValue);
  63.                 }
  64.                 else if (field.Type == SerializedPropertyType.Enum)
  65.                 {
  66.                     var oldValue = (Enum)field.GetValue();
  67.                     var newValue = EditorGUILayout.EnumPopup(field.Name, oldValue, emptyOptions);
  68.                     if (oldValue != newValue)
  69.                         field.SetValue(newValue);
  70.                 }
  71.                 else if (field.Type == SerializedPropertyType.Color)
  72.                 {
  73.                     var oldValue = (Color)field.GetValue();
  74.                     var newValue = EditorGUILayout.ColorField(field.Name, oldValue, emptyOptions);
  75.                     if (oldValue != newValue)
  76.                         field.SetValue(newValue);
  77.                 }
  78.                 EditorGUILayout.EndHorizontal();
  79.             }
  80.             EditorGUILayout.EndVertical();
  81.         }
  82.        
  83.         public static PropertyField[] GetProperties(object obj)
  84.         {
  85.             var fields = new List<PropertyField>();
  86.            
  87.             PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  88.            
  89.             foreach (PropertyInfo info in infos)
  90.             {
  91.                 if (!(info.CanRead && info.CanWrite))
  92.                     continue;
  93.                
  94.                 object[] attributes = info.GetCustomAttributes(true);
  95.                
  96.                 bool isExposed = false;
  97.                 foreach (object o in attributes)
  98.                     if (o.GetType() == typeof(ExposePropertyAttribute))
  99.                 {
  100.                     isExposed = true;
  101.                     break;
  102.                 }
  103.                 if (!isExposed)
  104.                     continue;
  105.                
  106.                 var type = SerializedPropertyType.Integer;
  107.                 if (PropertyField.GetPropertyType(info, out type))
  108.                 {
  109.                     var field = new PropertyField(obj, info, type);
  110.                     fields.Add(field);
  111.                 }
  112.             }
  113.            
  114.             return fields.ToArray();
  115.         }
  116.     }
  117.    
  118.     // All credits go to Mift and Venryx
  119.     // http://wiki.unity3d.com/index.php/ExposePropertiesInInspector_SetOnlyWhenChanged
  120.     //
  121.     public class PropertyField
  122.     {
  123.         object obj;
  124.         PropertyInfo info;
  125.         SerializedPropertyType type;
  126.        
  127.         MethodInfo getter;
  128.         MethodInfo setter;
  129.        
  130.         public SerializedPropertyType Type
  131.         {
  132.             get { return type; }
  133.         }
  134.        
  135.         public String Name
  136.         {
  137.             get { return ObjectNames.NicifyVariableName(info.Name); }
  138.         }
  139.        
  140.         public PropertyField(object obj, PropertyInfo info, SerializedPropertyType type)
  141.         {
  142.             this.obj = obj;
  143.             this.info = info;
  144.             this.type = type;
  145.            
  146.             getter = this.info.GetGetMethod();
  147.             setter = this.info.GetSetMethod();
  148.         }
  149.        
  150.         public object GetValue() { return getter.Invoke(obj, null); }
  151.         public void SetValue(object value) { setter.Invoke(obj, new[] {value}); }
  152.        
  153.         public static bool GetPropertyType(PropertyInfo info, out SerializedPropertyType propertyType)
  154.         {
  155.             Type type = info.PropertyType;
  156.             propertyType = SerializedPropertyType.Generic;
  157.             if (type == typeof(int))
  158.                 propertyType = SerializedPropertyType.Integer;
  159.             else if (type == typeof(float))
  160.                 propertyType = SerializedPropertyType.Float;
  161.             else if (type == typeof(bool))
  162.                 propertyType = SerializedPropertyType.Boolean;
  163.             else if (type == typeof(string))
  164.                 propertyType = SerializedPropertyType.String;
  165.             else if (type == typeof(Vector2))
  166.                 propertyType = SerializedPropertyType.Vector2;
  167.             else if (type == typeof(Vector3))
  168.                 propertyType = SerializedPropertyType.Vector3;
  169.             else if (type.IsEnum)
  170.                 propertyType = SerializedPropertyType.Enum;
  171.             else if (type == typeof(Color))
  172.                 propertyType = SerializedPropertyType.Color;
  173.             return propertyType != SerializedPropertyType.Generic;
  174.         }
  175.     }
  176. }
Advertisement
RAW Paste Data Copied
Advertisement