E_ElecCompetition/Electrical_inspectionCompet.../Assets/Adam/Scripts/Components/ToolItem.cs

113 lines
3.0 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 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;
private float y;
private void Start()
{
tips = UIManager.Instance.ToolsItemManager.toolPanel.transform.Find("Tips").gameObject;
showName = tips.transform.GetChild(0).GetComponent<Text>();
}
public void SetValue(string name, Transform modelTransform)
{
toolName = name;
closeButton.onClick.AddListener(OnClose);
Texture2D icon = Resources.Load<Texture2D>("New/UI/" + name);
showIcon.sprite = ToSprite(icon);
originPos = modelTransform.localPosition;
originAngle = modelTransform.localEulerAngles;
prefab = Resources.Load<GameObject>("New/Models/" + toolName);
attribute = prefab.GetComponent<ToolModelClick>().attribute;
}
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)
{
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;
Destroy(gameObject);
ToolsItemManager.Instance.RemoveNull();
}
public void OnPointerEnter(PointerEventData eventData)
{
showName.text = toolName;
tips.transform.position = transform.position + new Vector3(0, 35, 0);
tips.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData)
{
tips.SetActive(false);
}
}