Unity Editor Scripting- Custom Transfom Editor

Let’s continue from editor scripting. I’m going to show how to implement your own transform editor for Unity3d. By default Unity has it’s own editor for Transform component, which shows you your rotation as EulerAngles not as Quaternion. So when you create a custom editor for Transform and call DrawDefaultInspector(); from OnInspectorGUI() method you will see something like this;

e1

 

What I did ;

  • Draw back our transform editor like it used to be.
  • Add buttons for resetting position, rotation and scale.
  • Add buttons to copy and paste position rotation and scale individually.
  • And draw a different editor if we are in 2D mode or 3D mode.

We are going to change our transfrom editor based on our editor behaviour mode. You can change this from Edit/ProjectSettings/Editor/DefaultBehaviourMode.

For 3D it’s going to be look like this;3dAnd for 2D  we don’t need to show z position, x and y angles and z scale.It looks fancier that way:)

2d

To change that look simply use EditorBehaviorMode enum;

Transform _transform;
EditorBehaviorMode editorMode;
void OnEnable()
{
  _transform = target as Transform;
  editorMode = EditorSettings.defaultBehaviorMode;
}

public override void OnInspectorGUI ()
{
  // Check editor mode
  if(editorMode.Equals(EditorBehaviorMode.Mode3D)){
    // place 3d gui here
  }
  else {
    // place 2d gui here
}

I’m not going into so many details, I’m basicly using gui buttons to reset(R),copy(C) and paste(P) and to copy and paste I used static variables like;

  static Vector3 savedPos,savedScale,savedRotation;

You can download the source code on Github and take a look (Bonus: you can fine Custom TextMesh editor with multiline in the same repository!) https://github.com/paganini24/UnityEditorScripts .

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: