Files
FueraDeEscala/Assets/Game Kit Controller/Scripts/PickUps/playerPickupIconManager.cs
Robii Aragon fd87a6ffd5 add ckg
plantilla base para movimiento básico
2026-02-05 05:07:55 -08:00

517 lines
13 KiB
C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class playerPickupIconManager : MonoBehaviour
{
[Header ("Main Settings")]
[Space]
public bool showIconsActive = true;
public bool showIconsPaused;
public LayerMask layer;
public LayerMask layerForFirstPerson;
public LayerMask layerForFBA;
public checkIconType checkIcontype;
public float maxDistanceIconEnabled;
[Space]
[Header ("Other Settings")]
[Space]
public string mainManagerName = "Pickup Manager";
public string mainPanelName = "Pickup Objects Icons";
public bool useCanvasGroupOnIcons;
[Space]
[Header ("Debug")]
[Space]
public List<pickUpIconInfo> pickUpIconList = new List<pickUpIconInfo> ();
[Space]
[Header ("Components")]
[Space]
public GameObject pickUpIconObject;
public Transform pickupObjectIconParent;
public Camera mainCamera;
public pickUpManager mainPickupManager;
public playerController playerControllerManager;
public playerCamera mainPlayerCamera;
Transform mainCameraTransform;
Vector3 targetPosition;
Vector3 cameraPosition;
Vector3 screenPoint;
Vector3 direction;
float distance;
LayerMask currentLayer;
//how to check if the icon is visible,
// -using a raycast from the object to the camera
// -using distance from the object to the player position
// -visible always that the player is looking at the object position
public enum checkIconType
{
raycast,
distance,
always_visible,
nothing
}
Vector2 mainCanvasSizeDelta;
Vector2 halfMainCanvasSizeDelta;
Vector2 iconPosition2d;
bool usingScreenSpaceCamera;
bool targetOnScreen;
float screenWidth;
float screenHeight;
pickUpIconInfo currentPickUpIconInfo;
int pickUpIconListCount;
bool screenResolutionAssigned;
bool mainPanelParentLocated;
bool mainPanelParentChecked;
bool layerForThirdPersonAssigned;
bool layerForFirstPersonAssigned;
bool layerForFBAAssigned;
void Awake ()
{
if (showIconsActive) {
initializeElements ();
}
}
void initializeElements ()
{
bool pickupManagerLocated = (mainPickupManager != null);
if (!pickupManagerLocated) {
mainPickupManager = pickUpManager.Instance;
pickupManagerLocated = mainPickupManager != null;
}
if (!pickupManagerLocated) {
GKC_Utils.instantiateMainManagerOnSceneWithTypeOnApplicationPlaying (pickUpManager.getMainManagerName (), typeof(pickUpManager), true);
mainPickupManager = pickUpManager.Instance;
pickupManagerLocated = (mainPickupManager != null);
}
if (!pickupManagerLocated) {
mainPickupManager = FindObjectOfType<pickUpManager> ();
pickupManagerLocated = mainPickupManager != null;
}
if (pickupManagerLocated) {
mainPickupManager.addNewPlayer (this);
} else {
showIconsActive = false;
}
}
void Start ()
{
mainCanvasSizeDelta = mainPlayerCamera.getMainCanvasSizeDelta ();
halfMainCanvasSizeDelta = mainCanvasSizeDelta * 0.5f;
usingScreenSpaceCamera = mainPlayerCamera.isUsingScreenSpaceCamera ();
if (mainCamera == null) {
mainCamera = mainPlayerCamera.getMainCamera ();
}
mainCameraTransform = mainCamera.transform;
}
void FixedUpdate ()
{
if (!showIconsActive || showIconsPaused) {
return;
}
pickUpIconListCount = pickUpIconList.Count;
if (pickUpIconListCount == 0) {
return;
}
if (playerControllerManager.isPlayerOnFirstPerson ()) {
if (!layerForThirdPersonAssigned) {
currentLayer = layerForFirstPerson;
layerForThirdPersonAssigned = true;
layerForFirstPersonAssigned = false;
layerForFBAAssigned = false;
}
} else {
if (playerControllerManager.isFullBodyAwarenessActive ()) {
if (!layerForFBAAssigned) {
currentLayer = layerForFBA;
layerForThirdPersonAssigned = false;
layerForFirstPersonAssigned = false;
layerForFBAAssigned = true;
}
} else {
if (!layerForFirstPersonAssigned) {
currentLayer = layer;
layerForThirdPersonAssigned = false;
layerForFirstPersonAssigned = true;
layerForFBAAssigned = false;
}
}
}
if (!usingScreenSpaceCamera) {
if (!screenResolutionAssigned) {
updateScreenValues ();
screenResolutionAssigned = true;
}
}
cameraPosition = mainCameraTransform.position;
for (int i = 0; i < pickUpIconListCount; i++) {
currentPickUpIconInfo = pickUpIconList [i];
if (currentPickUpIconInfo.paused) {
if (currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (false, i);
currentPickUpIconInfo.iconActive = false;
}
} else {
//get the target position from global to local in the screen
targetPosition = currentPickUpIconInfo.targetTransform.position;
if (usingScreenSpaceCamera) {
screenPoint = mainCamera.WorldToViewportPoint (targetPosition);
targetOnScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;
} else {
screenPoint = mainCamera.WorldToScreenPoint (targetPosition);
targetOnScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < screenWidth && screenPoint.y > 0 && screenPoint.y < screenHeight;
}
//if the target is visible in the screen, enable the icon
if (targetOnScreen) {
//use a raycast to check if the icon is visible
if (checkIcontype == checkIconType.raycast) {
distance = GKC_Utils.distance (targetPosition, cameraPosition);
if (distance <= maxDistanceIconEnabled) {
//set the direction of the raycast
direction = targetPosition - cameraPosition;
direction = direction / direction.magnitude;
//Debug.DrawRay(target.transform.position,-direction*distance,Color.red);
//if the raycast find an obstacle between the pick up and the camera, disable the icon
if (Physics.Raycast (targetPosition, -direction, distance, currentLayer)) {
if (currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (false, i);
currentPickUpIconInfo.iconActive = false;
}
} else {
//else, the raycast reachs the camera, so enable the pick up icon
if (!currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (true, i);
currentPickUpIconInfo.iconActive = true;
}
}
} else {
if (currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (false, i);
currentPickUpIconInfo.iconActive = false;
}
}
} else if (checkIcontype == checkIconType.distance) {
//if the icon uses the distance, then check it
distance = GKC_Utils.distance (targetPosition, cameraPosition);
if (distance <= maxDistanceIconEnabled) {
if (!currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (true, i);
currentPickUpIconInfo.iconActive = true;
}
} else {
if (currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (false, i);
currentPickUpIconInfo.iconActive = false;
}
}
} else {
//else, always visible when the player is looking at its direction
if (!currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (true, i);
currentPickUpIconInfo.iconActive = true;
}
}
if (currentPickUpIconInfo.iconActive) {
if (usingScreenSpaceCamera) {
iconPosition2d = new Vector2 ((screenPoint.x * mainCanvasSizeDelta.x) - halfMainCanvasSizeDelta.x,
(screenPoint.y * mainCanvasSizeDelta.y) - halfMainCanvasSizeDelta.y);
currentPickUpIconInfo.iconRectTransform.anchoredPosition = iconPosition2d;
} else {
currentPickUpIconInfo.iconObject.transform.position = new Vector3 (screenPoint.x, screenPoint.y, 0);
}
}
} else {
//else the icon is only disabled, when the player is not looking at its direction
if (currentPickUpIconInfo.iconActive) {
enableOrDisableIcon (false, i);
currentPickUpIconInfo.iconActive = false;
}
}
}
}
}
public void enableOrDisableIcon (bool state, int index)
{
if (useCanvasGroupOnIcons) {
if (state) {
if (pickUpIconList [index].mainCanvasGroup.alpha != 1) {
pickUpIconList [index].mainCanvasGroup.alpha = 1;
}
} else {
if (pickUpIconList [index].mainCanvasGroup.alpha != 0) {
pickUpIconList [index].mainCanvasGroup.alpha = 0;
}
}
} else {
if (pickUpIconList [index].iconObject.activeSelf != state) {
pickUpIconList [index].iconObject.SetActive (state);
}
}
}
//set what type of pick up is this object, and the object that the icon has to follow
public void setPickUpIcon (GameObject target, Texture targetTexture, int objectID, GameObject iconPrefab)
{
if (!showIconsActive) {
return;
}
if (checkIcontype == checkIconType.nothing) {
return;
}
if (mainPanelParentChecked) {
if (!mainPanelParentLocated) {
return;
}
} else {
mainPanelParentChecked = true;
if (!mainPanelParentLocated) {
mainPanelParentLocated = pickupObjectIconParent != null;
if (!mainPanelParentLocated) {
GameObject newPanelParentGameObject = GKC_Utils.getHudElementParent (playerControllerManager.gameObject, mainPanelName);
if (newPanelParentGameObject != null) {
pickupObjectIconParent = newPanelParentGameObject.transform;
mainPanelParentLocated = pickupObjectIconParent != null;
GKC_Utils.updateCanvasValuesByPlayer (playerControllerManager.gameObject, null, newPanelParentGameObject);
}
}
if (!mainPanelParentLocated) {
return;
}
}
}
pickUpIconListCount = pickUpIconList.Count;
for (int i = 0; i < pickUpIconListCount; i++) {
if (pickUpIconList [i].ID == objectID) {
return;
}
}
GameObject currentIconPrefab = pickUpIconObject;
if (iconPrefab != null) {
currentIconPrefab = iconPrefab;
}
GameObject newIconElement = (GameObject)Instantiate (currentIconPrefab, Vector3.zero, Quaternion.identity, pickupObjectIconParent);
pickUpIconInfo newIcon = newIconElement.GetComponent<pickUpIcon> ().pickUpElementInfo;
newIconElement.transform.localScale = Vector3.one;
newIconElement.transform.localPosition = Vector3.zero;
newIcon.target = target;
newIcon.targetTransform = target.transform;
if (!newIconElement.gameObject.activeSelf) {
newIconElement.gameObject.SetActive (true);
}
newIcon.ID = objectID;
if (targetTexture != null) {
newIcon.pickupIconImage.texture = targetTexture;
}
newIcon.iconActive = true;
pickUpIconList.Add (newIcon);
if (!showIconsActive) {
if (useCanvasGroupOnIcons) {
if (newIcon.mainCanvasGroup.alpha != 0) {
newIcon.mainCanvasGroup.alpha = 0;
}
} else {
if (newIcon.iconObject.activeSelf) {
newIcon.iconObject.SetActive (false);
}
}
newIcon.iconActive = false;
}
if (showIconsPaused) {
if (pickupObjectIconParent != null) {
if (pickupObjectIconParent.gameObject.activeSelf == showIconsPaused) {
pickupObjectIconParent.gameObject.SetActive (!showIconsPaused);
}
}
}
}
//destroy the icon
public void removeAtTarget (int index)
{
if (!showIconsActive) {
return;
}
if (index < pickUpIconList.Count) {
if (pickUpIconList [index].iconObject) {
Destroy (pickUpIconList [index].iconObject);
}
mainPickupManager.removeElementFromPickupListCalledByPlayer (pickUpIconList [index].ID);
pickUpIconList.RemoveAt (index);
} else {
print ("WARNING: the index to remove in player pickup icon manager is not correct, check the object picked to see if the icon is configured correctly");
}
}
public void removeAtTargetByID (int objectID)
{
if (!showIconsActive) {
return;
}
for (int i = 0; i < pickUpIconList.Count; i++) {
if (pickUpIconList [i].ID == objectID) {
if (pickUpIconList [i].iconObject != null) {
Destroy (pickUpIconList [i].iconObject);
}
pickUpIconList.RemoveAt (i);
return;
}
}
}
public void setPauseState (bool state, int index)
{
if (!showIconsActive) {
return;
}
if (index < pickUpIconList.Count) {
pickUpIconList [index].paused = state;
}
}
public void pauseOrResumeShowIcons (bool state)
{
showIconsPaused = state;
if (pickupObjectIconParent != null) {
if (pickupObjectIconParent.gameObject.activeSelf == showIconsPaused) {
pickupObjectIconParent.gameObject.SetActive (!showIconsPaused);
}
}
}
public void pauseOrResumeShowIconsFromEditor (bool state)
{
pauseOrResumeShowIcons (state);
updateComponent ();
}
public void setShowIconsActiveState (bool state)
{
showIconsActive = state;
}
public void updateScreenValues ()
{
screenWidth = Screen.width;
screenHeight = Screen.height;
}
public void setShowIconsActiveStateFromEditor (bool state)
{
setShowIconsActiveState (state);
updateComponent ();
}
void updateComponent ()
{
GKC_Utils.updateComponent (this);
GKC_Utils.updateDirtyScene ("Update Player Pickup Manager", gameObject);
}
}