/// <summary> /// Helper function that draws a field of 3 floats. /// </summary> static Vector3Update DrawVector3(Vector3 value, Vector3Axe multipleValueAxes) { GUILayoutOption opt = GUILayout.MinWidth(30f); Vector3Update update = new Vector3Update() { Value = value }; update.SetX(DrawFloatField("X", value.x, ((multipleValueAxes & Vector3Axe.X) == Vector3Axe.None), opt)); update.SetY(DrawFloatField("Y", value.y, ((multipleValueAxes & Vector3Axe.Y) == Vector3Axe.None), opt)); update.SetZ(DrawFloatField("Z", value.z, ((multipleValueAxes & Vector3Axe.Z) == Vector3Axe.None), opt)); return(update); }
/// <summary> /// Helper function that draws a field of 3 floats. /// </summary> static Vector3Update DrawVector3(Vector3 value, Vector3Axe multipleValueAxes) { GUILayoutOption opt = GUILayout.MinWidth(30f); Vector3Update update = new Vector3Update() { Value = value }; update.SetX(DrawFloatField("X", value.x, ((multipleValueAxes & Vector3Axe.X) == Vector3Axe.None), opt)); update.SetY(DrawFloatField("Y", value.y, ((multipleValueAxes & Vector3Axe.Y) == Vector3Axe.None), opt)); update.SetZ(DrawFloatField("Z", value.z, ((multipleValueAxes & Vector3Axe.Z) == Vector3Axe.None), opt)); return update; }
/// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI() { Transform trans = target as Transform; EditorGUIUtility.LookLikeControls(15f); bool forceValue = false; // Need this in unity 3.5 to fix a weird bug with GUI.changed Vector3Update pos; Vector3Update rot; Vector3Update scale; Vector3Axe axe; serializedObject.Update(); // Position EditorGUILayout.BeginHorizontal(); { Vector3Update forcePos = new Vector3Update() { UpdatedAxes = Vector3Axe.None }; axe = GetMultipleValuesAxes(_posProp); if (DrawButton("P", "Reset Position", axe != Vector3Axe.None || IsResetPositionValid(trans), 20f)) { NGUIEditorTools.RegisterUndo("Reset Position", serializedObject.targetObjects); forcePos.ForceSet(Vector3.zero); trans.localPosition = Vector3.zero; forceValue = true; } pos = DrawVector3(trans.localPosition, axe); if (forcePos.UpdatedAxes == Vector3Axe.All) { pos = forcePos; } } EditorGUILayout.EndHorizontal(); // Rotation EditorGUILayout.BeginHorizontal(); { Vector3Update forceRot = new Vector3Update() { UpdatedAxes = Vector3Axe.None }; axe = GetMultipleValuesAxes(_rotProp); if (DrawButton("R", "Reset Rotation", axe != Vector3Axe.None || IsResetRotationValid(trans), 20f)) { NGUIEditorTools.RegisterUndo("Reset Rotation", serializedObject.targetObjects); forceRot.ForceSet(Vector3.zero); trans.localEulerAngles = Vector3.zero; forceValue = true; } rot = DrawVector3(trans.localEulerAngles, axe); if (forceRot.UpdatedAxes == Vector3Axe.All) { rot = forceRot; } } EditorGUILayout.EndHorizontal(); // Scale EditorGUILayout.BeginHorizontal(); { Vector3Update forceScale = new Vector3Update() { UpdatedAxes = Vector3Axe.None }; axe = GetMultipleValuesAxes(_sclProp); if (DrawButton("S", "Reset Scale", axe != Vector3Axe.None || IsResetScaleValid(trans), 20f)) { NGUIEditorTools.RegisterUndo("Reset Scale", serializedObject.targetObjects); forceScale.ForceSet(Vector3.one); trans.localScale = Vector3.one; forceValue = true; } scale = DrawVector3(trans.localScale, axe); if (forceScale.UpdatedAxes == Vector3Axe.All) { scale = forceScale; } } EditorGUILayout.EndHorizontal(); // If something changes, set the transform values for each object selected. // Apply changes on modified axes only. if (GUI.changed || forceValue) { NGUIEditorTools.RegisterUndo("Transform Change", serializedObject.targetObjects); pos.Validate(); rot.Validate(); scale.Validate(); foreach (Object obj in serializedObject.targetObjects) { trans = obj as Transform; if (trans != null) { trans.localPosition = pos.GetUpdatedVector3(trans.localPosition); trans.localEulerAngles = rot.GetUpdatedVector3(trans.localEulerAngles); trans.localScale = scale.GetUpdatedVector3(trans.localScale); } } } }
/// <summary> /// Draw the inspector widget. /// </summary> public override void OnInspectorGUI() { Transform trans = target as Transform; EditorGUIUtility.LookLikeControls(15f); bool forceValue = false; // Need this in unity 3.5 to fix a weird bug with GUI.changed Vector3Update pos; Vector3Update rot; Vector3Update scale; Vector3Axe axe; serializedObject.Update(); // Position EditorGUILayout.BeginHorizontal(); { Vector3Update forcePos = new Vector3Update() { UpdatedAxes = Vector3Axe.None }; axe = GetMultipleValuesAxes(_posProp); if (DrawButton("P", "Reset Position", axe != Vector3Axe.None || IsResetPositionValid(trans), 20f)) { //NGUIEditorTools.RegisterUndo("Reset Position", serializedObject.targetObjects); forcePos.ForceSet(Vector3.zero); trans.localPosition = Vector3.zero; forceValue = true; } pos = DrawVector3(trans.localPosition, axe); if (forcePos.UpdatedAxes == Vector3Axe.All) { pos = forcePos; } } EditorGUILayout.EndHorizontal(); // Rotation EditorGUILayout.BeginHorizontal(); { Vector3Update forceRot = new Vector3Update() { UpdatedAxes = Vector3Axe.None }; axe = GetMultipleValuesAxes(_rotProp); if (DrawButton("R", "Reset Rotation", axe != Vector3Axe.None || IsResetRotationValid(trans), 20f)) { //NGUIEditorTools.RegisterUndo("Reset Rotation", serializedObject.targetObjects); forceRot.ForceSet(Vector3.zero); trans.localEulerAngles = Vector3.zero; forceValue = true; } rot = DrawVector3(trans.localEulerAngles, axe); if (forceRot.UpdatedAxes == Vector3Axe.All) { rot = forceRot; } } EditorGUILayout.EndHorizontal(); // Scale EditorGUILayout.BeginHorizontal(); { Vector3Update forceScale = new Vector3Update() { UpdatedAxes = Vector3Axe.None }; axe = GetMultipleValuesAxes(_sclProp); if (DrawButton("S", "Reset Scale", axe != Vector3Axe.None || IsResetScaleValid(trans), 20f)) { //NGUIEditorTools.RegisterUndo("Reset Scale", serializedObject.targetObjects); forceScale.ForceSet(Vector3.one); trans.localScale = Vector3.one; forceValue = true; } scale = DrawVector3(trans.localScale, axe); if (forceScale.UpdatedAxes == Vector3Axe.All) { scale = forceScale; } } EditorGUILayout.EndHorizontal(); // If something changes, set the transform values for each object selected. // Apply changes on modified axes only. if (GUI.changed || forceValue) { //NGUIEditorTools.RegisterUndo("Transform Change", serializedObject.targetObjects); pos.Validate(); rot.Validate(); scale.Validate(); foreach (Object obj in serializedObject.targetObjects) { trans = obj as Transform; if (trans != null) { trans.localPosition = pos.GetUpdatedVector3(trans.localPosition); trans.localEulerAngles = rot.GetUpdatedVector3(trans.localEulerAngles); trans.localScale = scale.GetUpdatedVector3(trans.localScale); } } } }