예제 #1
0
        /// <summary>
        /// Register callbacks and make first call to RemoteConfigDataManager, and initialize the UI.
        /// </summary>
        public void OnEnable()
        {
            Instance     = this;
            titleContent = new GUIContent("Remote Config Sync");

            // Initialize UI state if necessary.
            if (rootVisualElement.childCount == 0)
            {
                InitializeUI();
            }
            retrievingRemoteConfigData = false;

            SyncDataManager.DataRetrieved += RemoteConfigDataUpdatedCallback;
            SyncDataManager.DataUploaded  += RemoteConfigDataUpdatedCallback;
            SyncDataManager.RetrieveError += RemoteConfigErrorCallback;
            if (File.Exists(SyncDataManager.LocalFilePath))
            {
                var localContent = File.ReadAllText(SyncDataManager.LocalFilePath);
                RemoteConfigData            = RemoteConfigData.Deserialize(localContent);
                SyncDataManager.CurrentData = RemoteConfigData;
                ready = true;
                UpdateUI();
            }
            else if (!EditorApplication.isPlayingOrWillChangePlaymode)
            {
                GetRemoteConfigData();
            }
        }
        /// <summary>
        /// Creates the UI elements and registers callbacks.
        /// </summary>
        public void OnEnable()
        {
            syncBehaviour     = target as RemoteConfigSyncBehaviour;
            rootVisualElement = new VisualElement();
            rootVisualElement.styleSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>(stylePath));

            // Show the Script field (not-editable) like standard inspectors do.
            var script      = MonoScript.FromMonoBehaviour(syncBehaviour);
            var scriptField = new ObjectField("Script")
            {
                value = script
            };

            scriptField.SetEnabled(false);
            rootVisualElement.Add(scriptField);

            // PrefixSource enum dropdown control. When PrefixSource changes, observe whether the
            // KeyPrefix field should be shown/hidden.
            var prefixSourceProp = serializedObject.FindProperty("PrefixSource");

            prefixSourceField = new EnumField("Prefix Source", syncBehaviour.PrefixSource);
            prefixSourceField.BindProperty(prefixSourceProp);
            prefixSourceField.RegisterCallback <ChangeEvent <Enum> >(OnPrefixSourceChange);
            rootVisualElement.Add(prefixSourceField);

            // KeyPrefix control, only shown if syncBehaviour.PrefixSource == PrefixSource.Custom.
            var keyPrefixProperty = serializedObject.FindProperty("KeyPrefix");

            keyPrefixField = new PropertyField(keyPrefixProperty);
            if (syncBehaviour.PrefixSource == PrefixSource.Custom)
            {
                rootVisualElement.Add(keyPrefixField);
            }

            // Sync All Fields control.
            var syncAllProp = serializedObject.FindProperty("SyncAllFields");

            syncAllField = new PropertyField(syncAllProp);
            syncAllField.RegisterCallback <ChangeEvent <bool> >(OnSyncAllFieldsChange);
            rootVisualElement.Add(syncAllField);

            // Include sub-fields
            var includeSubProp = serializedObject.FindProperty("IncludeSubFields");

            includeSubFieldsField = new PropertyField(includeSubProp);
            if (syncBehaviour.SyncAllFields)
            {
                rootVisualElement.Add(includeSubFieldsField);
            }

            var buttonContainer = new TemplateContainer();

            // Use row class to place buttons side-by-side.
            buttonContainer.AddToClassList("row");

            // Add a button that can invoke the SyncFields function on the object during gameplay.
            var syncButton = new Button(() => SyncFields())
            {
                text = "Sync Fields"
            };

            buttonContainer.Add(syncButton);

            // Add a button that prompts RemoteConfigSyncUIWindow to update sync targets.
            var updateButton = new Button(() => SyncWindow.RefreshSyncTargets())
            {
                text = "Update targets"
            };

            buttonContainer.Add(updateButton);

            // Add button to open the RemoteConfigSyncWindow.
            var rcWindowButton = new Button(() => SyncWindow.OpenWindow())
            {
                text = "Open Sync Window"
            };

            buttonContainer.Add(rcWindowButton);
            rootVisualElement.Add(buttonContainer);
        }