59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using UnityEngine.UI;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.Events;
|
|
|
|
public class WorldItemSelector : MonoBehaviour
|
|
{
|
|
public float moveThreshold = 1;
|
|
public LayerMask layerMask;
|
|
|
|
public void SelectScreenPoint(Vector2 screenPoint){
|
|
Ray ray = Camera.main.ScreenPointToRay(screenPoint);
|
|
RaycastHit hit = new RaycastHit();
|
|
|
|
if(Physics.Raycast(ray,out hit, Mathf.Infinity, layerMask)){
|
|
Building selectedB = hit.collider.GetComponent<Building>();
|
|
if(selectedB!=null){
|
|
Debug.Log("Selected building : " + selectedB.buildingData.buildingName);
|
|
Selector.selectBuilding(selectedB);
|
|
}else{
|
|
Debug.Log("No target here, Unselecting");
|
|
Selector.selectBuilding(null);
|
|
}
|
|
}else{
|
|
Selector.selectBuilding(null);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private Vector2 startedPos= Vector2.zero;
|
|
public void OnPointerDown(BaseEventData e){
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
startedPos = ped.position;
|
|
}
|
|
|
|
public void OnPointerUp(BaseEventData e){
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
float pointerDiff = Mathf.Abs(ped.position.magnitude - startedPos.magnitude);
|
|
if(pointerDiff < moveThreshold){
|
|
SelectScreenPoint(ped.position);
|
|
}else{
|
|
Debug.Log("Pointer moved (" + pointerDiff+ "), Not gonna select item");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class Selector{
|
|
public static Building selectedBuilding;
|
|
public static UnityEvent OnSelectedChanged = new UnityEvent();
|
|
|
|
public static void selectBuilding(Building e){
|
|
selectedBuilding = e;
|
|
OnSelectedChanged.Invoke();
|
|
}
|
|
}
|