usingSystem;usingUnityEngine;usingUnityEditor;//Add to an Editor folderpublicclassOtto{/// <summary>/// Loops through all public fields on the target mono behaviour and searches for a relevant asset in the asset database/// Separate your search terms with an underscore, and being as specific as possible with your variable names helps Otto to find the right assets/// Example: "Optimistic Unicorn.mp4" can be found with variable names "Optimistic", "Unicorn", or best of all: "Optimistic_Unicorn"/// </summary> [MenuItem("CONTEXT/MonoBehaviour/Auto Populate Variables")]staticvoidAutoPopulate(MenuCommandcommand){Debug.Log("[Otto]: Auto populating variables");vartype=command.context.GetType();Debug.Log($"[Otto]: Doing a kickflip over the {type.Name}");varfields=type.GetFields();varfieldsSet=0;for(inti=0;i<fields.Length;i++){varassetName=fields[i].Name.Split('_');varasset=AssetDatabase.FindAssets(String.Join(" ",assetName));if(asset.Length>0){varassetPath=AssetDatabase.GUIDToAssetPath(asset[0]);varloadedAsset=AssetDatabase.LoadAssetAtPath(assetPath,fields[i].FieldType);if(loadedAsset!=null){Undo.RecordObject(command.context,$"Set field {fields[i].Name}");fieldsSet++;fields[i].SetValue(command.context,loadedAsset);}}}Debug.Log($"[Otto]: All done! I set {fieldsSet} fields");}}
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.