Tools for refactoring content stored in Unity Prefabs, Scenes and other Assets.
This project was initially used to show examples for this blogpost.
This package can be installed using OpenUPM, just click here for more details.
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
};
});
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
}
});
Here are some ideas on how to expand the project.
- Customize generic refactor logic by expanding
RefactorParameters
,RefactorData
andRefactorResult
. - 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.
Feel free to create issues for feature requests and/or submit pull requests with your own improvements.