Search Unity

Creating a PlayableAsset from Code

Discussion in 'Timeline' started by Redhook_Galen, Apr 3, 2019.

  1. Redhook_Galen

    Redhook_Galen

    Joined:
    Nov 21, 2018
    Posts:
    11
    I've been writing a tool to help with authoring and previewing Timeline Sequences. I wanted to add a quick way of creating a PlayableAsset for blank fields so I added a button using a Custom Property Drawer.

    The issue is that despite PlayableAsset extending from ScriptableObject I can't seem to instantiate it like other Scriptable Objects.

    Code (CSharp):
    1. string filePath = EditorUtility.SaveFilePanelInProject("Create Playable Asset", property.serializedObject.targetObject.name, "playable", "Where do you want to create this timeline playable?", "Assets/Data/Playables");
    2. PlayableAsset newPlayableAsset = ScriptableObject.CreateInstance<PlayableAsset>();
    3. string fileName = Path.GetFileNameWithoutExtension(filePath);
    4. newPlayableAsset.name = fileName;
    5. AssetDatabase.CreateAsset(newPlayableAsset, filePath);
    Normally this would instantiate a ScriptableObject in memory and then save it to the filepath but the result of ScriptableObject.CreateInstance is null. Is there a special API for creating these assets?
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    PlayableAsset is abstract, you can create a derived class of it, but not a PlayableAsset itself.
     
  3. Redhook_Galen

    Redhook_Galen

    Joined:
    Nov 21, 2018
    Posts:
    11
    Ah I see. Is TimelineAsset the one I want that would be the same as the Create menu?

    Edit: Yeah TimelineAsset was the one I needed.
     
    Last edited: Apr 4, 2019