Skip to content

Code + examples for Refactoring Data stored in Unity Prefabs, Scenes and other Assets

License

Notifications You must be signed in to change notification settings

acoppes/unity-refactoring-tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openupm License: MIT

Unity Refactor Tools

Tools for refactoring content stored in Unity Prefabs, Scenes and other Assets.

This project was initially used to show examples for this blogpost.

Install it from OpenUPM

This package can be installed using OpenUPM, just click here openupm for more details.

Usage

If you want, for example, copy some MonoBehaviour fields to new field and consider all prefabs and prefab instances in scene, then run something like this:

RefactorTools.RefactorMonoBehaviour<CustomBehaviour>(new RefactorTools.RefactorParameters
{
    prefabs = AssetDatabaseExt.FindPrefabs<CustomBehaviour>(),
    scenes = AssetDatabaseExt.FindAllScenes()
}, delegate(GameObject gameObject, 
    RefactorTools.RefactorData _)
{
    var behaviours = gameObject.GetComponentsInChildren<CustomBehaviour>();
    foreach (var behaviour in behaviours)
    {
        behaviour.speed = new Speed
        {
            baseValue = behaviour.speedBaseValue,
            incrementValue = behaviour.speedIncrementValue
        };
    }
    return new RefactorTools.RefactorResult
    {
        completed = true
    };
});

RefactorData parameter

You can customize refactors by using extra data received when refactoring with RefactorData parameter:

RefactorTools.RefactorMonoBehaviour<CustomBehaviour>(new RefactorTools.RefactorParameters
{
    prefabs = AssetDatabaseExt.FindPrefabs<CustomBehaviour>(),
    scenes = AssetDatabaseExt.FindAllScenes()
}, delegate(GameObject gameObject, 
    RefactorTools.RefactorData data)
{
    if (data.inScene)
    {
        if (data.scenePath.Contains("Levels"))
        {
            // do extra logic like search for specific reference or anything else
        }
    }
    else
    {
        // do some prefab refactor   
    }
});

Roadmap

Here are some ideas on how to expand the project.

  • Customize generic refactor logic by expanding RefactorParameters, RefactorData and RefactorResult.
  • More refactor methods for general usage, like moving MonoBehaviour to another object, etc.
  • Customizable window to perform general refactors.
  • Automatize refactors using SerializedObjects and SerializedProperties to refactor field changes.

Collaborate

Feel free to create issues for feature requests and/or submit pull requests with your own improvements.

Contact

My Personal Website & Dev Blog

About

Code + examples for Refactoring Data stored in Unity Prefabs, Scenes and other Assets

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages