|
| 1 | +#if AT_USE_PENCILLINE |
| 2 | + |
| 3 | +using System.Collections; |
| 4 | +using System.Collections.Generic; |
| 5 | +using Pencil_4; |
| 6 | +using Unity.EditorCoroutines.Editor; |
| 7 | +using Unity.FilmInternalUtilities; //Required when using Timeline 1.4.x or below |
| 8 | +using Unity.FilmInternalUtilities.Editor; |
| 9 | +using Unity.StreamingImageSequence; |
| 10 | +using Unity.StreamingImageSequence.Editor; |
| 11 | +using UnityEditor; |
| 12 | +using UnityEditor.Timeline; |
| 13 | +using UnityEngine; |
| 14 | +using UnityEngine.Assertions; |
| 15 | +using UnityEngine.Playables; |
| 16 | +using UnityEngine.Timeline; |
| 17 | + |
| 18 | +[CustomEditor(typeof(PencilLineCacheUpdater))] |
| 19 | +[CanEditMultipleObjects] |
| 20 | +internal class PencilLineCacheUpdaterInspector : Editor { |
| 21 | + void OnEnable() { |
| 22 | + m_lineCacheUpdater = this.target as PencilLineCacheUpdater; |
| 23 | + Assert.IsNotNull(m_lineCacheUpdater); |
| 24 | + } |
| 25 | + |
| 26 | + public override void OnInspectorGUI() { |
| 27 | + EditorGUIDrawerUtility.DrawUndoableGUI(m_lineCacheUpdater, "PencilLineCacheUpdater", |
| 28 | + guiFunc: () => { |
| 29 | + return EditorGUILayout.ObjectField("Director", m_lineCacheUpdater.GetPlayableDirector(), |
| 30 | + typeof(PlayableDirector), allowSceneObjects: true) as PlayableDirector; |
| 31 | + }, |
| 32 | + updateFunc: (PlayableDirector director) => { m_lineCacheUpdater.SetPlayableDirector(director); } |
| 33 | + ); |
| 34 | + |
| 35 | + PlayableDirector director = m_lineCacheUpdater.GetPlayableDirector(); |
| 36 | + if (null == director) { |
| 37 | + EditorGUILayout.HelpBox("Please assign a PlayableDirector to browse RenderCache assets.", MessageType.Warning); |
| 38 | + } |
| 39 | + |
| 40 | + DrawRenderCachePlayableAssetGUI(director); |
| 41 | + |
| 42 | + RenderCachePlayableAsset renderCachePlayableAsset = m_lineCacheUpdater.GetRenderCachePlayableAsset(); |
| 43 | + if (null == renderCachePlayableAsset) { |
| 44 | + EditorGUILayout.HelpBox("Click the browse button and assign a RenderCachePlayableAsset.", MessageType.Warning); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + DrawDefaultInspector(); |
| 49 | + GUILayout.Space(30); |
| 50 | + |
| 51 | + bool canUpdate = null != director && null != renderCachePlayableAsset; |
| 52 | + EditorGUI.BeginDisabledGroup(!canUpdate); |
| 53 | + if (GUILayout.Button("Update Pencil Line Cache")) { |
| 54 | + UpdatePencilLineCache(director, renderCachePlayableAsset); |
| 55 | + } |
| 56 | + |
| 57 | + EditorGUI.EndDisabledGroup(); |
| 58 | + } |
| 59 | + |
| 60 | + void DrawRenderCachePlayableAssetGUI(PlayableDirector director) { |
| 61 | + const int BUTTON_WIDTH = 30; |
| 62 | + |
| 63 | + Rect rect = EditorGUILayout.GetControlRect(); |
| 64 | + rect.width -= BUTTON_WIDTH; |
| 65 | + EditorGUI.ObjectField(rect, "Render Cache Playable Asset", m_lineCacheUpdater.GetRenderCachePlayableAsset(), |
| 66 | + typeof(RenderCachePlayableAsset), allowSceneObjects: false); |
| 67 | + |
| 68 | + |
| 69 | + Rect buttonRect = rect; |
| 70 | + buttonRect.x = rect.x + rect.width; |
| 71 | + buttonRect.width = BUTTON_WIDTH; |
| 72 | + |
| 73 | + if (null == director) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (!GUI.Button(buttonRect, "..")) |
| 78 | + return; |
| 79 | + |
| 80 | + // Show the dropdown under the field only, but not the label |
| 81 | + Rect fieldRect = rect; |
| 82 | + fieldRect.xMin += EditorGUIUtility.labelWidth; |
| 83 | + |
| 84 | + if (null == director.playableAsset) { |
| 85 | + EditorUtility.DisplayDialog(SISEditorConstants.DIALOG_TITLE, ASSIGN_TIMELINE_ASSET_MSG, "OK"); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + TimelineAsset timelineAsset = director.playableAsset as TimelineAsset; |
| 90 | + RenderCachePlayableAssetPopup.Show(fieldRect, new Vector2(fieldRect.width, 80), timelineAsset, OnClipSelected); |
| 91 | + } |
| 92 | + |
| 93 | + void OnClipSelected(TimelineClip clip) { |
| 94 | + m_lineCacheUpdater.SetRenderCachePlayableAsset(null == clip ? null : clip.asset as RenderCachePlayableAsset); |
| 95 | + } |
| 96 | + |
| 97 | +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 98 | + |
| 99 | + void UpdatePencilLineCache(PlayableDirector director, RenderCachePlayableAsset renderCachePlayableAsset) { |
| 100 | + Assert.IsNotNull(director); |
| 101 | + Assert.IsNotNull(renderCachePlayableAsset); |
| 102 | + |
| 103 | + TimelineAsset timelineAsset = renderCachePlayableAsset.GetBoundClipData()?.GetOwner().GetParentTrack().timelineAsset; |
| 104 | + if (null == timelineAsset || timelineAsset != director.playableAsset) { |
| 105 | + EditorUtility.DisplayDialog(SISEditorConstants.DIALOG_TITLE, "The RenderCachePlayableAsset does not exist in the specified Director object", "OK"); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + EditorCoroutineUtility.StartCoroutineOwnerless(UpdatePencilLineCacheCoroutine(director, renderCachePlayableAsset)); |
| 110 | + } |
| 111 | + |
| 112 | + private IEnumerator UpdatePencilLineCacheCoroutine(PlayableDirector director, RenderCachePlayableAsset renderCachePlayableAsset) { |
| 113 | + TimelineEditorUtility.SelectDirectorInTimelineWindow(director); //Need to show in TimelineWindow |
| 114 | + yield return null; |
| 115 | + |
| 116 | + if (TimelineEditor.inspectedAsset != director.playableAsset) { |
| 117 | + EditorUtility.DisplayDialog(SISEditorConstants.DIALOG_TITLE, "Can't show the specified Director in the Timeline Window. Please unlock it if it's locked.", "OK"); |
| 118 | + yield break; |
| 119 | + } |
| 120 | + |
| 121 | + if (m_lineCacheUpdater.IsUpdateSeparately()) { |
| 122 | + yield return UpdateSeparatePencilLineCacheCoroutine(director, renderCachePlayableAsset); |
| 123 | + } |
| 124 | + else { |
| 125 | + yield return RenderCachePlayableAssetInspector.UpdateRenderCacheCoroutine(director, renderCachePlayableAsset); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + private IEnumerator UpdateSeparatePencilLineCacheCoroutine(PlayableDirector director, |
| 131 | + RenderCachePlayableAsset playableAsset) { |
| 132 | + Dictionary<LineNode, bool> origLineNodeEnabledDic = new Dictionary<LineNode, bool>(); |
| 133 | + string origFolder = playableAsset.GetFolder(); |
| 134 | + |
| 135 | + |
| 136 | + foreach (PencilLineCacheInfo info in m_lineCacheUpdater.EnumerateSeparateCacheInfo()) { |
| 137 | + origLineNodeEnabledDic[info.lineNode] = info.lineNode.enabled; |
| 138 | + info.lineNode.enabled = false; |
| 139 | + } |
| 140 | + |
| 141 | + //disable all lineNodes |
| 142 | + foreach (PencilLineCacheInfo info in m_lineCacheUpdater.EnumerateSeparateCacheInfo()) { |
| 143 | + info.lineNode.enabled = false; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + foreach (PencilLineCacheInfo info in m_lineCacheUpdater.EnumerateSeparateCacheInfo()) { |
| 148 | + info.lineNode.enabled = true; |
| 149 | + playableAsset.SetFolder(info.cacheFolder); |
| 150 | + IEnumerator it = RenderCachePlayableAssetInspector.UpdateRenderCacheCoroutine(director, playableAsset); |
| 151 | + while (it.MoveNext()) { |
| 152 | + yield return null; |
| 153 | + } |
| 154 | + |
| 155 | + info.lineNode.enabled = false; |
| 156 | + yield return null; |
| 157 | + } |
| 158 | + |
| 159 | + //return original values |
| 160 | + foreach (PencilLineCacheInfo info in m_lineCacheUpdater.EnumerateSeparateCacheInfo()) { |
| 161 | + info.lineNode.enabled = origLineNodeEnabledDic[info.lineNode]; |
| 162 | + } |
| 163 | + |
| 164 | + playableAsset.SetFolder(origFolder); |
| 165 | + } |
| 166 | + |
| 167 | +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 168 | + |
| 169 | + private PencilLineCacheUpdater m_lineCacheUpdater = null; |
| 170 | + |
| 171 | + private const string ASSIGN_TIMELINE_ASSET_MSG = "Please assign a TimelineAsset to the PlayableDirector."; |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +#endif //AT_USE_PENCILLINE |
0 commit comments