training-academy/UNITY_SETUP_GUIDE.md

8.2 KiB

Unity Setup & Prototype Guide - Phase 1

🎯 Goal: Validate Unity Video Performance vs Web


📋 Step 1: Unity Project Setup

Create New Project:

  1. Open Unity Hub
  2. New Project3D Core template
  3. Project Name: Gooner-Training-Academy-Unity
  4. Unity Version: 2022.3 LTS (recommended)
  5. Location: Choose appropriate folder

Essential Packages to Install:

  1. WindowPackage Manager
  2. Install these packages:
    • TextMeshPro (for UI text)
    • Video Player (should be built-in)
    • UI Toolkit (modern UI system)
    • Newtonsoft Json (for data serialization)

🎬 Step 2: Basic Video Player Test

Create Basic Scene:

  1. Create Empty GameObject → Name: VideoManager
  2. Add ComponentVideo Player
  3. Create UI CanvasScreen Space - Overlay
  4. Add Button to Canvas → Name: PlayButton

Test Single Video:

// Create: Assets/Scripts/BasicVideoTest.cs
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;

public class BasicVideoTest : MonoBehaviour 
{
    [Header("Video Settings")]
    public VideoPlayer videoPlayer;
    public Button playButton;
    public RenderTexture renderTexture;
    
    void Start() 
    {
        // Setup render texture for video output
        renderTexture = new RenderTexture(1920, 1080, 24);
        videoPlayer.targetTexture = renderTexture;
        
        // Setup button
        playButton.onClick.AddListener(ToggleVideo);
        
        // Load test video (you'll need to provide a video file)
        videoPlayer.url = System.IO.Path.Combine(Application.streamingAssetsPath, "test-video.mp4");
        videoPlayer.Prepare();
    }
    
    void ToggleVideo() 
    {
        if (videoPlayer.isPlaying) 
        {
            videoPlayer.Pause();
            playButton.GetComponentInChildren<Text>().text = "Play";
        } 
        else 
        {
            videoPlayer.Play();
            playButton.GetComponentInChildren<Text>().text = "Pause";
        }
    }
}

🎮 Step 3: Quad Video Prototype

Create Quad Layout:

  1. Create 4 UI Panels in Canvas (2x2 grid layout)
  2. Add RawImage to each panel
  3. Create 4 VideoPlayer GameObjects

Quad Video Manager:

// Create: Assets/Scripts/QuadVideoManager.cs
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using System.Collections.Generic;

public class QuadVideoManager : MonoBehaviour 
{
    [Header("Video Players")]
    public VideoPlayer[] videoPlayers = new VideoPlayer[4];
    
    [Header("UI Elements")]
    public RawImage[] videoDisplays = new RawImage[4];
    public Button[] playButtons = new Button[4];
    public Slider[] volumeSliders = new Slider[4];
    
    [Header("Video Settings")]
    public RenderTexture[] renderTextures = new RenderTexture[4];
    public string[] testVideoUrls = new string[4];
    
    void Start() 
    {
        SetupQuadVideos();
    }
    
    void SetupQuadVideos() 
    {
        for (int i = 0; i < 4; i++) 
        {
            // Create render texture for each video
            renderTextures[i] = new RenderTexture(1920, 1080, 24);
            
            // Setup video player
            videoPlayers[i].targetTexture = renderTextures[i];
            videoPlayers[i].isLooping = true;
            videoPlayers[i].audioOutputMode = VideoAudioOutputMode.Direct;
            
            // Assign render texture to UI
            videoDisplays[i].texture = renderTextures[i];
            
            // Setup controls
            int index = i; // Capture for closure
            playButtons[i].onClick.AddListener(() => ToggleVideo(index));
            volumeSliders[i].onValueChanged.AddListener((value) => SetVolume(index, value));
            
            // Load test video
            if (i < testVideoUrls.Length && !string.IsNullOrEmpty(testVideoUrls[i])) 
            {
                videoPlayers[i].url = testVideoUrls[i];
                videoPlayers[i].Prepare();
            }
        }
    }
    
