Skip to content

Instantly share code, notes, and snippets.

@andybak
Last active April 3, 2018 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybak/997aabc2dbb826c7d089cda413c1617d to your computer and use it in GitHub Desktop.
Save andybak/997aabc2dbb826c7d089cda413c1617d to your computer and use it in GitHub Desktop.
Simple Playable Example
using UnityEngine;
using UnityEngine.Playables;
public class ToggleComponentAsset : PlayableAsset {
public ExposedReference<MonoBehaviour> Component;
public bool EnabledOnStart;
public bool EnabledOnEnd;
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) {
var playable = ScriptPlayable<ToggleComponentPlayable>.Create(graph);
var toggleComponentPlayable = playable.GetBehaviour();
var component = Component.Resolve(graph.GetResolver());
toggleComponentPlayable.Initialize(component, EnabledOnStart, EnabledOnEnd);
return playable;
}
}
using UnityEngine;
using UnityEngine.Playables;
public class ToggleComponentPlayable : PlayableBehaviour {
private MonoBehaviour _component;
private bool _enabledOnStart;
private bool _enabledOnEnd;
public void Initialize(MonoBehaviour component, bool enabledOnStart, bool enabledOnEnd) {
_component = component;
_enabledOnStart = enabledOnStart;
_enabledOnEnd = enabledOnEnd;
}
public override void OnBehaviourPlay(Playable playable, FrameData info) {
if (_component != null) {
_component.enabled = _enabledOnStart;
}
}
public override void OnBehaviourPause(Playable playable, FrameData info) {
if (_component != null) {
_component.enabled = _enabledOnEnd;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment