Building Selection started + Outline
This commit is contained in:
65
Assets/Game/Scripts/WorldItemSelector.cs
Normal file
65
Assets/Game/Scripts/WorldItemSelector.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
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.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.buildingName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user