Files
Main/Assets/Plugins/ExternalLibs/CinematicEditor/CinematicKeyframeEditor.cs

127 lines
4.0 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Thousandto.Cinematic.Plugin
{
public class CinematicKeyframeEditor : MonoBehaviour
{
private static GameObject _keyframeEditorRoot;
public static GameObject KeyframeEditorRoot
{
get
{
if(_keyframeEditorRoot == null)
{
_keyframeEditorRoot = new GameObject("KeyframeEditor");
_keyframeEditorRoot.transform.parent = CinematicManager.RootGo.transform;
}
return _keyframeEditorRoot;
}
}
public CinematicKeyframe KeyframeData;
[NonSerialized]
public CinematicEventData TransData;
//游戏运行时对象
public CinematicObj RuntimeGameGo;
public Action<CinematicObj, CinematicKeyframe> OnSelectAction;
public void Initialize(CinematicObj runtimeGo, CinematicKeyframe frameData)
{
RuntimeGameGo = runtimeGo;
KeyframeData = frameData;
frameData.RefreshCallback = OnRefresh;
var root = KeyframeEditorRoot.transform.Find(runtimeGo.GameObject.name);
if(root == null)
{
GameObject go = new GameObject(runtimeGo.GameObject.name);
go.transform.parent = KeyframeEditorRoot.transform;
root = go.transform;
}
transform.parent = root;
OnRefresh(false);
}
public void OnRefresh(bool destroy)
{
//消耗
if(destroy)
{
DestroyImmediate(gameObject);
return;
}
TransData = null;
for (int i = 0; i < KeyframeData.EventData.Count; ++i)
{
var eventData = KeyframeData.EventData[i];
if (eventData.EventTypeEx == KeyFrameEvent.)
{
transform.position = eventData.Position;
transform.rotation = Quaternion.Euler(eventData.Rotation);
transform.localScale = eventData.Scale;
TransData = eventData;
}
}
if(RuntimeGameGo != null)
{
gameObject.name = RuntimeGameGo.GameObject.name + "_" + KeyframeData.Keyframe;
}
}
public void OnSelected()
{
if(OnSelectAction != null)
{
OnSelectAction(RuntimeGameGo, KeyframeData);
}
}
#if UNITY_EDITOR
void OnDrawGizmos()
{
if(TransData != null)
{
Gizmos.color = Color.green;
Gizmos.DrawCube(transform.position, Vector3.one * 0.1f);
UnityEditor.Handles.Label(transform.position + new Vector3(0, 1, 0), gameObject.name);
bool needRefreshCurve = TransData.Position != transform.localPosition ||
TransData.Rotation != transform.rotation.eulerAngles||
TransData.Scale != transform.localScale;
if (TransData.Position != transform.localPosition)
{
TransData.Position = transform.localPosition;
RuntimeGameGo.ClearSelectState();
}
if (TransData.Rotation != transform.rotation.eulerAngles)
{
TransData.Rotation = transform.rotation.eulerAngles;
RuntimeGameGo.ClearSelectState();
}
if (TransData.Scale != transform.localScale)
{
TransData.Scale = transform.localScale;
RuntimeGameGo.ClearSelectState();
}
//刷新editor中展示的曲线
if (needRefreshCurve && RuntimeGameGo != null && RuntimeGameGo.RefreshAction != null)
RuntimeGameGo.RefreshAction();
}
}
#endif
}
}