Skip to content

Commit

Permalink
提交代码
Browse files Browse the repository at this point in the history
  • Loading branch information
akof1314 committed Jun 29, 2016
1 parent a09f7f4 commit 94b9d43
Show file tree
Hide file tree
Showing 35 changed files with 377 additions and 2 deletions.
2 changes: 2 additions & 0 deletions AnimationCurvePopupMenu/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/Library/
/Temp/
9 changes: 9 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/AnimationCurveDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(AnimationCurve))]
public class AnimationCurveDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
AnimationCurveGUI.OnGUI(position, property, label);
}
}
12 changes: 12 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/AnimationCurveDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/AnimationCurveGUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using UnityEngine;
using UnityEditor;

public class AnimationCurveGUI
{
private static Rect SubtractPopupWidth(Rect position)
{
position.width -= 18f;
return position;
}

private static Rect GetPopupRect(Rect position)
{
position.xMin = position.xMax - 13f;
return position;
}

#region EditorGUILayout
public static AnimationCurve CurveField(AnimationCurve value, params GUILayoutOption[] options)
{
Rect position = EditorGUILayout.GetControlRect(false, 16f, EditorStyles.colorField, options);
return CurveField(position, value);
}

public static AnimationCurve CurveField(string label, AnimationCurve value, params GUILayoutOption[] options)
{
Rect position = EditorGUILayout.GetControlRect(true, 16f, EditorStyles.colorField, options);
return CurveField(position, label, value);
}

public static AnimationCurve CurveField(GUIContent label, AnimationCurve value, params GUILayoutOption[] options)
{
Rect position = EditorGUILayout.GetControlRect(true, 16f, EditorStyles.colorField, options);
return CurveField(position, label, value);
}
#endregion

#region EditorGUI
public static AnimationCurve CurveField(Rect position, AnimationCurve value)
{
AnimationCurve animationCurve = EditorGUI.CurveField(SubtractPopupWidth(position), value);
AnimationCurvePopupMenu.Show(GetPopupRect(position), animationCurve, null);
return animationCurve;
}

public static AnimationCurve CurveField(Rect position, string label, AnimationCurve value)
{
return CurveField(position, new GUIContent(label), value);
}

public static AnimationCurve CurveField(Rect position, GUIContent label, AnimationCurve value)
{
AnimationCurve animationCurve = EditorGUI.CurveField(SubtractPopupWidth(position), label, value);
AnimationCurvePopupMenu.Show(GetPopupRect(position), animationCurve, null);
return animationCurve;
}
#endregion

/// <summary>
/// 默认属性字段的绘制,也会为PropertyField自动绘制
/// </summary>
/// <param name="position"></param>
/// <param name="property"></param>
/// <param name="label"></param>
public static void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
label = EditorGUI.BeginProperty(position, label, property);
EditorGUI.CurveField(EditorGUI.PrefixLabel(SubtractPopupWidth(position), label), property, Color.green, default(Rect));
AnimationCurvePopupMenu.Show(GetPopupRect(position), null, property);
EditorGUI.EndProperty();
}
}
12 changes: 12 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/AnimationCurveGUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 118 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/AnimationCurvePopupMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

