File size: 7,624 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Object = UnityEngine.Object;
namespace UnityEditor.Timeline
{
class PropertyCollector : IPropertyCollector
{
readonly Stack<GameObject> m_ObjectStack = new Stack<GameObject>();
// Call immediately before use
public void Reset()
{
m_ObjectStack.Clear();
}
// call to reset caches. should be called when switching master timelines
public void Clear()
{
m_ObjectStack.Clear();
AnimationPreviewUtilities.ClearCaches();
}
public void PushActiveGameObject(GameObject gameObject)
{
m_ObjectStack.Push(gameObject);
}
public void PopActiveGameObject()
{
m_ObjectStack.Pop();
}
public void AddFromClip(AnimationClip clip)
{
var go = m_ObjectStack.Peek(); // allow it to throw if empty
if (go != null && clip != null) // null game object is allowed for calls to be ignored
AddFromClip(go, clip);
}
public void AddFromClips(IEnumerable<AnimationClip> clips)
{
var go = m_ObjectStack.Peek();
if (go != null)
AddFromClips(go, clips);
}
public void AddFromName<T>(string name) where T : Component
{
var go = m_ObjectStack.Peek(); // allow it to throw if empty
if (go != null) // null game object is allowed for calls to be ignored
AddFromName<T>(go, name);
}
public void AddFromName(string name)
{
var go = m_ObjectStack.Peek(); // allow it to throw if empty
if (go != null) // null game object is allowed for calls to be ignored
AddFromName(go, name);
}
public void AddFromClip(GameObject obj, AnimationClip clip)
{
if (!Application.isPlaying)
AddPropertiesFromClip(obj, clip);
}
public void AddFromClips(GameObject animatorRoot, IEnumerable<AnimationClip> clips)
{
if (Application.isPlaying)
return;
AnimationPreviewUtilities.PreviewFromCurves(animatorRoot, AnimationPreviewUtilities.GetBindings(animatorRoot, clips));
}
public void AddFromName<T>(GameObject obj, string name) where T : Component
{
if (!Application.isPlaying)
AddPropertiesFromName(obj, typeof(T), name);
}
public void AddFromName(GameObject obj, string name)
{
if (!Application.isPlaying)
AddPropertiesFromName(obj, name);
}
public void AddFromName(Component component, string name)
{
if (!Application.isPlaying)
AddPropertyModification(component, name);
}
public void AddFromComponent(GameObject obj, Component component)
{
if (Application.isPlaying)
return;
if (obj == null || component == null)
return;
var serializedObject = new SerializedObject(component);
SerializedProperty property = serializedObject.GetIterator();
while (property.NextVisible(true))
{
if (property.hasVisibleChildren || !AnimatedParameterUtility.IsTypeAnimatable(property.propertyType))
continue;
AddPropertyModification(component, property.propertyPath);
}
}
void AddPropertiesFromClip(GameObject go, AnimationClip clip)
{
if (go != null && clip != null)
{
AnimationMode.InitializePropertyModificationForGameObject(go, clip);
}
}
static void AddPropertiesFromName(GameObject go, string property)
{
if (go == null)
return;
AddPropertyModification(go, property);
}
static void AddPropertiesFromName(GameObject go, Type compType, string property)
{
if (go == null)
return;
var comp = go.GetComponent(compType);
if (comp == null)
return;
AddPropertyModification(comp, property);
}
public void AddObjectProperties(Object obj, AnimationClip clip)
{
if (obj == null || clip == null)
return;
IPlayableAsset asset = obj as IPlayableAsset;
IPlayableBehaviour playable = obj as IPlayableBehaviour;
// special case for assets that contain animated script playables.
// The paths in the clip start from the field with the templated playable
if (asset != null)
{
if (playable == null)
{
AddSerializedPlayableModifications(asset, clip);
}
else
{
// in this case the asset is the playable. The clip applies directly
AnimationMode.InitializePropertyModificationForObject(obj, clip);
}
}
}
void AddSerializedPlayableModifications(IPlayableAsset asset, AnimationClip clip)
{
var obj = asset as Object;
if (obj == null)
return;
var driver = WindowState.previewDriver;
if (driver == null || !AnimationMode.InAnimationMode(driver))
return;
var serializedObj = new SerializedObject(obj);
var bindings = AnimationClipCurveCache.Instance.GetCurveInfo(clip).bindings;
var fields = AnimatedParameterUtility.GetScriptPlayableFields(asset);
// go through each binding and offset using the field name
// so the modification system can find the particle object using the asset as a root
foreach (var b in bindings)
{
foreach (var f in fields)
{
var propertyPath = f.Name + "." + b.propertyName;
if (serializedObj.FindProperty(propertyPath) != null)
{
DrivenPropertyManager.RegisterProperty(driver, obj, propertyPath);
break;
}
}
}
}
private static void AddPropertyModification(GameObject obj, string propertyName)
{
var driver = WindowState.previewDriver;
if (driver == null || !AnimationMode.InAnimationMode(driver))
return;
DrivenPropertyManager.RegisterProperty(driver, obj, propertyName);
}
private static void AddPropertyModification(Component comp, string name)
{
if (comp == null)
return;
var driver = WindowState.previewDriver;
if (driver == null || !AnimationMode.InAnimationMode(driver))
return;
// Register Property will display an error if a property doesn't exist (wanted behaviour)
// However, it also displays an error on Monobehaviour m_Script property, since it can't be driven. (not wanted behaviour)
// case 967026
if (name == "m_Script" && (comp as MonoBehaviour) != null)
return;
DrivenPropertyManager.RegisterProperty(driver, comp, name);
}
}
}
|