Files
monde-usage-unity/Assets/Scripts/Editor/VideoAssetGenerator.cs
2024-09-22 21:45:37 +02:00

109 lines
3.4 KiBLFS
C#

using System.IO;
using PlasticPipe.PlasticProtocol.Messages.Serialization;
using UnityEditor;
using UnityEngine;
using UnityEngine.Video;
public class VideoAssetGenerator : EditorWindow
{
private static readonly string videoFolderPath = "Assets/Videos";
private static readonly string videoAssetFolderPath = "Assets/VideoAssets";
private static readonly string rootStorageUrl = "gs://monde-en-carton.appspot.com/Videos/";
[MenuItem("Tools/Generate VideoAssets")]
public static void GenerateVideoAssets()
{
if (!Directory.Exists(videoFolderPath))
{
Debug.LogError($"Video folder '{videoFolderPath}' does not exist.");
return;
}
// Ensure the VideoAssets folder exists
if (!Directory.Exists(videoAssetFolderPath))
{
Directory.CreateDirectory(videoAssetFolderPath);
AssetDatabase.Refresh();
}
Debug.Log("Generating VideoAssets...");
// Recursively scan and create ScriptableObjects
ProcessDirectory(videoFolderPath);
Debug.Log("VideoAssets generated successfully.");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
private static void ProcessDirectory(string directory)
{
// Get all files and subdirectories
string[] files = Directory.GetFiles(directory, "*.mp4");
string[] subdirectories = Directory.GetDirectories(directory);
// Create VideoAsset for each full-resolution file
foreach (string filePath in files)
{
// Skip half-resolution files
if (filePath.Contains("_halfres.mp4"))
continue;
CreateVideoAsset(filePath);
}
// Recurse through each subdirectory
foreach (string subdirectory in subdirectories)
{
ProcessDirectory(subdirectory);
}
}
private static void CreateVideoAsset(string videoFilePath)
{
Debug.Log($"Creating VideoAsset for: {videoFilePath}");
string relativePath = videoFilePath.Replace(Application.dataPath, "Assets");
Debug.Log($"Relative path: {relativePath}");
Debug.Log($"Video file path: {videoFilePath}");
Debug.Log($"Root storage URL: {rootStorageUrl}");
Debug.Log($"Video asset folder path: {videoAssetFolderPath}");
// Calculate the corresponding VideoAsset folder path
string assetFolder = videoAssetFolderPath + Path.GetDirectoryName(relativePath).Replace("Assets/Videos", "");
Debug.Log($"Asset folder: {assetFolder}");
if (!Directory.Exists(assetFolder))
{
Directory.CreateDirectory(assetFolder);
}
// Create VideoAsset ScriptableObject
string fileName = Path.GetFileNameWithoutExtension(videoFilePath);
string videoAssetPath = Path.Combine(assetFolder, fileName + ".asset");
// Check if asset already exists
if (File.Exists(videoAssetPath))
{
Debug.Log($"VideoAsset already exists for: {videoFilePath}");
return;
}
// Create new VideoAsset instance
VideoAsset videoAsset = CreateInstance<VideoAsset>();
// Assign the storage URL (Fullres)
string storageUrl = rootStorageUrl + relativePath.Replace("Assets/Videos/", "").Replace("\\", "/");
videoAsset.storageUrl = storageUrl;
videoAsset.videoClip = (VideoClip)AssetDatabase.LoadAssetAtPath(relativePath, typeof(VideoClip));
videoAsset.isHalfRes = false;
// Save the asset to the correct location
AssetDatabase.CreateAsset(videoAsset, videoAssetPath);
Debug.Log($"Created VideoAsset for: {videoFilePath}");
}
}