public class AnimationCurvePopupMenu
{
private static AnimationCurve s_ClipBoardAnimationCurve;

public static void Show(Rect popupRect, AnimationCurve animationCurve, SerializedProperty property)
{
if (GUI.Button(popupRect, GUIContent.none, "ShurikenDropdown"))
{
GUIContent content = new GUIContent("Copy");
GUIContent content2 = new GUIContent("Paste");
GUIContent content3 = new GUIContent("Clear");
GenericMenu genericMenu = new GenericMenu();

if (property != null)
{
genericMenu.AddItem(content, false, AnimationCurveCallbackCopy, property);
genericMenu.AddItem(content2, false, AnimationCurveCallbackPaste, property);
genericMenu.AddItem(content3, false, AnimationCurveCallbackClear, property);
}
else
{
genericMenu.AddItem(content, false, AnimationCurveCallback2Copy, animationCurve);
genericMenu.AddItem(content2, false, AnimationCurveCallback2Paste, animationCurve);
genericMenu.AddItem(content3, false, AnimationCurveCallback2Clear, animationCurve);
}

if (!HasClipBoardAnimationCurve())
{
genericMenu.AddDisabledItem(content2);
}
genericMenu.DropDown(popupRect);
Event.current.Use();
}
}

/// <summary>
/// 清除检视器界面的曲线缓存,才能刷新新的值
/// </summary>
public static void AnimationCurvePreviewCacheClearCache()
{
Assembly assembly = Assembly.GetAssembly(typeof(ReorderableList));
Type type = assembly.GetType("UnityEditorInternal.AnimationCurvePreviewCache");
MethodInfo clearCache = type.GetMethod("ClearCache", BindingFlags.Static | BindingFlags.Public);
if (clearCache != null)
{
clearCache.Invoke(null, null);
}
}

private static bool HasClipBoardAnimationCurve()
{
return s_ClipBoardAnimationCurve != null;
}

private static void AnimationCurveCallbackCopy(object obj)
{
SerializedProperty property = (SerializedProperty)obj;
s_ClipBoardAnimationCurve = property.animationCurveValue;
}

private static void AnimationCurveCallbackPaste(object obj)
{
if (s_ClipBoardAnimationCurve == null)
{
return;
}
SerializedProperty property = (SerializedProperty)obj;
property.serializedObject.Update();
property.animationCurveValue = s_ClipBoardAnimationCurve;
property.serializedObject.ApplyModifiedProperties();
}

private static void AnimationCurveCallbackClear(object obj)
{
SerializedProperty property = (SerializedProperty)obj;
property.serializedObject.Update();
property.animationCurveValue = new AnimationCurve();
property.serializedObject.ApplyModifiedProperties();
}

private static void AnimationCurveCallback2Copy(object obj)
{
AnimationCurve animationCurve = (AnimationCurve)obj;
s_ClipBoardAnimationCurve = animationCurve;
}

private static void AnimationCurveCallback2Paste(object obj)
{
if (s_ClipBoardAnimationCurve == null)
{
return;
}
AnimationCurve animationCurve = (AnimationCurve)obj;
animationCurve.keys = s_ClipBoardAnimationCurve.keys;
animationCurve.postWrapMode = s_ClipBoardAnimationCurve.postWrapMode;
animationCurve.preWrapMode = s_ClipBoardAnimationCurve.preWrapMode;
AnimationCurvePreviewCacheClearCache();
}

private static void AnimationCurveCallback2Clear(object obj)
{
AnimationCurve animationCurve = (AnimationCurve)obj;
if (animationCurve != null)
{
for (int i = animationCurve.length - 1; i >= 0; i--)
{
animationCurve.RemoveKey(i);
}
AnimationCurvePreviewCacheClearCache();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/TestCurveEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(TestCurve))]
public class TestCurveEditor : Editor
{
SerializedProperty curveProp;
SerializedProperty curveProp2;
private AnimationCurve m_TestAnimCurve = new AnimationCurve();
private AnimationCurve m_TestAnimCurve2 = new AnimationCurve();

void OnEnable()
{
curveProp = serializedObject.FindProperty("curve");
curveProp2 = serializedObject.FindProperty("curve2");
}

public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(curveProp);
EditorGUILayout.PropertyField(curveProp2, new GUIContent("Curve2 Object"));

m_TestAnimCurve = AnimationCurveGUI.CurveField("扩展曲线", m_TestAnimCurve);
m_TestAnimCurve2 = EditorGUILayout.CurveField("默认曲线", m_TestAnimCurve2);

//EditorGUILayout.TextField("abc");

serializedObject.ApplyModifiedProperties();
}
}
12 changes: 12 additions & 0 deletions AnimationCurvePopupMenu/Assets/Editor/TestCurveEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added AnimationCurvePopupMenu/Assets/Test.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions AnimationCurvePopupMenu/Assets/Test.unity.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions AnimationCurvePopupMenu/Assets/TestCurve.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using UnityEngine;
using System.Collections;

public class TestCurve : MonoBehaviour
{
public AnimationCurve curve; // for preview
public AnimationCurve curve2; // for preview
}
12 changes: 12 additions & 0 deletions AnimationCurvePopupMenu/Assets/TestCurve.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions AnimationCurvePopupMenu/ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
m_EditorVersion: 5.3.4p5
m_StandardAssetsVersion: 0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
# UnityAnimationCurvePopupMenu
Unity AnimationCurvePopupMenu (Copy, Paste)
# Unity AnimationCurvePopupMenu
Unity 曲线编辑扩展菜单功能

# 原因
默认的 AnimationCurve 字段曲线编辑,不支持复制粘贴到另一个 AnimationCurve 字段,亦不支持关键帧的清空。所以扩展 AnimationCurve 的绘制,在右侧添加下拉菜单,以扩展功能。

# 功能
* 复制曲线
* 粘贴曲线
* 清空曲线

# 截图
点击右侧下拉按钮,点击 Copy 复制:
![](https://github.com/akof1314/UnityAnimationCurvePopupMenu/raw/master/Screenshots/screenshot_copy.png)
在另一个曲线,右侧菜单点击 Paste 粘贴:
![](https://github.com/akof1314/UnityAnimationCurvePopupMenu/raw/master/Screenshots/screenshot_copy.png)
清空曲线的关键帧值,点击 Clear 清空:
![](https://github.com/akof1314/UnityAnimationCurvePopupMenu/raw/master/Screenshots/screenshot_clear.png)

# 使用说明
代码放进工程里面,对于默认的脚本,会自动调用扩展的曲线绘制方式:
默认的脚本类似:
```
public class TestCurve : MonoBehaviour
{
public AnimationCurve curve;
}
```
高级方式,要对默认脚本进行自定义绘制的话,创建 Editor 类。接着,对于属性值,调用 Unity 自带的接口 `EditorGUILayout.PropertyField` 即可。对于非属性值,可通过 AnimationCurveGUI 接口来替换 EditorGUILayout 和 EditorGUI 来绘制曲线:
```
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(TestCurve))]
public class TestCurveEditor : Editor
{
private SerializedProperty curveProp;
private AnimationCurve m_TestAnimCurve = new AnimationCurve();
void OnEnable()
{
curveProp = serializedObject.FindProperty("curve");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(curveProp);
m_TestAnimCurve = AnimationCurveGUI.CurveField("扩展曲线", m_TestAnimCurve);
serializedObject.ApplyModifiedProperties();
}
}
```
Binary file added Screenshots/screenshot_clear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/screenshot_copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/screenshot_paste.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 94b9d43

Please sign in to comment.