|
| 1 | +using UnityEngine; |
| 2 | +using UnityEditor; |
| 3 | +using CasperSDK.Core.Configuration; |
| 4 | + |
| 5 | +namespace CasperSDK.Samples.Editor |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Custom Editor for CasperWalletDemoController. |
| 9 | + /// Shows a "Create Network Config" button when the field is empty. |
| 10 | + /// </summary> |
| 11 | + [CustomEditor(typeof(CasperWalletDemoController))] |
| 12 | + public class CasperWalletDemoControllerEditor : UnityEditor.Editor |
| 13 | + { |
| 14 | + private SerializedProperty _networkConfigProp; |
| 15 | + |
| 16 | + private void OnEnable() |
| 17 | + { |
| 18 | + _networkConfigProp = serializedObject.FindProperty("_networkConfig"); |
| 19 | + } |
| 20 | + |
| 21 | + public override void OnInspectorGUI() |
| 22 | + { |
| 23 | + serializedObject.Update(); |
| 24 | + |
| 25 | + // Draw the NetworkConfig field |
| 26 | + EditorGUILayout.PropertyField(_networkConfigProp); |
| 27 | + |
| 28 | + // If NetworkConfig is null, show helper button |
| 29 | + if (_networkConfigProp.objectReferenceValue == null) |
| 30 | + { |
| 31 | + EditorGUILayout.HelpBox( |
| 32 | + "No Network Config assigned. Using Testnet defaults.\n" + |
| 33 | + "Create one to customize network settings (Mainnet, custom RPC, etc.)", |
| 34 | + MessageType.Info); |
| 35 | + |
| 36 | + EditorGUILayout.BeginHorizontal(); |
| 37 | + GUILayout.FlexibleSpace(); |
| 38 | + |
| 39 | + if (GUILayout.Button("Create Network Config", GUILayout.Width(180))) |
| 40 | + { |
| 41 | + CreateAndAssignNetworkConfig(); |
| 42 | + } |
| 43 | + |
| 44 | + EditorGUILayout.EndHorizontal(); |
| 45 | + } |
| 46 | + |
| 47 | + serializedObject.ApplyModifiedProperties(); |
| 48 | + } |
| 49 | + |
| 50 | + private void CreateAndAssignNetworkConfig() |
| 51 | + { |
| 52 | + // Create the NetworkConfig asset |
| 53 | + var config = ScriptableObject.CreateInstance<NetworkConfig>(); |
| 54 | + |
| 55 | + // Determine save path |
| 56 | + var path = EditorUtility.SaveFilePanelInProject( |
| 57 | + "Save Network Config", |
| 58 | + "NetworkConfig", |
| 59 | + "asset", |
| 60 | + "Choose where to save the Network Config"); |
| 61 | + |
| 62 | + if (string.IsNullOrEmpty(path)) |
| 63 | + return; |
| 64 | + |
| 65 | + // Save and assign |
| 66 | + AssetDatabase.CreateAsset(config, path); |
| 67 | + AssetDatabase.SaveAssets(); |
| 68 | + |
| 69 | + _networkConfigProp.objectReferenceValue = config; |
| 70 | + serializedObject.ApplyModifiedProperties(); |
| 71 | + |
| 72 | + // Select the new asset |
| 73 | + Selection.activeObject = config; |
| 74 | + EditorGUIUtility.PingObject(config); |
| 75 | + |
| 76 | + Debug.Log($"[CasperSDK] NetworkConfig created at: {path}"); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments