ZMWSLI0-SL2021-GR11/cw1/clock/Library/PackageCache/com.unity.timeline@1.2.18/Editor/Utilities/BindingUtility.cs

59 lines
1.9 KiB
C#
Raw Permalink Normal View History

2021-04-10 17:27:42 +02:00
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
}
}