add
This commit is contained in:
241
Assets/CoolapeFrame/3rd/UnityEditorHelper/Editor/EditorHelper.cs
Normal file
241
Assets/CoolapeFrame/3rd/UnityEditorHelper/Editor/EditorHelper.cs
Normal file
@@ -0,0 +1,241 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEditorHelper
|
||||
{
|
||||
public static class EditorHelper
|
||||
{
|
||||
private static readonly GUIStyle SimpleRectStyle;
|
||||
|
||||
static EditorHelper()
|
||||
{
|
||||
Texture2D simpleTexture = new Texture2D(1, 1);
|
||||
simpleTexture.SetPixel(0, 0, Color.white);
|
||||
simpleTexture.Apply();
|
||||
|
||||
SimpleRectStyle = new GUIStyle { normal = { background = simpleTexture } };
|
||||
}
|
||||
|
||||
public static bool DrawIconHeader(string key, Texture icon, string caption, Color captionColor)
|
||||
{
|
||||
bool state = EditorPrefs.GetBool(key, true);
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
{
|
||||
EditorGUILayout.LabelField(new GUIContent(icon), EditorStyles.miniLabel, GUILayout.Width(22), GUILayout.Height(22));
|
||||
using (new SwitchColor(captionColor)) {
|
||||
EditorGUILayout.BeginVertical();
|
||||
GUILayout.Space(5);
|
||||
EditorGUILayout.LabelField(caption, EditorStyles.boldLabel);
|
||||
EditorGUILayout.EndVertical();
|
||||
}
|
||||
EditorGUILayout.Space();
|
||||
string cap = state ? "\u25bc" : "\u25b2";
|
||||
if (GUILayout.Button(cap, EditorStyles.label, GUILayout.Width(16), GUILayout.Height(16))) {
|
||||
state = !state;
|
||||
EditorPrefs.SetBool(key, state);
|
||||
}
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
GUILayout.Space(2);
|
||||
return state;
|
||||
}
|
||||
|
||||
public static string GetAssetPath(string assetFileName)
|
||||
{
|
||||
if (!AssetDatabase.GetAllAssetPaths().Any(p => p.EndsWith(assetFileName))) {
|
||||
AssetDatabase.Refresh();
|
||||
}
|
||||
string basePath = AssetDatabase.GetAllAssetPaths().First(p => p.EndsWith(assetFileName));
|
||||
int lastDelimiter = basePath.LastIndexOf('/') + 1;
|
||||
basePath = basePath.Remove(lastDelimiter, basePath.Length - lastDelimiter);
|
||||
return basePath;
|
||||
}
|
||||
|
||||
public static GUIStyle GetEditorStyle(string style)
|
||||
{
|
||||
return EditorGUIUtility.GetBuiltinSkin(EditorGUIUtility.isProSkin ? EditorSkin.Scene : EditorSkin.Inspector).GetStyle(style);
|
||||
}
|
||||
|
||||
public static void GUIDrawRect(Rect position, Color color)
|
||||
{
|
||||
using (new SwitchColor(color)) {
|
||||
GUI.Box(position, GUIContent.none, SimpleRectStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class HighlightBox : IDisposable
|
||||
{
|
||||
public HighlightBox() : this(new Color(0.1f, 0.1f, 0.2f))
|
||||
{
|
||||
}
|
||||
|
||||
public HighlightBox(Color color)
|
||||
{
|
||||
GUILayout.Space(8f);
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Space(4f);
|
||||
using (new SwitchColor(color)) {
|
||||
EditorGUILayout.BeginHorizontal(EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).GetStyle("sv_iconselector_labelselection"), GUILayout.MinHeight(10f));
|
||||
}
|
||||
GUILayout.BeginVertical();
|
||||
GUILayout.Space(4f);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try {
|
||||
GUILayout.Space(3f);
|
||||
GUILayout.EndVertical();
|
||||
EditorGUILayout.EndHorizontal();
|
||||
GUILayout.Space(3f);
|
||||
GUILayout.EndHorizontal();
|
||||
GUILayout.Space(3f);
|
||||
} catch (Exception e) {
|
||||
Debug.LogWarning(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class EditorBlock : IDisposable
|
||||
{
|
||||
public enum Orientation
|
||||
{
|
||||
Horizontal,
|
||||
Vertical
|
||||
}
|
||||
|
||||
private readonly Orientation _orientation;
|
||||
|
||||
public EditorBlock(Orientation orientation, string style, params GUILayoutOption[] options)
|
||||
{
|
||||
_orientation = orientation;
|
||||
if (orientation == Orientation.Horizontal) {
|
||||
EditorGUILayout.BeginHorizontal(string.IsNullOrEmpty(style) ? GUIStyle.none : style, options);
|
||||
} else {
|
||||
EditorGUILayout.BeginVertical(string.IsNullOrEmpty(style) ? GUIStyle.none : style, options);
|
||||
}
|
||||
}
|
||||
|
||||
public EditorBlock(Orientation orientation, string style) : this(orientation, style, new GUILayoutOption[] { })
|
||||
{
|
||||
}
|
||||
|
||||
public EditorBlock(Orientation orientation) : this(orientation, null, new GUILayoutOption[] { })
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_orientation == Orientation.Horizontal) {
|
||||
EditorGUILayout.EndHorizontal();
|
||||
} else {
|
||||
EditorGUILayout.EndVertical();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class SwitchColor : IDisposable
|
||||
{
|
||||
private readonly Color _savedColor;
|
||||
|
||||
public SwitchColor(Color newColor)
|
||||
{
|
||||
_savedColor = GUI.backgroundColor;
|
||||
GUI.color = newColor;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
GUI.color = _savedColor;
|
||||
}
|
||||
}
|
||||
|
||||
public class IndentBlock : IDisposable
|
||||
{
|
||||
public IndentBlock()
|
||||
{
|
||||
EditorGUI.indentLevel++;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
EditorGUI.indentLevel--;
|
||||
}
|
||||
}
|
||||
|
||||
public class ScrollViewBlock : IDisposable
|
||||
{
|
||||
public ScrollViewBlock(ref Vector2 scrollPosition, params GUILayoutOption[] options)
|
||||
{
|
||||
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, options);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
EditorGUILayout.EndScrollView();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class FoldableBlock : IDisposable
|
||||
{
|
||||
private readonly Color _defaultBackgroundColor;
|
||||
|
||||
private bool _expanded;
|
||||
|
||||
public FoldableBlock(ref bool expanded, string header) : this(ref expanded, header, null)
|
||||
{
|
||||
}
|
||||
|
||||
public FoldableBlock(ref bool expanded, string header, Texture2D icon)
|
||||
{
|
||||
_defaultBackgroundColor = GUI.backgroundColor;
|
||||
GUILayout.Space(3f);
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Space(3f);
|
||||
GUI.changed = false;
|
||||
if (!GUILayout.Toggle(true, new GUIContent("<b><size=11>" + header + "</size></b>", icon), "dragtab", GUILayout.MinWidth(20f)))
|
||||
expanded = !expanded;
|
||||
GUILayout.Space(2f);
|
||||
GUILayout.EndHorizontal();
|
||||
if (!expanded) {
|
||||
GUILayout.Space(3f);
|
||||
} else {
|
||||
GroupStart();
|
||||
}
|
||||
_expanded = expanded;
|
||||
}
|
||||
|
||||
private void GroupStart()
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Space(4f);
|
||||
EditorGUILayout.BeginHorizontal(NGUIEditorTools.textArea, GUILayout.MinHeight(10f));
|
||||
GUILayout.BeginVertical();
|
||||
GUILayout.Space(2f);
|
||||
}
|
||||
|
||||
private void GroupEnd()
|
||||
{
|
||||
try {
|
||||
GUILayout.Space(3f);
|
||||
GUILayout.EndVertical();
|
||||
EditorGUILayout.EndHorizontal();
|
||||
GUILayout.Space(3f);
|
||||
GUILayout.EndHorizontal();
|
||||
GUILayout.Space(3f);
|
||||
GUI.backgroundColor = _defaultBackgroundColor;
|
||||
} catch (Exception e) {
|
||||
Debug.LogWarning(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_expanded)
|
||||
GroupEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c3e20c87f936934aaefd4030d9b275b
|
||||
timeCreated: 1434825476
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4735623ddbab36341a8c0974da521977
|
||||
folderAsset: yes
|
||||
timeCreated: 1484616170
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace UnityEditorHelper
|
||||
{
|
||||
[CustomPropertyDrawer(typeof (LayerAttribute))]
|
||||
public class LayerPropertyDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.Integer)
|
||||
{
|
||||
Debug.LogWarning("LayerAttribute can only be applied on integer properties/fields");
|
||||
return;
|
||||
}
|
||||
|
||||
property.intValue = EditorGUI.LayerField(position, property.name, property.intValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db7cad7f95703f84ea5d8b4baea7a483
|
||||
timeCreated: 1434825477
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace UnityEditorHelper
|
||||
{
|
||||
[CustomPropertyDrawer(typeof (LimitAttribute))]
|
||||
public class LimitPropertyDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.Integer)
|
||||
{
|
||||
Debug.LogWarning("LimitAttribute can only be applied on integer properties/fields");
|
||||
return;
|
||||
}
|
||||
|
||||
LimitAttribute limiter = attribute as LimitAttribute;
|
||||
property.intValue = limiter.Limit(EditorGUI.IntField(position, property.name, property.intValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c031d8492d933e49b0bc44865d327aa
|
||||
timeCreated: 1434825477
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Reflection;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace UnityEditorHelper
|
||||
{
|
||||
[CustomPropertyDrawer(typeof (SortingLayerAttribute))]
|
||||
public class SortingLayerDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.Integer)
|
||||
{
|
||||
Debug.LogWarning("SortingLayerAttributes can only be applied on integer properties/fields");
|
||||
return;
|
||||
}
|
||||
EditorGUI.LabelField(position, label);
|
||||
|
||||
position.x += EditorGUIUtility.labelWidth;
|
||||
position.width -= EditorGUIUtility.labelWidth;
|
||||
|
||||
string[] sortingLayerNames = GetSortingLayerNames();
|
||||
int[] sortingLayerIDs = GetSortingLayerIDs();
|
||||
|
||||
int sortingLayerIndex = Mathf.Max(0, System.Array.IndexOf(sortingLayerIDs, property.intValue));
|
||||
sortingLayerIndex = EditorGUI.Popup(position, sortingLayerIndex, sortingLayerNames);
|
||||
property.intValue = sortingLayerIDs[sortingLayerIndex];
|
||||
}
|
||||
|
||||
private string[] GetSortingLayerNames()
|
||||
{
|
||||
System.Type internalEditorUtilityType = typeof (InternalEditorUtility);
|
||||
PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
|
||||
return (string[]) sortingLayersProperty.GetValue(null, new object[0]);
|
||||
}
|
||||
|
||||
private int[] GetSortingLayerIDs()
|
||||
{
|
||||
System.Type internalEditorUtilityType = typeof (InternalEditorUtility);
|
||||
PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
|
||||
return (int[]) sortingLayersProperty.GetValue(null, new object[0]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3e77ff7706ee9c4a85b478eb23510fd
|
||||
timeCreated: 1434825477
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace UnityEditorHelper
|
||||
{
|
||||
[CustomPropertyDrawer(typeof (TagAttribute))]
|
||||
public class TagPropertyDrawer : PropertyDrawer
|
||||
{
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
||||
{
|
||||
if (property.propertyType != SerializedPropertyType.String)
|
||||
{
|
||||
Debug.LogWarning("TagAttribute can only be applied on string properties/fields");
|
||||
return;
|
||||
}
|
||||
|
||||
property.stringValue = EditorGUI.TagField(position, property.name, property.stringValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32950be6beb9f3b4ca5419e5d71ec0ed
|
||||
timeCreated: 1434825476
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user