Enable GPU Instancing via Editor script

I generate grass via a custom script and need to enable GPU Instancing on all of them, unfortunately the inspector won’t let me multi edit and doing it manually for each one would take forever.

I looked at the Standard source and there doesn’t seem to be an exposed variable to modify.

Any ideas?

You can enable/disable GPU Instancing using material.enableInstancing.

Here is an example that provides the ability to toggle GPU Instancing of all materials that are located in a directory called “Assets/MyMaterials”.
Code example

// Save as Assets/Editor/ToggleGpuInstancing.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

class ToggleGpuInstancing
{
    [MenuItem("Example/Enable GPU Instancing")]
    static void EnableGpuInstancing()
    {
        SetGpuInstancing(true);
    }

    [MenuItem("Example/Disable GPU Instancing")]
    static void DisableGpuInstancing()
    {
        SetGpuInstancing(false);
    }

    static void SetGpuInstancing(bool value)
    {
        foreach (var guid in AssetDatabase.FindAssets("t:Material", new[] { "Assets/MyMaterials" }))
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var material = AssetDatabase.LoadAssetAtPath<Material>(path);
            if (material != null)
            {
                material.enableInstancing = value;
                EditorUtility.SetDirty(material);
            }
        }
    }
}
7 Likes

Thank you very much for your help!

1 Like