Skip to content
Om Puter

Om Puter

Berbagi Tutorial Coding dan Pemrograman Komputer

Menu
  • Channel YouTube ThirteeNov
  • Channel YouTube Om Puter
Menu

Script C# untuk mengetahui 20 file paling besar yang digunakan di project game Unity kamu

Posted on 15 Mei 202515 Mei 2025 by OmPuter

Saat membuat game di Unity terkadang kamu menemui kendala bahwa game yang kamu build ukurannya terlalu besar. Nah tandanya kamu perlu optimasi ukuran file-file aset yang kamu gunakan di projectmu. Masalahnya, kalau mau cek satu per satu file dan folder yang ada di dalam project kamu pasti akan kecapean. Nah, script unity ini berfungsi untuk membuat list otomatis 20 file terbesar yang dipakai dalam project kamu. Nanti daftar filenya muncul di jendela console. Cara pakainya, cukup masukkan script ini ke project Unity, dan nanti akan ada menu tool dan klik saja Find Referenced Large Assets di dalamnya untuk menjalankan script.

Ini dia scriptnya:

using UnityEditor;
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using System.IO;

public class FindReferencedLargeAssets : EditorWindow
{
    [MenuItem("Tools/Find Referenced Large Assets")]
    public static void ShowWindow()
    {
        // Get all referenced assets
        HashSet<string> referencedPaths = GetReferencedAssetPaths();

        // Filter large assets (>1MB) and sort by size
        var largeReferencedAssets = AssetDatabase.GetAllAssetPaths()
            .Where(path => 
                !path.StartsWith("Assets/Editor") && 
                !path.StartsWith("Assets/StreamingAssets") &&
                referencedPaths.Contains(path) // Only referenced assets
            )
            .Select(path => new 
            {
                Path = path,
                Size = (new FileInfo(path)).Length / 1024 / 1024f // Size in MB
            })
            .Where(asset => asset.Size > 1) // Show files >1MB
            .OrderByDescending(asset => asset.Size)
            .Take(20); // Top 20 largest referenced files

        // Log results
        Debug.Log("=== LARGEST REFERENCED ASSETS (USED IN GAME) ===");
        foreach (var asset in largeReferencedAssets)
        {
            Debug.Log($"{asset.Path} - {asset.Size:F2} MB");
        }
    }

    // Get all asset paths referenced in scenes, prefabs, or Resources
    private static HashSet<string> GetReferencedAssetPaths()
    {
        HashSet<string> referencedPaths = new HashSet<string>();

        // 1. Check all scenes and prefabs
        string[] allScenePaths = AssetDatabase.FindAssets("t:Scene")
            .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
            .ToArray();
        
        string[] allPrefabPaths = AssetDatabase.FindAssets("t:Prefab")
            .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
            .ToArray();

        // 2. Find dependencies (recursively)
        foreach (string path in allScenePaths.Concat(allPrefabPaths))
        {
            string[] dependencies = AssetDatabase.GetDependencies(path, recursive: true);
            foreach (string dependency in dependencies)
            {
                referencedPaths.Add(dependency);
            }
        }

        // 3. Include everything in "Resources" folders (since they can be loaded dynamically)
        string[] resourcesAssets = AssetDatabase.FindAssets("", new[] { "Assets/Resources" })
            .Select(guid => AssetDatabase.GUIDToAssetPath(guid))
            .ToArray();
        
        foreach (string resourcePath in resourcesAssets)
        {
            referencedPaths.Add(resourcePath);
        }

        return referencedPaths;
    }
}

Pastikan script ini ada di dalam folder Editor di project kamu. Kamu perlu buat folder Editor jika tidak ada.

Post Views: 300

Kategori

  • 3D Max
  • Adobe Animate
  • Android
  • c#
  • Cordova
  • HTML5, CSS & JavaScript
  • iOS
  • Lain-lain
  • Photoshop
  • PHP
  • Python
  • Roblox
  • Tak Berkategori
  • Unity
  • WordPress
ciihuy2020
© 2026 Om Puter | Powered by Superbs Personal Blog theme