Converting Scripts in a Large Prefab Scene to Udon Sharp

I downloaded a popular free prefab scene from the Unity store with the intent of practicing optimization techniques. I planned to start with Unity’s occlusion culling, but noticed that the existing scripts for custom culling were relatively simple and could probably be converted to Udon Sharp, even with my rudimentary coding skills. The challenging part, however, is that the various scripts are attached to a huge number of objects in the scene. Most of these objects are not individual prefabs that I could edit at the root. Thus, adding an Udon Behavior component to each would be a daunting task. In a perfect world, I could push a button and a corresponding Udon Program Asset would be added to every object with a script. Then, it would just be a matter of editing the scripts. Unfortunately, I am unaware of such a button and wouldn’t know where to begin creating an editor extension to do something like that (assuming it’s even possible). Is there a simple solution I’m overlooking, or is this a fool’s errand?

You can multi-select objects and add a component to all of them at the same time.

You could also write an editor script to do this for you. Make sure you save and have a backup first. Something along the lines of:

[ExecuteInEditMode]
public class SwapComponent : MonoBehaviour
{
    public bool replace = false;
    void Update()
    {
        if (replace)
        {
            replace = false;
            foreach(var thing in FindObjectsOfType<TheScriptYouWantToReplace>())
            {
                thing.AddComponent<ThingYouWantToAdd>();
                DestroyImmediate(thing);
            }
        }
    }
}

@shinyscalesvr - Thanks! I’ll see if I can figure out how to make something work with your function, but I fear it might be tricky. I’m not just swapping scripts; I’m replacing scripts with Udon Sharp Program Assets that point to scripts. As previously mentioned, I’m pretty limited when it comes to coding, but am I correct in thinking that the variable, “replace,” in your script above needs to be “true” the first time a value is assigned?

I tried Multi-editing. I was able to add an Udon Behavior in the inspector to all the objects, but instead of showing the field in which I could drag the program asset, it said multi-object editing was not allowed. Next, I figured I’d try to add the component to one object and drag it to the others in the hierarchy window. I deleted the original script and added an Udon Behavior component. Even after turning off “auto-compile on modify” in the Udon Sharp project settings, however, I kept getting crashes when trying to drag in a new Udon Program Asset. I suspect I’ll need to remove all the scripts from the project before I can do anything, but then I’ll no longer be able to find references to them in the scene and figure out what goes where. Hopefully, I can make something work with your script, but either way, I greatly appreciate the advice.

One of the SDK toolkit features is to remove missing scripts in bulk, though if I understand correctly the scripts are there in your scene, you’re just replacing the C# with Udon# or Udon Graph equivalents?

I believe the “multi editing is not allowed” appears when the object is a child of an unpacked prefab, which can’t be edited.

I ran into the same thing with scripts I couldn’t delete on older unity projects, until I unpacked the offending prefab.

Just unselect the ones that trigger that notice, you’ll need to come back to them later. Or, unpack the prefabs first and then run your replacement script.

Yeah, I was hoping to replace the C# scripts with Udon# equivalents, but as I noted before, it’s not just a simple swap, since it entails adding an Udon Program Asset along with a C# script. I tried to come up with an editor script, following advice from shinyscalesvr and a couple others, but couldn’t get anything to work (probably a result of my own limitations). I eventually just deleted the scripts and used the bulk feature you mentioned to get rid of the script component on the remaining objects. I created an Udon Graph to turn interior objects on and off with box colliders/triggers. Ultimately, that’s probably a more optimized solution than the original script anyway. I also turned a bunch of props into prefabs with an LOD component for distance culling.

An addon to swap C# scripts for Udon# behaviors would be pretty cool, especially if it could add the necessary library references to the scripts, but the use cases might be too infrequent to make it worthwhile. I’m sure the scripts would rarely work out-of-the-box, and many would require a complete re-write to work with VRChat.

I’m sure a good number of people run into the same C# to U# challenges, and sounds like you handled it well.

Also, quite admirable you are considering LODs. Most of the worlds or assets I’ve acquired don’t take LODs into consideration. It might make more sense for larger worlds, but even small spaces can benefit. Another topic, surely :slight_smile: