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

68 lines
1.8 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.UI;
//============================================================
//支持中文文件使用UTF-8编码
//@author #AUTHOR#
//@create #CREATEDATE#
//@company #COMPANY#
//
//@description:
//============================================================
public class ToolItem : MonoBehaviour
{
public string toolName;
public Button closeButton;
public Button selfButton;
public Image showIcon;
public Vector3 originPos = new Vector3();
public Vector3 originAngle = new Vector3();
public GameObject prefab;
public void SetValue(string name, Transform modelTransform, Action selfCallback = null)
{
toolName = name;
closeButton.onClick.AddListener(OnClose);
selfButton.onClick.AddListener(() =>
{
selfCallback?.Invoke();
});
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);
}
public void SetState(bool isActive)
{
closeButton.gameObject.SetActive(isActive);
selfButton.interactable = !isActive;
}
public void OnSelfClick()
{
}
public 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;
}
public void OnClose()
{
GameObject obj = Instantiate(prefab);
obj.name = toolName;
obj.transform.localPosition = originPos;
obj.transform.localEulerAngles = originAngle;
Destroy(gameObject);
}
}