78 lines
2.4 KiB
C#
78 lines
2.4 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 Transform selectedItemUI;
|
|
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);
|
|
}
|
|
RefreshUI();
|
|
}
|
|
}
|
|
|
|
|
|
public void RefreshUI(){
|
|
selectedItemUI.gameObject.SetActive(Selector.selectedBuilding !=null);
|
|
if(Selector.selectedBuilding!=null){
|
|
selectedItemUI.GetChild(0).GetComponent<Text>().text = Selector.selectedBuilding.buildingData.buildingName;
|
|
}
|
|
|
|
// switch (Selector.selectedBuilding.buildingData.buildingName)
|
|
// {
|
|
// case "Research Facility":
|
|
// Debug.Log("research facility menu");
|
|
// break;
|
|
|
|
// case "Green House":
|
|
// Debug.Log("green house menu");
|
|
// break;
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
}
|
|
}
|