Skip to content

Commit 55dd5f4

Browse files
author
Unity Technologies
committed
Unity 2017.3.0a2 C# reference source code
1 parent ff0db31 commit 55dd5f4

File tree

149 files changed

+4174
-3295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+4174
-3295
lines changed

Editor/Mono/Animation/AnimationWindow/AnimEditor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,15 +1032,15 @@ private void SaveChangedCurvesFromCurveEditor()
10321032
if (curvesToUpdate.TryGetValue(curveWrapper.animationClip, out changedCurves))
10331033
{
10341034
changedCurves.bindings.Add(curveWrapper.binding);
1035-
changedCurves.curves.Add(curveWrapper.curve);
1035+
changedCurves.curves.Add(curveWrapper.curve.length > 0 ? curveWrapper.curve : null);
10361036
}
10371037
else
10381038
{
10391039
changedCurves.bindings = new List<EditorCurveBinding>();
10401040
changedCurves.curves = new List<AnimationCurve>();
10411041

10421042
changedCurves.bindings.Add(curveWrapper.binding);
1043-
changedCurves.curves.Add(curveWrapper.curve);
1043+
changedCurves.curves.Add(curveWrapper.curve.length > 0 ? curveWrapper.curve : null);
10441044

10451045
curvesToUpdate.Add(curveWrapper.animationClip, changedCurves);
10461046
}

Editor/Mono/Animation/AnimationWindow/AnimationRecording.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,12 @@ static private UndoPropertyModification[] ProcessRotationModifications(IAnimatio
320320
}
321321
else
322322
{
323-
AddRotationKey(state, binding, type, previousValue.eulerAngles, currentValue.eulerAngles);
323+
Vector3 eulerAngles = target.GetLocalEulerAngles(RotationOrder.OrderZXY);
324+
325+
Vector3 previousEulerAngles = AnimationUtility.GetClosestEuler(previousValue, eulerAngles, RotationOrder.OrderZXY);
326+
Vector3 currentEulerAngles = AnimationUtility.GetClosestEuler(currentValue, eulerAngles, RotationOrder.OrderZXY);
327+
328+
AddRotationKey(state, binding, type, previousEulerAngles, currentEulerAngles);
324329
}
325330
}
326331

Editor/Mono/Animation/AnimationWindow/AnimationWindowCurve.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal class AnimationWindowCurve : IComparable<AnimationWindowCurve>
2727

2828
public EditorCurveBinding binding { get { return m_Binding; } }
2929
public bool isPPtrCurve { get { return m_Binding.isPPtrCurve; } }
30+
public bool isDiscreteCurve { get { return m_Binding.isDiscreteCurve; } }
3031
public bool isPhantom { get { return m_Binding.isPhantom; } }
3132
public string propertyName { get { return m_Binding.propertyName; } }
3233
public string path { get { return m_Binding.path; } }
@@ -223,7 +224,7 @@ public AnimationWindowKeyframe FindKeyAtTime(AnimationKeyTime keyTime)
223224
public object Evaluate(float time)
224225
{
225226
if (m_Keyframes.Count == 0)
226-
return null;
227+
return isPPtrCurve ? null : (object)0f;
227228

228229
AnimationWindowKeyframe firstKey = m_Keyframes[0];
229230
if (time <= firstKey.time)
@@ -261,7 +262,7 @@ public object Evaluate(float time)
261262
}
262263

263264
// Shouldn't happen...
264-
return null;
265+
return isPPtrCurve ? null : (object)0f;
265266
}
266267

267268
public void AddKeyframe(AnimationWindowKeyframe key, AnimationKeyTime keyTime)

Editor/Mono/Animation/AnimationWindow/AnimationWindowKeyframe.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public AnimationWindowCurve curve
6060
}
6161

6262
public bool isPPtrCurve { get { return curve.isPPtrCurve; } }
63+
public bool isDiscreteCurve { get { return curve.isDiscreteCurve; } }
6364

