Created
August 18, 2024 19:27
-
-
Save adamgit/411d3725c897827e907814619006ad20 to your computer and use it in GitHub Desktop.
Globally converts all (impossible, deleted) values of a specific enum to (new, correct / updated) values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UpdateableMyStruct | |
{ | |
public Func<MyStruct> getter; | |
public Action setter; | |
public string attribute; | |
public string ownerName; | |
} | |
public class ProposedChanges | |
{ | |
public List<UpdateableMyStruct> toChange; | |
} | |
/// | |
/// You made a mistake in your unity asset / game / whatever: you had an enum | |
/// with values eg "1, 2, 3, 4", and now you only have values "0, 1, 2"; you | |
/// need to convert all instances of "3" to (e.g.) "0". | |
/// | |
/// Add this class to an empty Game object, then right-click the component to | |
/// get the popup menus to check what it will change, and to run the changes. | |
/// | |
/// I used this to fix 10,000's of wrong/legacy enum values - executes instantly | |
/// even on moderately large scenes. | |
/// | |
/// Use this code at your own risk - commit your project to git/repo first, and | |
/// check it's updated the right things correctly before continuing development | |
/// | |
public class GlobalUnityEnumUpdater : MonoBehaviour | |
{ | |
private ProposedChanges _CalculateProposedChanges() | |
{ | |
var items = Resources.FindObjectsOfTypeAll<MyComponent>().ToList(); | |
var proposedChanges1 = new List<UpdateableMyStruct>(); | |
items.ForEach( item => | |
{ | |
proposedChanges1.AddRange( StructsThatNeedUpdating( item ) ); | |
} ); | |
return new ProposedChanges() | |
{ | |
toChange = proposedChanges1, | |
}; | |
} | |
[ContextMenu( "find all enums that need updating" )] | |
public void Menu_FindAll() | |
{ | |
var proposal = _CalculateProposedChanges(); | |
Debug.Log( "...found "+(proposal.toChange.Count)+" attributes that need changing" ); | |
OutputChanges( proposal.toChange ); | |
} | |
[ContextMenu( "UPDATE all enums that need updating" )] | |
public void Menu_UpdateAll() | |
{ | |
var proposal = _CalculateProposedChanges(); | |
PerformChanges( proposal.toChange ); | |
} | |
private List<UpdateableMyStruct> StructsThatNeedUpdating( MyComponent owner ) | |
{ | |
var r = new List<UpdateableMyStruct>(); | |
int illegalOldValueOfStruct = 3; | |
if( (int)owner.structFieldName.enumName == illegalOldValueOfStruct ) | |
r.Add( new UpdateableMyStruct() {getter = () => owner.structFieldName, setter = oldValue => owner.SetWithoutNotify_structFieldName( new MyStruct( [acceptable new values] )), attribute = ".structFieldName", ownerName = owner.name } ); | |
return r; | |
} | |
private void PerformChanges( List<UpdateableMyStruct> uls ) | |
{ | |
foreach( var ul in uls ) | |
ul.setter( ul.getter().value ); | |
} | |
private void OutputChanges( List<UpdateableMyStruct> uls ) | |
{ | |
foreach( var ul in uls ) | |
Debug.Log( "Will change: "+ul.getter()+" on object: "+ul.ownerName ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment