123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class BeaconController : MonoBehaviour
- {
- public float Height = 0.5f;
- public int Rounding = 1;
-
- public bool info = false;
- public enum Mode { Player, Editor};
- public Mode mode = Mode.Player;
- public string text_info;
- public string id;
- Text text;
- GameObject canvas;
- private Vector3 screenPoint;
- private Vector3 offset;
- private Vector3 curScreenPoint;
- private Vector3 curPosition;
-
- void Start()
- {
- text = transform.GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>();
- text.text = text_info;
- canvas = transform.GetChild(0).gameObject;
- canvas.SetActive(info);
- }
-
- void Update()
- {
- if (info != transform.GetChild(0).gameObject.activeSelf)
- {
- canvas.SetActive(info);
- }
- }
- void OnMouseDown()
- {
- if(mode == Mode.Player) info = !info;
-
- if (mode == Mode.Editor)
- {
- screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
- offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
- info = true;
- text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
- text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 15);
- }
- }
-
- void OnMouseDrag()
- {
- if (mode == Mode.Editor)
- {
- curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
- curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
- var x =(float) Math.Round(curPosition.x, Rounding);
- var z = (float)Math.Round(curPosition.z, Rounding);
- transform.position = new Vector3(x, Height, z);
- text.text = $"id={id} x={x} y={z}";
- canvas.transform.GetChild(0).transform.position = new Vector3(Input.mousePosition.x, Input.mousePosition.y+20, screenPoint.z);
- }
- }
- private void OnMouseUp()
- {
- if (mode == Mode.Editor)
- {
- info = false;
- text.text = text_info;
- text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 120);
- text.gameObject.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 75);
- }
- }
- }
|