6465
public AnimationWindowKeyframe()
6566
{

Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,13 @@ public CurveWrapper[] activeCurveWrappers
580580
{
581581
List<CurveWrapper> activeCurveWrappers = new List<CurveWrapper>();
582582
foreach (AnimationWindowCurve curve in activeCurves)
583-
if (!curve.isPPtrCurve)
583+
if (!curve.isDiscreteCurve)
584584
activeCurveWrappers.Add(AnimationWindowUtility.GetCurveWrapper(curve, curve.clip));
585585

586586
// If there are no active curves, we would end up with empty curve editor so we just give all curves insteads
587587
if (!activeCurveWrappers.Any())
588588
foreach (AnimationWindowCurve curve in allCurves)
589-
if (!curve.isPPtrCurve)
589+
if (!curve.isDiscreteCurve)
590590
activeCurveWrappers.Add(AnimationWindowUtility.GetCurveWrapper(curve, curve.clip));
591591

592592
m_ActiveCurveWrappersCache = activeCurveWrappers.ToArray();

Editor/Mono/Animation/AnimationWindow/AnimationWindowUtility.cs

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,29 @@ public static AnimationWindowKeyframe AddKeyframeToCurve(AnimationWindowCurve cu
184184
keyframe.curve = curve;
185185
curve.AddKeyframe(keyframe, time);
186186
}
187-
else if (type == typeof(bool) || type == typeof(float))
187+
else if (type == typeof(bool) || type == typeof(float) || type == typeof(int))
188188
{
189189
keyframe = new AnimationWindowKeyframe();
190190

191191
// Create temporary curve for getting proper tangents
192192
AnimationCurve animationCurve = curve.ToAnimationCurve();
193193

194194
Keyframe tempKey = new Keyframe(time.time, (float)value);
195-
if (type == typeof(bool))
195+
if (type == typeof(bool) || type == typeof(int))
196196
{
197-
AnimationUtility.SetKeyLeftTangentMode(ref tempKey, TangentMode.Constant);
198-
AnimationUtility.SetKeyRightTangentMode(ref tempKey, TangentMode.Constant);
197+
if (type == typeof(int) && !curve.isDiscreteCurve)
198+
{
199+
AnimationUtility.SetKeyLeftTangentMode(ref tempKey, TangentMode.Linear);
200+
AnimationUtility.SetKeyRightTangentMode(ref tempKey, TangentMode.Linear);
201+
}
202+
else
203+
{
204+
AnimationUtility.SetKeyLeftTangentMode(ref tempKey, TangentMode.Constant);
205+
AnimationUtility.SetKeyRightTangentMode(ref tempKey, TangentMode.Constant);
206+
}
207+
199208
AnimationUtility.SetKeyBroken(ref tempKey, true);
200209
keyframe.m_TangentMode = tempKey.tangentMode;
201-
keyframe.m_InTangent = Mathf.Infinity;
202-
keyframe.m_OutTangent = Mathf.Infinity;
203210
}
204211
else
205212
{
@@ -486,8 +493,9 @@ public static PropertyModification[] SerializedPropertyToPropertyModifications(S
486493
var isFloat = propertyIter.propertyType == SerializedPropertyType.Float;
487494
var isBool = propertyIter.propertyType == SerializedPropertyType.Boolean;
488495
var isInt = propertyIter.propertyType == SerializedPropertyType.Integer;
496+
var isEnum = propertyIter.propertyType == SerializedPropertyType.Enum;
489497

490-
if (isObject || isFloat || isBool || isInt)
498+
if (isObject || isFloat || isBool || isInt || isEnum)
491499
{
492500
var serializedObject = propertyIter.serializedObject;
493501
var targetObjects = serializedObject.targetObjects;
@@ -508,6 +516,8 @@ public static PropertyModification[] SerializedPropertyToPropertyModifications(S
508516
value = singleProperty.floatValue.ToString();
509517
else if (isInt)
510518
value = singleProperty.intValue.ToString();
519+
else if (isEnum)
520+
value = singleProperty.enumValueIndex.ToString();
511521
else // if (isBool)
512522
value = singleProperty.boolValue.ToString();
513523

@@ -727,7 +737,9 @@ public static string GetPropertyGroupName(string propertyName)
727737
public static float GetNextKeyframeTime(AnimationWindowCurve[] curves, float currentTime, float frameRate)
728738
{
729739
float candidate = float.MaxValue;
730-
float nextFrame = currentTime + 1f / frameRate;
740+
AnimationKeyTime time = AnimationKeyTime.Time(currentTime, frameRate);
741+
AnimationKeyTime nextTime = AnimationKeyTime.Frame(time.frame + 1, frameRate);
742+
731743
bool found = false;
732744

733745
foreach (AnimationWindowCurve curve in curves)
@@ -736,20 +748,22 @@ public static float GetNextKeyframeTime(AnimationWindowCurve[] curves, float cur
736748
{
737749
float offsetTime = keyframe.time + curve.timeOffset;
738750

739-
if (offsetTime < candidate && offsetTime >= nextFrame)
751+
if (offsetTime < candidate && offsetTime >= nextTime.time)
740752
{
741753
candidate = offsetTime;
742754
found = true;
743755
}
744756
}
745757
}
746-
return found ? candidate : currentTime;
758+
return found ? candidate : time.time;
747759
}
748760

749761
public static float GetPreviousKeyframeTime(AnimationWindowCurve[] curves, float currentTime, float frameRate)
750762
{
751763
float candidate = float.MinValue;
752-
float previousFrame = Mathf.Max(0f, currentTime - 1f / frameRate);
764+
AnimationKeyTime time = AnimationKeyTime.Time(currentTime, frameRate);
765+
AnimationKeyTime previousTime = AnimationKeyTime.Frame(time.frame - 1, frameRate);
766+
753767
bool found = false;
754768

755769
foreach (AnimationWindowCurve curve in curves)
@@ -758,14 +772,14 @@ public static float GetPreviousKeyframeTime(AnimationWindowCurve[] curves, float
758772
{
759773
float offsetTime = keyframe.time + curve.timeOffset;
760774

761-
if (offsetTime > candidate && offsetTime <= previousFrame)
775+
if (offsetTime > candidate && offsetTime <= previousTime.time)
762776
{
763777
candidate = offsetTime;
764778
found = true;
765779
}
766780
}
767781
}
768-
return found ? candidate : currentTime;
782+
return found ? candidate : time.time;
769783
}
770784

771785
// Add animator, controller and clip to gameobject if they are missing to make this gameobject animatable
@@ -1049,10 +1063,47 @@ public static void DrawRect(Rect rect, Color color)
10491063
GL.PopMatrix();
10501064
}
10511065

1066+
private static CurveRenderer CreateRendererForCurve(AnimationWindowCurve curve)
1067+
{
1068+
CurveRenderer renderer;
1069+
switch (System.Type.GetTypeCode(curve.valueType))
1070+
{
1071+
case TypeCode.Int32:
1072+
renderer = new IntCurveRenderer(curve.ToAnimationCurve());
1073+
break;
1074+
case TypeCode.Boolean:
1075+
renderer = new BoolCurveRenderer(curve.ToAnimationCurve());
1076+
break;
1077+
default:
1078+
renderer = new NormalCurveRenderer(curve.ToAnimationCurve());
1079+
break;
1080+
}
1081+
return renderer;
1082+
}
1083+
1084+
private static CurveWrapper.PreProcessKeyMovement CreateKeyPreprocessorForCurve(AnimationWindowCurve curve)
1085+
{
1086+
CurveWrapper.PreProcessKeyMovement method;
1087+
switch (System.Type.GetTypeCode(curve.valueType))
1088+
{
1089+
case TypeCode.Int32:
1090+
method = (ref Keyframe key) => { key.value = Mathf.Floor(key.value + 0.5f); };
1091+
break;
1092+
case TypeCode.Boolean:
1093+
method = (ref Keyframe key) => { key.value = key.value > 0.5f ? 1.0f : 0.0f; };
1094+
break;
1095+
default:
1096+
method = null;
1097+
break;
1098+
}
1099+
return method;
1100+
}
1101+
10521102
public static CurveWrapper GetCurveWrapper(AnimationWindowCurve curve, AnimationClip clip)
10531103
{
10541104
CurveWrapper curveWrapper = new CurveWrapper();
1055-
curveWrapper.renderer = new NormalCurveRenderer(curve.ToAnimationCurve());
1105+
curveWrapper.renderer = CreateRendererForCurve(curve);
1106+
curveWrapper.preProcessKeyMovementDelegate = CreateKeyPreprocessorForCurve(curve);
10561107
curveWrapper.renderer.SetWrap(WrapMode.Clamp, clip.isLooping ? WrapMode.Loop : WrapMode.Clamp);
10571108
curveWrapper.renderer.SetCustomRange(clip.startTime, clip.stopTime);
10581109
curveWrapper.binding = curve.binding;

Editor/Mono/Animation/AnimationWindow/CurveEditor.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Copyright (c) Unity Technologies. For terms of use, see
33
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
44

5-
65
using UnityEngine;
76
using UnityEditor;
87
using System;
@@ -40,6 +39,7 @@ internal class CurveWrapper
4039
{
4140
public delegate Vector2 GetAxisScalarsCallback();
4241
public delegate void SetAxisScalarsCallback(Vector2 newAxisScalars);
42+
public delegate void PreProcessKeyMovement(ref Keyframe key);
4343

4444
public CurveWrapper()
4545
{
@@ -90,6 +90,8 @@ internal enum SelectionMode
9090
public GetAxisScalarsCallback getAxisUiScalarsCallback; // Delegate used to fetch values that are multiplied on x and y axis ui values
9191
public SetAxisScalarsCallback setAxisUiScalarsCallback; // Delegate used to set values back that has been changed by this curve editor
9292

93+
public PreProcessKeyMovement preProcessKeyMovementDelegate; // Delegate used limit key manipulation to fit curve constraints
94+
9395
// Should be updated by curve editor as appropriate
9496
public SelectionMode selected;
9597
public int listIndex; // Index into m_AnimationCurves list
@@ -111,12 +113,29 @@ public bool changed
111113
}
112114
}
113115

116+
public int AddKey(Keyframe key)
117+
{
118+
PreProcessKey(ref key);
119+
return curve.AddKey(key);
120+
}
121+
122+
public void PreProcessKey(ref Keyframe key)
123+
{
124+
if (preProcessKeyMovementDelegate != null)
125+
preProcessKeyMovementDelegate(ref key);
126+
}
127+
128+
public int MoveKey(int index, ref Keyframe key)
129+
{
130+
PreProcessKey(ref key);
131+
return curve.MoveKey(index, key);
132+
}
133+
114134
// An additional vertical min / max range clamp when editing multiple curves with different ranges
115135
public float vRangeMin = -Mathf.Infinity;
116136
public float vRangeMax = Mathf.Infinity;
117137
}
118138

119-
120139
// Control point collection renderer
121140
class CurveControlPointRenderer
122141
{
@@ -568,7 +587,6 @@ private enum AxisLock { None, X, Y }
568587
private AxisLock m_AxisLock;
569588

570589
CurveControlPointRenderer m_PointRenderer;
571-
572590
CurveEditorRectangleTool m_RectangleTool;
573591

574592
// The square of the maximum pick distance in pixels.
@@ -1248,7 +1266,7 @@ void DragTangents()
12481266
}
12491267
}
12501268

1251-
dragged.key = curveWrapper.curve.MoveKey(dragged.key, key);
1269+
dragged.key = curveWrapper.MoveKey(dragged.key, ref key);
12521270
AnimationUtility.UpdateTangentsFromModeSurrounding(curveWrapper.curve, dragged.key);
12531271

12541272
curveWrapper.changed = true;
@@ -1480,7 +1498,6 @@ internal void TransformSelectedKeys(Matrix4x4 matrix, bool flipX, bool flipY)
14801498

14811499
return duplicateKeyframe;
14821500
}
1483-
14841501
return keyframe;
14851502
}
14861503
);
@@ -1582,6 +1599,7 @@ void UpdateCurvesFromPoints(SavedCurve.KeyFrameOperation action)
15821599
if (keyframe.selected == CurveWrapper.SelectionMode.None)
15831600
{
15841601
var newKeyframe = action(keyframe, sc);
1602+
cw.PreProcessKey(ref newKeyframe.key);
15851603

15861604
// We might have moved keys around, let's add new key or replace existing key.
15871605
working[newKeyframe.key.time] = newKeyframe;
@@ -1594,7 +1612,7 @@ void UpdateCurvesFromPoints(SavedCurve.KeyFrameOperation action)
15941612
if (keyframe.selected != CurveWrapper.SelectionMode.None)
15951613
{
15961614
var newKeyframe = action(keyframe, sc);
1597-
1615+
cw.PreProcessKey(ref newKeyframe.key);
15981616
// We might have moved keys around, let's add new key or replace existing key.
15991617
working[newKeyframe.key.time] = newKeyframe;
16001618
}
@@ -1988,7 +2006,7 @@ CurveSelection AddKeyframeAndSelect(Keyframe key, CurveWrapper cw)
19882006
if (!cw.animationIsEditable)
19892007
return null;
19902008

1991-
int keyIndex = cw.curve.AddKey(key);
2009+
int keyIndex = cw.AddKey(key);
19922010
CurveUtility.SetKeyModeFromContext(cw.curve, keyIndex);
19932011
AnimationUtility.UpdateTangentsFromModeSurrounding(cw.curve, keyIndex);
19942012

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Unity C# reference source
2+
// Copyright (c) Unity Technologies. For terms of use, see
3+
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
4+
5+
using System.Linq;
6+
using UnityEngine;
7+
using UnityEditor;
8+
using System.Collections;
9+
using System.Collections.Generic;
10+
11+
namespace UnityEditor
12+
{
13+
internal class BoolCurveRenderer : NormalCurveRenderer
14+
{
15+
public BoolCurveRenderer(AnimationCurve curve)
16+
: base(curve)
17+
{
18+
}
19+
20+
public override float ClampedValue(float value)
21+
{
22+
return value != 0.0f ? 1.0f : 0.0f;
23+
}
24+
25+
public override float EvaluateCurveSlow(float time)
26+
{
27+
return ClampedValue(GetCurve().Evaluate(time));
28+
}
29+
}
30+
} // namespace

Editor/Mono/Animation/AnimationWindow/CurveRenderer/CurveRenderer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ internal interface CurveRenderer
2222
float EvaluateCurveDeltaSlow(float time);
2323
Bounds GetBounds();
2424
Bounds GetBounds(float minTime, float maxTime);
25+
float ClampedValue(float value);
2526
void FlushCache();
2627
}
2728
} // namespace

Editor/Mono/Animation/AnimationWindow/CurveRenderer/EulerCurveRenderer.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public AnimationCurve GetCurve()
2525
return renderer.GetCurveOfComponent(component);
2626
}
2727

28+
public float ClampedValue(float value)
29+
{
30+
return value;
31+
}
32+
2833
public float RangeStart() { return renderer.RangeStart(); }
2934
public float RangeEnd() { return renderer.RangeEnd(); }
3035
public void SetWrap(WrapMode wrap)

0 commit comments

Comments
 (0)