Is there a way to add a new State to ViewType.States via editor code

rkeown

Active member
I'm trying to write a configuration utility for my Weapon Attachments System, and want to add a new State via code, called ZoomFocus, with an existing preset.

I've tried adding it to the cameraController.ViewTypes[combatIndex].States via an Array.Resize, and then calling ViewTypeBuilder.SerializeViewTypes(cameraController);, but it doesn't seem to save the added state.
 
Also, combatIndex is the index of the Combat ViewType in the cameraController.ViewStates array, and camerController is a reference to the camera controller component on the Main camera in the current scene.
 
Adding/removing state presets during runtime is not how the state system is intended to be used. What's the functionality you're trying to achieve here by adding them during runtime?
 
I didn't say runtime, I stated via editor code.im writing an in editor configuration for a system I am writing
 
Ah, my bad. Take a look at ViewTypeBuilder.cs - lines 48-70 are an example of how add states in the editor.
 
Sorry for the late reply, but I'm just now looking at this today. I'll keep you updated, and thanks again.
 
So, after reviewing the code, the AddState is an attribute class, meant to decorate a ViewType class, itself. In this case, I want to append a state to the UCC First Person View Type Combat, and modify the Preset for the already existing Zoom.

The problem is that I dont want to modify the included UCC Combatr ViewType's class defintion. Here is the code I'm using, and am getting mixed results with it. It defintely updates the Zoom State's preset, but somehow the Default state is getting updated with the FocusedZoom states preset, but the FocusZoom state never gets added.

Here;s my code

C#:
/// <summary>
    ///
    /// </summary>
    private static bool ConfigureCameraController() {
      if (!AreNecessaryPackagesInstalled())
        return false;

      var mainCamera = Camera.main;
      if (mainCamera == null)
        return false;

      var cameraController = mainCamera.GetComponent<CameraController>();
      if (cameraController == null)
        return false;
            
      var componentItemsConfigured = 0;

      var focusedZoomGuid = "";
      var focusedZoomPreset = FindPreset("WAS_FocusedZoomFirstPersonCombatPreset", out focusedZoomGuid);
      
      var zoomGuid = "";
      var zoomPreset = FindPreset("WAS_ZoomFirstPersonCombatPreset", out zoomGuid);
      
      var viewTypes = cameraController.ViewTypes;
      var viewTypeIndex = Array.FindIndex<ViewType>(viewTypes, 0, viewType => viewType.GetType() == typeof(Combat));
      var states = viewTypes[viewTypeIndex].States;
      var focusedZoomStateFound = false;
      var zoomStateFound = false;
      if (states.Length > 1) {       
        for (int i = 0; i < states.Length; i++) {
          if (states[i].Name.Equals("FocusedZoom") && focusedZoomPreset != null) {
            focusedZoomStateFound = true;
            states[i].Preset = focusedZoomPreset;
            states[i].BlockList = new string[0];
            states[i].Default = false;         
          } else if (states[i].Name.Equals("Zoom") && zoomPreset != null) {
            zoomStateFound = true;
            states[i].Preset = zoomPreset;
            states[i].BlockList = new string[1] {
              "FocusedZoom"
            };
            states[i].Default = false;
          }
        }
      }

      var viewStatesAdded = 0;
      if (!zoomStateFound) {
        Array.Resize(ref states, states.Length + 1);
        states[states.Length - 1] = new State("Zoom", zoomPreset, new string[1] { "FocusedZoom" });
        viewStatesAdded++;
      }
      if (!focusedZoomStateFound) {
        Array.Resize(ref states, states.Length + 1);
        states[states.Length - 1] = new State("FocusedZoom", focusedZoomPreset, null);
        viewStatesAdded++;
      }

      if (viewStatesAdded > 0) {
        viewTypes[viewTypeIndex].States = states;
        cameraController.ViewTypes = viewTypes;
        ViewTypeBuilder.SerializeViewTypes(cameraController);
      }

      var startingViewFound = false;
      foreach (var viewType in cameraController.ViewTypeData) {
        if (viewType.ObjectType == typeof(WAS_OpsiveUCCFirstPersonStartingView).ToString()) {
          startingViewFound = true;
          break;
        }
      }

      if (!startingViewFound) {
        var addedViewType = ViewTypeBuilder.AddViewType(cameraController, typeof(WAS_OpsiveUCCFirstPersonStartingView));
        if (addedViewType == null) {
          return false;
        } else {
          componentItemsConfigured++;
        }
      }

      if (cameraController.ActiveViewType.GetType() != typeof(WAS_OpsiveUCCFirstPersonStartingView)) {
        cameraController.SetViewType(typeof(WAS_OpsiveUCCFirstPersonStartingView), true);
      }

      if (!ConfigurePersistantEvenListener(m_uccCameraController.OnChangePerspectivesEvent, m_weaponAttachmentsController.OnChangePerspectives)) {
        Debug.LogWarning("Unable to configure persistant event listener for CameraController.OnChangePerspectivesEvent!");
      } else {
        componentItemsConfigured++;
      }

      if (!ConfigurePersistantEvenListener(m_uccCameraController.OnChangeViewTypesEvent, m_weaponAttachmentsController.OnChangeViewTypes)) {
        Debug.LogWarning("Unable to configure persistant event listener for CameraController.OnChangeViewTypesEvent!");
      } else {
        componentItemsConfigured++;
      }

      if (!ConfigurePersistantEvenListener(m_uccCameraController.OnZoomEvent, m_weaponAttachmentsController.OnZoom)) {
        Debug.LogWarning("Unable to configure persistant event listener for CameraController.OnZoomEvent!");
      } else {
        componentItemsConfigured++;
      }

      return componentItemsConfigured > 0;
    }
 
The last element in the states array is always used for the "Default" state, so whenever you're adding a new state, make sure to add it as the second-last element, not the last.
 
Top