ShanxiKnowledgeBase/SXElectricalInspection/Assets/Adam/Scripts/Components/ToolItem.cs

153 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Components;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
//============================================================
//支持中文文件使用UTF-8编码
//@author #AUTHOR#
//@create #CREATEDATE#
//@company #COMPANY#
//
//@description:
//============================================================
public class ToolItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public string toolName;
public Button closeButton;
public Button selfButton;
public Image showIcon;
public Text showName;
public GameObject tips;
public Vector3 originPos = new Vector3();
public Vector3 originAngle = new Vector3();
public GameObject prefab;
public Attribute attribute = Attribute.defaultModel;
public bool isRightOrWroing = false;
public Text countText;
public int count = 1;
public ToolParentItem toolParentItem;
//public float h;
public RightAndWrong rightAndWeong = RightAndWrong.defaultModel;
public WearAndUse wearAndUse = WearAndUse.defaultModel;
private float y;
private void Start()
{
tips = UIManager.Instance.toolsItemManager.toolPanel.transform.Find("Tips").gameObject;
showName = tips.transform.GetChild(0).GetComponent<Text>();
closeButton.gameObject.SetActive(false);
//countText.gameObject.SetActive(false);
}
public void SetValue(string name, Transform modelTransform, Vector3 pos, Vector3 Rot)
{
toolName = name;
closeButton.onClick.AddListener(OnClose);
Texture2D icon = Resources.Load<Texture2D>("New/UI/" + name);
showIcon.sprite = ToSprite(icon);
originPos = pos;
originAngle = Rot;
prefab = Resources.Load<GameObject>("New/Models/" + toolName);
attribute = prefab.GetComponent<ToolModelClick>().attribute;
rightAndWeong = modelTransform.GetComponent<ToolModelClick>().rightAndWrong;
wearAndUse = modelTransform.GetComponent<ToolModelClick>().wearAnduse;
isRightOrWroing = modelTransform.GetComponent<FractionTools>().isRight;
}
public void SetCount()
{
count++;
countText.text = $"×{count}";
countText.gameObject.SetActive(true);
}
public void SetState(bool isActive)
{
//closeButton.gameObject.SetActive(isActive);
if (toolName == "工作卡")
selfButton.interactable = isActive;
else
selfButton.interactable = !isActive;
}
/// <summary>
/// 判断自己是不是可以点击
/// </summary>
public void SwitchSelfIsClick()
{
closeButton.gameObject.SetActive(false);
if (attribute == Attribute.SafetyToolsAndInstruments)
{
selfButton.interactable = false;
}
else if (attribute == Attribute.ToolsAndInstruments)
{
if (toolName == "工作卡")
selfButton.interactable = false;
else
selfButton.interactable = true;
}
else
{
}
}
private Sprite ToSprite(Texture2D t)
{
Sprite prite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
return prite;
}
private void OnClose()
{
tips.SetActive(false);
GameObject obj = Instantiate(prefab);
obj.name = toolName;
obj.transform.localPosition = originPos;
obj.transform.localEulerAngles = originAngle;
obj.GetComponent<ToolModelClick>().rightAndWrong = rightAndWeong;
obj.GetComponent<ToolModelClick>().wearAnduse = wearAndUse;
obj.GetComponent<FractionTools>().isRight = isRightOrWroing;
if (toolParentItem != null)
{
ToolsItemManager.Instance.RemoveMergedItemsHave(this);
}
FractionManager.Instance.RemoveToolByName(obj);
Destroy(gameObject);
ToolsItemManager.Instance.RemoveNull();
}
public void OnPointerEnter(PointerEventData eventData)
{
if (GlobalFlag.currentLoadSceneName == "工具间场景")
{
closeButton.gameObject.SetActive(true);
}
showName.text = toolName;
tips.transform.position = transform.position + new Vector3(0, 50, 0);
tips.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData)
{
tips.SetActive(false);
if (GlobalFlag.currentLoadSceneName == "工具间场景")
{
closeButton.gameObject.SetActive(false);
}
}
}