Unity Bundle Loader

mail@pastecode.io avatar
unknown
csharp
6 months ago
2.0 kB
120
Indexable
Never
using System.Collections;
using System.IO;
using UnityEngine;

public class AssetBundleLoader : MonoBehaviour
{
    private string bundleFolderPath = "D:/UnityProjects/SDK/EscapeFromTarkov-SDK/Assets/BundlesToLoad";

    void Start()
    {
        LoadAssetBundles();
    }

    void LoadAssetBundles()
    {
        string[] bundlePaths = Directory.GetFiles(bundleFolderPath, "*.bundle");
        foreach (string bundlePath in bundlePaths)
        {
            LoadAssetBundle(bundlePath);
        }
    }

    void LoadAssetBundle(string path)
    {
        path = path.Replace("\\", "/");
        AssetBundle assetBundle = AssetBundle.LoadFromFile(path);
        if (assetBundle == null)
        {
            Debug.LogError("Failed to load asset bundle at path: " + path);
            return;
        }
        Object[] assets = assetBundle.LoadAllAssets();

        foreach (Object asset in assets)
        {
            if (asset is GameObject)
            {
                GameObject instantiatedObject = Instantiate((GameObject)asset);
                MeshRenderer[] meshRenderers = instantiatedObject.GetComponentsInChildren<MeshRenderer>();
                foreach (MeshRenderer renderer in meshRenderers)
                {
                    ChangeShader(renderer);
                }
                SkinnedMeshRenderer[] skinnedMeshRenderers = instantiatedObject.GetComponentsInChildren<SkinnedMeshRenderer>();
                foreach (SkinnedMeshRenderer renderer in skinnedMeshRenderers)
                {
                    ChangeShader(renderer);
                }
            }
        }
        assetBundle.Unload(false);
    }

    void ChangeShader(Renderer renderer)
    {
        if (renderer.material != null)
        {
             Shader newShader = Shader.Find("p0/Reflective/Bumped Specular SMap");
            if (newShader != null)
            {
                renderer.material.shader = newShader;
            }
        }
    }
}
Leave a Comment