Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

スクリーンショットを作成するボタン #604

Merged
merged 1 commit into from
Nov 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 69 additions & 4 deletions Assets/VRM/UniVRM/Editor/Meta/VRMMetaObjectEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEditor;
using UnityEngine;
using MeshUtility.M17N;
using System.IO;

namespace VRM
{
Expand Down Expand Up @@ -172,12 +173,25 @@ public override void OnInspectorGUI()
// texture
EditorGUILayout.BeginHorizontal();
{
// 左側
EditorGUILayout.BeginVertical();
GUI.enabled = false;
EditorGUILayout.PropertyField(m_exporterVersion);
GUI.enabled = true;
EditorGUILayout.PropertyField(m_thumbnail);
{
GUI.enabled = false;
EditorGUILayout.PropertyField(m_exporterVersion);
GUI.enabled = true;
EditorGUILayout.PropertyField(m_thumbnail);

if (Camera.main)
{
EditorGUILayout.HelpBox("Camera.main で画像を Render します。", MessageType.Info);
if (GUILayout.Button("スクリーンショット"))
{
TakeScreenShot();
}
}
}
EditorGUILayout.EndVertical();
// 右側
m_thumbnail.objectReferenceValue = TextureField("", (Texture2D)m_thumbnail.objectReferenceValue, 100);
}
EditorGUILayout.EndHorizontal();
Expand Down Expand Up @@ -218,6 +232,57 @@ public override void OnInspectorGUI()
serializedObject.ApplyModifiedProperties();
}

void TakeScreenShot()
{
var dst = SaveDialog(m_target, "png");
if (string.IsNullOrEmpty(dst))
{
return;
}

var backup = RenderTexture.active;
var backup2 = Camera.main.targetTexture;
var rt = new RenderTexture(1024, 1024, 16, RenderTextureFormat.ARGB32);
var tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
try
{
RenderTexture.active = rt;
Camera.main.targetTexture = rt;
Camera.main.Render();
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex.Apply();
File.WriteAllBytes(dst, tex.EncodeToJPG());
var assetPath = MeshUtility.UnityPath.FromFullpath(dst);
EditorApplication.delayCall += () =>
{
assetPath.ImportAsset();
m_target.Thumbnail = assetPath.LoadAsset<Texture2D>();
};
}
finally
{
RenderTexture.active = backup;
Camera.main.targetTexture = backup2;
Texture2D.DestroyImmediate(tex);
RenderTexture.DestroyImmediate(rt);
}
}

static string SaveDialog(VRMMetaObject meta, string ext)
{
var directory = Application.dataPath;
var assetPath = AssetDatabase.GetAssetPath(meta);
if (!string.IsNullOrEmpty(assetPath))
{
directory = Path.Combine(directory + "/..", Path.GetDirectoryName(assetPath));
}
return EditorUtility.SaveFilePanel(
"Save thumbnail",
directory,
$"thumbnail.{ext}",
ext);
}

static (Rect, Rect) FixedRight(Rect r, int width)
{
if (width > r.width)
Expand Down