    void ToggleVideo(int index) 
    {
        if (videoPlayers[index].isPlaying) 
        {
            videoPlayers[index].Pause();
        } 
        else 
        {
            videoPlayers[index].Play();
        }
    }
    
    void SetVolume(int index, float volume) 
    {
        videoPlayers[index].SetDirectAudioVolume(0, volume);
    }
    
    void Update() 
    {
        // Monitor performance
        if (Input.GetKeyDown(KeyCode.P)) 
        {
            Debug.Log($"FPS: {1f / Time.deltaTime:F1}");
            Debug.Log($"Videos playing: {GetPlayingVideoCount()}");
        }
    }
    
    int GetPlayingVideoCount() 
    {
        int count = 0;
        for (int i = 0; i < videoPlayers.Length; i++) 
        {
            if (videoPlayers[i].isPlaying) count++;
        }
        return count;
    }
}

📁 Step 4: Setup Test Videos

Create StreamingAssets Folder:

  1. AssetsCreateFolder → Name: StreamingAssets
  2. Copy 4 test videos into this folder (MP4 format recommended)
  3. Name them: test-video-1.mp4, test-video-2.mp4, etc.

Update Video URLs:

// In QuadVideoManager, set testVideoUrls in inspector:
testVideoUrls[0] = Application.streamingAssetsPath + "/test-video-1.mp4";
testVideoUrls[1] = Application.streamingAssetsPath + "/test-video-2.mp4";
testVideoUrls[2] = Application.streamingAssetsPath + "/test-video-3.mp4";
testVideoUrls[3] = Application.streamingAssetsPath + "/test-video-4.mp4";

🧪 Step 5: Performance Testing

Performance Monitor Script:

// Create: Assets/Scripts/PerformanceMonitor.cs
using UnityEngine;
using UnityEngine.UI;

public class PerformanceMonitor : MonoBehaviour 
{
    [Header("UI")]
    public Text fpsText;
    public Text memoryText;
    public Text videoCountText;
    
    private float deltaTime = 0.0f;
    private QuadVideoManager quadManager;
    
    void Start() 
    {
        quadManager = FindObjectOfType<QuadVideoManager>();
    }
    
    void Update() 
    {
        deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
        
        if (fpsText != null) 
        {
            float fps = 1.0f / deltaTime;
            fpsText.text = $"FPS: {fps:0.}";
            
            // Color code based on performance
            if (fps >= 60) fpsText.color = Color.green;
            else if (fps >= 30) fpsText.color = Color.yellow;
            else fpsText.color = Color.red;
        }
        
        if (memoryText != null) 
        {
            long memory = System.GC.GetTotalMemory(false);
            memoryText.text = $"Memory: {memory / 1024 / 1024}MB";
        }
        
        if (videoCountText != null && quadManager != null) 
        {
            videoCountText.text = $"Playing: {quadManager.GetPlayingVideoCount()}/4";
        }
    }
}

🎯 Step 6: Test Plan

Performance Benchmarks:

  1. Baseline Test: Single video @ 1080p
  2. Dual Test: 2 videos simultaneously
  3. Quad Test: 4 videos simultaneously
  4. Stress Test: 4 videos + UI interactions

Metrics to Track:

  • FPS: Target 60+ with 4 videos
  • Memory Usage: Target <2GB
  • CPU Usage: Monitor in Task Manager
  • Video Switching Speed: Target <100ms

Comparison to Web:

  • Run your web QuadVideoPlayer alongside Unity
  • Monitor both performance profiles
  • Document frame rate differences

📋 Your Immediate Tasks:

Today:

  1. Create Unity project with packages
  2. Implement BasicVideoTest script
  3. Test single video playback

This Week:

  1. Build QuadVideoManager system
  2. Add performance monitoring
  3. Compare web vs Unity performance
  4. Document results for migration decision

🚀 Expected Results:

If Unity performs significantly better:

  • Proceed with full migration
  • Start porting game logic systems
  • Plan Unity-specific enhancements

If performance is similar:

  • Re-evaluate web optimizations
  • Consider hybrid approach
  • Focus on Unity for future features

Ready to start? Begin with the Unity project setup and let me know when you have the basic project created! 🎮