BindingUtility.cs
1.92 KB
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
using System;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
using Object = UnityEngine.Object;
namespace UnityEditor.Timeline
{
static class BindingUtility
{
public static Type GetRequiredBindingType(PlayableBinding binding)
{
return binding.outputTargetType;
}
public static void Bind(PlayableDirector director, TrackAsset bindTo, Object objectToBind)
{
if (director == null || bindTo == null || TimelineWindow.instance == null)
return;
TimelineWindow.instance.state.previewMode = false; // returns all objects to previous state
TimelineUndo.PushUndo(director, "PlayableDirector Binding");
director.SetGenericBinding(bindTo, objectToBind);
TimelineWindow.instance.state.rebuildGraph = true;
}
public static BindingAction GetBindingAction(Type requiredBindingType, Object objectToBind)
{
if (requiredBindingType == null || objectToBind == null)
return BindingAction.DoNotBind;
// prevent drag and drop of prefab assets
if (PrefabUtility.IsPartOfPrefabAsset(objectToBind))
return BindingAction.DoNotBind;
if (requiredBindingType.IsInstanceOfType(objectToBind))
return BindingAction.BindDirectly;
var draggedGameObject = objectToBind as GameObject;
if (!typeof(Component).IsAssignableFrom(requiredBindingType) || draggedGameObject == null)
return BindingAction.DoNotBind;
if (draggedGameObject.GetComponent(requiredBindingType) == null)
return BindingAction.BindToMissingComponent;
return BindingAction.BindToExistingComponent;
}
}
enum BindingAction
{
DoNotBind,
BindDirectly,
BindToExistingComponent,
BindToMissingComponent
}
}