You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.9 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class PlayerPreviewEditorBase : Editor
{
protected virtual DShowPlayer player { get { return null; } }
protected virtual DShowClip clip { get { return null; } }
public override bool HasPreviewGUI()
{
return true;
}
public override bool RequiresConstantRepaint()
{
return player.IsPlaying;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
}
public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
{
checkPlayer();
player.updateTexture();
DrawFrameSlider();
if ((target as MonoBehaviour))
{
if ((target as MonoBehaviour).GetComponent<RawImage>() != null)
{
(target as MonoBehaviour).GetComponent<RawImage>().texture = player.Texture;
}
if ((target as MonoBehaviour).GetComponent<Image>() != null)
{
(target as MonoBehaviour).GetComponent<Image>().material.mainTexture = player.Texture;
}
}
Texture texture = player.Texture;
if (texture == null)
{
EditorGUILayout.HelpBox("texture is null, something whent wrong...", MessageType.Info);
}
else
{
float x, y, w, h;
float tex_aspect = (float)texture.width / texture.height;
float aspect = (float)r.width / r.height;
if (tex_aspect > aspect)
{
w = r.width;
h = w / tex_aspect;
}
else
{
h = r.height;
w = h * tex_aspect;
}
x = 0.5f * (r.width - w);
y = 0.5f * (r.height - h);
r.x += x;
r.y += y;
r.width = w;
r.height = h;
r.y += r.height;
r.height = -r.height;
GUI.DrawTexture(r, texture);
}
}
public override void OnPreviewSettings()
{
//DrawFrameSlider();
}
private void DrawFrameSlider()
{
EditorGUILayout.BeginHorizontal();
GUIStyle previewButtonSettingsStyle = new GUIStyle("preButton");
{
var playButtonContent = EditorGUIUtility.IconContent("PlayButton");
bool newPlaying = GUILayout.Toggle(player.IsPlaying, playButtonContent, previewButtonSettingsStyle, GUILayout.ExpandWidth(false));
if (player.IsPlaying != newPlaying)
{
if (newPlaying)
player.Play();
else
player.Pause();
GUI.FocusControl(null);
}
}
//EditorGUILayout.LabelField("Frame:");
var prev = EditorGUIUtility.IconContent("Animation.PrevKey");
var next = EditorGUIUtility.IconContent("Animation.NextKey");
if (GUILayout.Button(prev, previewButtonSettingsStyle, GUILayout.ExpandWidth(false)))
{
player.Frame--;
GUI.FocusControl(null);
}
uint currentFrame = player.Frame;
if (currentFrame != uint.MaxValue)
{
int newFrame = EditorGUILayout.IntSlider((int)currentFrame, 0, (int)player.TotalNumFrames, GUILayout.ExpandWidth(true));
if (newFrame != currentFrame)
{
player.Frame = (uint)newFrame;
}
}
if (GUILayout.Button(next, previewButtonSettingsStyle, GUILayout.ExpandWidth(false)))
{
player.Frame++;
GUI.FocusControl(null);
}
EditorGUILayout.EndHorizontal();
}
private void checkPlayer()
{
if (player.IsLoaded == false || player.FilePath != clip.fullPath)
{
player.Load(clip);
player.Play();
player.Pause();
}
}
}