Created
March 18, 2016 01:52
-
-
Save birdinforest/665b9301aa8eb544d80f to your computer and use it in GitHub Desktop.
Customise Inspector GUI and Scene GUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//C# Example (LookAtPoint.cs) | |
using UnityEngine; | |
[ExecuteInEditMode] // Can execute in edit mode even game is not running. | |
public class LookAtPoint : MonoBehaviour | |
{ | |
public Vector3 lookAtPoint = Vector3.zero; | |
void Update() | |
{ | |
transform.LookAt(lookAtPoint); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//C# Example (LookAtPointEditor.cs) | |
using UnityEngine; | |
using UnityEditor; | |
[CustomEditor(typeof(LookAtPoint))] // Target script | |
[CanEditMultipleObjects] | |
public class LookAtPointEditor : Editor | |
{ | |
SerializedProperty lookAtPoint; | |
void OnEnable() | |
{ | |
lookAtPoint = serializedObject.FindProperty("lookAtPoint"); // Get a property in target script. Notice that string "lookAtPoints" presents Vector3 lookAtPoint, not script 'LookAtPoint'. | |
} | |
// Custmise Inspector GUI | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
// Customise, although the same to original format. | |
EditorGUILayout.PropertyField(lookAtPoint); | |
// Add a label field | |
if (lookAtPoint.vector3Value.y > (target as LookAtPoint).transform.position.y) | |
{ | |
EditorGUILayout.LabelField("(Above this object)"); | |
} | |
if (lookAtPoint.vector3Value.y < (target as LookAtPoint).transform.position.y) | |
{ | |
EditorGUILayout.LabelField("(Below this object)"); | |
} | |
serializedObject.ApplyModifiedProperties(); | |
} | |
// Customise Scene GUI | |
public void OnSceneGUI() | |
{ | |
var t = (target as LookAtPoint); | |
EditorGUI.BeginChangeCheck(); | |
// Add a position handle on look at point. | |
Vector3 pos = Handles.PositionHandle(t.lookAtPoint, Quaternion.identity); | |
// Update target object depends on change of look at point | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
Undo.RecordObject(target, "Move point"); | |
t.lookAtPoint = pos; | |
t.Update(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment