203 lines
5.6 KiB
C#
203 lines
5.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using WI;
|
|
|
|
namespace CHN
|
|
{
|
|
[Serializable]
|
|
public class Machine : MonoBehaviour
|
|
{
|
|
public string code;
|
|
public string[] typeOptions;
|
|
public UI_MachineKPI machineKPI;
|
|
public Sprite previewImage;
|
|
public Vector3 centerPos;
|
|
public Animator animator;
|
|
|
|
Renderer[] renderers;
|
|
public List<Material> HoverMaterials = new List<Material>();
|
|
Dictionary<Renderer, Material[]> originMaterials = new();
|
|
|
|
public Material translucentMat;
|
|
Material hoverRedMat;
|
|
public int flashCount;
|
|
public float animSpeed;
|
|
|
|
public override void AfterAwake()
|
|
{
|
|
if (!gameObject.activeSelf)
|
|
return;
|
|
GetComponentInParent<Floor>().machines.Add(this);
|
|
centerPos = transform.GetMeshGroupCenter();
|
|
animator = GetComponentInChildren<Animator>();
|
|
|
|
renderers = GetComponentsInChildren<Renderer>();
|
|
|
|
translucentMat = Resources.Load<Material>("MAT_Translucent");
|
|
hoverRedMat = Resources.Load<Material>("Mat_HoverColor");
|
|
CachingOriginMat();
|
|
}
|
|
|
|
public void SetAnimationSpeed()
|
|
{
|
|
if (machineKPI != null)
|
|
{
|
|
float.TryParse(machineKPI.data.eorate, out var eorate);
|
|
|
|
animSpeed = eorate / 100f;
|
|
animator.speed = animSpeed;
|
|
}
|
|
}
|
|
|
|
void CachingOriginMat()
|
|
{
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] originMat = renderer.materials;
|
|
originMaterials.Add(renderer, originMat);
|
|
}
|
|
}
|
|
|
|
public void ActivateTranslucent()
|
|
{
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] newMats = renderer.sharedMaterials;
|
|
|
|
for (int i = 0; i < newMats.Length; i++)
|
|
{
|
|
newMats[i] = translucentMat;
|
|
}
|
|
|
|
renderer.sharedMaterials = newMats;
|
|
}
|
|
}
|
|
|
|
public void StartOneFlash()
|
|
{
|
|
StartCoroutine(OneFlash());
|
|
}
|
|
public void StartFlash()
|
|
{
|
|
StartCoroutine(Flash());
|
|
}
|
|
|
|
public void StopFlash()
|
|
{
|
|
flashCount = 0;
|
|
StopAllCoroutines();
|
|
foreach (var m in HoverMaterials)
|
|
{
|
|
m.color = new Color(m.color.r, m.color.g, m.color.b, 0f);
|
|
}
|
|
}
|
|
|
|
public void DeactivateTranslucent()
|
|
{
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] originMat = originMaterials[renderer];
|
|
renderer.sharedMaterials = originMat;
|
|
}
|
|
AddHoverMaterials();
|
|
}
|
|
public void ResetTranslucent()
|
|
{
|
|
foreach (Renderer renderer in renderers)
|
|
{
|
|
Material[] originMat = originMaterials[renderer];
|
|
renderer.sharedMaterials = originMat;
|
|
}
|
|
}
|
|
|
|
public int GetMachineFloorIndex()
|
|
{
|
|
Transform currentParent = transform.parent;
|
|
|
|
while (currentParent != null)
|
|
{
|
|
Floor myFloor = currentParent.GetComponent<Floor>();
|
|
if (myFloor != null)
|
|
{
|
|
return myFloor.floorIndex;
|
|
}
|
|
currentParent = currentParent.parent;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void AddHoverMaterials()
|
|
{
|
|
HoverMaterials.Clear();
|
|
foreach (var renderer in renderers)
|
|
{
|
|
var highlightMat = hoverRedMat;
|
|
List<Material> RendererMats = new List<Material>();
|
|
RendererMats.AddRange(renderer.sharedMaterials);
|
|
RendererMats.Add(highlightMat);
|
|
renderer.sharedMaterials = RendererMats.ToArray();
|
|
HoverMaterials.Add(highlightMat);
|
|
}
|
|
}
|
|
|
|
IEnumerator Flash()
|
|
{
|
|
float alpha = 0f;
|
|
float coeff = 0.05f;
|
|
while (true)
|
|
{
|
|
yield return new WaitForSeconds(0.05f);
|
|
foreach(var m in HoverMaterials)
|
|
{
|
|
m.color = new Color(m.color.r,m.color.g,m.color.b,alpha);
|
|
}
|
|
if (alpha > 0.7f)
|
|
{
|
|
alpha = 0.7f;
|
|
coeff = -coeff;
|
|
}
|
|
else if (alpha < 0.3f)
|
|
{
|
|
alpha = 0.3f;
|
|
coeff = -coeff;
|
|
}
|
|
else
|
|
{
|
|
alpha += coeff;
|
|
}
|
|
}
|
|
}
|
|
IEnumerator OneFlash()
|
|
{
|
|
float alpha = 0.8f;
|
|
float coeff = 0.15f;
|
|
if (flashCount == 0)
|
|
{
|
|
foreach (var m in HoverMaterials)
|
|
{
|
|
m.color = new Color(m.color.r, m.color.g, m.color.b, alpha);
|
|
}
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
while (alpha > 0f)
|
|
{
|
|
yield return new WaitForSeconds(0.05f);
|
|
|
|
foreach (var m in HoverMaterials)
|
|
{
|
|
m.color = new Color(m.color.r, m.color.g, m.color.b, alpha);
|
|
}
|
|
|
|
alpha -= coeff;
|
|
}
|
|
|
|
flashCount = 1;
|
|
}
|
|
}
|
|
}
|
|
} |