using UnityEngine;
using UnityEngine.Events;
using Lean.Common;
namespace Lean.Touch
{
/// This component can calculate the average position of all selected objects in the scene.
/// NOTE: You must manually call the Calculate method from somewhere (e.g. inspector event), then it will be output from the OnPosition event.
[HelpURL(LeanTouch.PlusHelpUrlPrefix + "LeanSelectableCenter")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Selectable Center")]
public class LeanSelectableCenter : MonoBehaviour
{
[System.Serializable] public class Vector3Event : UnityEvent {}
/// This allows you to output Seconds to UI text.
public Vector3Event OnPosition { get { if (onPosition == null) onPosition = new Vector3Event(); return onPosition; } } [SerializeField] private Vector3Event onPosition;
/// This method will calculate the position and output it using the OnPosition event.
[ContextMenu("Calculate")]
public void Calculate()
{
var total = default(Vector3);
var count = 0;
foreach (var selectable in LeanSelectable.Instances)
{
if (selectable.IsSelected == true)
{
total += selectable.transform.position;
count += 1;
}
}
if (count > 0)
{
var center = total / count;
if (onPosition != null)
{
onPosition.Invoke(center);
}
}
}
}
}
#if UNITY_EDITOR
namespace Lean.Touch.Editor
{
using TARGET = LeanSelectableCenter;
[UnityEditor.CanEditMultipleObjects]
[UnityEditor.CustomEditor(typeof(TARGET), true)]
public class LeanSelectableCenter_Editor : LeanEditor
{
protected override void OnInspector()
{
TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts);
Draw("onPosition");
}
}
}
#endif