Files
ChunilENG/Assets/Plugins/XRLib/GenericController/GenericController.cs

131 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.EventSystems;
namespace WI
{
public enum ViewMode
{
None,
TopView,
PerspectiveView,
FirstPersonView,
}
//TODO::Eat Viewmode
[RequireComponent(typeof(Camera))]
public abstract class GenericController : MonoBehaviour
{
public new Camera camera;
public GenericControllerOption option;
public MaxRangeLimitter maxRangeLimitter = new();
protected Vector3 moveVector;
protected Vector3 cameraPosition;
public Vector3 nextPosition;
public override void AfterAwake()
{
camera = GetComponent<Camera>();
option.Apply(this);
Collider MaxRange = transform.parent.Find(nameof(MaxRange)).GetComponent<BoxCollider>();
maxRangeLimitter.SetRange(MaxRange);
}
public virtual void Movement()
{
Move();
Zoom();
Rotate();
}
protected abstract void Move();
protected abstract void Zoom();
protected abstract void Rotate();
public abstract void LastPositioning(bool limit);
public abstract void Rewind();
protected UserInput input;
public bool IsClickUI
{
get
{
bool result = false;
if (Input.GetMouseButtonDown(0))
{
result = EventSystem.current.IsPointerOverGameObject();
}
return result;
}
}
public bool IsOnTheUI
{
get
{
if (IsPointerOverExcludedUI())
{
return false;
}
else
{
var result = EventSystem.current.IsPointerOverGameObject();
return result;
}
}
}
bool IsPointerOverExcludedUI()
{
PointerEventData pointerData = new PointerEventData(EventSystem.current);
pointerData.position = Input.mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, raycastResults);
if (raycastResults.Count > 0)
{
if ((LayerMask.GetMask("Default") & (1 << raycastResults[0].gameObject.layer)) != 0)
{
return true;
}
}
return false;
}
bool isPressed;
protected virtual void LateUpdate()
{
if (IsPressedUI())
return;
if (IsClickUI)
return;
if (IsOnTheUI)
return;
input.GetInput();
Movement();
var limitCheck = maxRangeLimitter.MoveRangeLimit(nextPosition);
LastPositioning(limitCheck);
}
private bool IsPressedUI()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject())
{
isPressed = true;
}
}
if (Input.GetMouseButtonUp(0))
{
isPressed = false;
}
return isPressed;
}
}
}