vrchat-community / UdonSharp

A compiler for compiling C# to Udon assembly
https://udonsharp.docs.vrchat.com
MIT License
470 stars 50 forks source link

Proxy function to handle prefab modifications #23

Closed BocuD closed 2 years ago

BocuD commented 2 years ago

Feature Description: This would be a function on the base UdonSharpBehaviour class that checks if the gameobject the target behaviour is on is part of any prefab, and if yes, records prefab instance modifications for the behaviour. It could speed up adding proper prefab support to custom inspectors and editor scripts.

Additional context: For example something like this:

public void HandlePrefabModifications()
{
    if (PrefabUtility.IsPartOfAnyPrefab(gameObject))
    {
        EditorUtility.SetDirty(this);
        PrefabUtility.RecordPrefabInstancePropertyModifications(this);
    }
}

In the custom inspector, you could then simply do

EditorGUI.BeginChangeCheck();
someBehaviour.someStringProperty = EditorGUILayout.TextField("String property: ", someBehaviour.stringProperty);
if (EditorGUI.EndChangeCheck())
{
    someBehaviour.HandlePrefabModifications();
}
BocuD commented 2 years ago

Most of this would be made redundant by using SerializedProperties in custom inspectors. When automating component changes without using SerializedProperties marking the object as dirty and then calling PrefabUtility.RecordPrefabInstancePropertyModifications(behaviour); will take care of this.

MerlinVR commented 2 years ago

Discussed on Discord for context: https://discord.com/channels/652715801714884620/690799234991259708/956270013835255808