M
M
Mimocodil2022-04-17 22:11:15
Unity
Mimocodil, 2022-04-17 22:11:15

How is the resizable view of the inspector component made?

Some components have a switch, using which you can change the list of visible fields, and, accordingly, use them differently.

Example: Light.type ( docs )
625c5ef33dc94614131204.png
Example: Camera.orthographic ( docs )
625c5efd892fc646677270.png

In the Github Unity C# Reference , I found the corresponding classes - LightEditor and CameraEditor . But in both classes I did not find anything that would definitely do this. There are suspicions about EditorGUI.EndProperty() and SerializedObject.ApplyModifiedProperties() , I'm just figuring out how it all works.

I want to understand the issue using the example script below. Make it so that the minX and maxX / minY and maxY fields are only visible if hasHorizontalBounds or hasVerticalBounds are set to true respectively.

using UnityEngine;

public class CameraFollow2D : MonoBehaviour {

  [SerializeField] private Transform target;

  [Header("Following settings")]
  [SerializeField] private float cameraSmooth = 2f;
  [SerializeField] private float cameraZ = -10f;
  [SerializeField] private bool useFixedUpdate;

  [Header("Horizontal bounds")]
  [SerializeField] private bool hasHorizontalBounds;
  [SerializeField] private float minX, maxX;

  [Header("Vertical bounds")]
  [SerializeField] private bool hasVerticalBounds;
  [SerializeField] private float minY, maxY;

  public Transform Target { get; set; }
  public float CameraSmooth { get; set; }

  private void FixedUpdate() {
    if (useFixedUpdate)
      FollowTarget(Time.fixedDeltaTime);
  }

  private void LateUpdate() {
    if (!useFixedUpdate)
      FollowTarget(Time.deltaTime);
  }

  private void FollowTarget(float deltaTime) {
    Vector3 targetVector = Vector3.forward * cameraZ + target.position;

    targetVector = Vector3.Lerp(transform.position, targetVector, cameraSmooth * deltaTime);

    if (hasHorizontalBounds)
      targetVector.x = Mathf.Clamp(targetVector.x, minX, maxX);

    if (hasVerticalBounds)
      targetVector.y = Mathf.Clamp(targetVector.y, minY, maxY);

    transform.position = targetVector;
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-04-17
@freeExec

The entry point is there OnInspectorGUI
. And all view constructs draw the appropriate elements if required.

if (EditorGUILayout.BeginFadeGroup(m_AnimShowLightBounceIntensity.faded))
                settings.DrawBounceIntensity();
            EditorGUILayout.EndFadeGroup();

There is no magic, only long and tedious to paint all the combinations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question