147 lines
4.0 KiB
C#
147 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using WI;
|
|
|
|
namespace CHN
|
|
{
|
|
|
|
public class Machine : MonoBehaviour
|
|
{
|
|
public string code;
|
|
public string[] typeOptions;
|
|
public UI_MachineKPI machineKPI;
|
|
public Sprite previewImage;
|
|
public Vector3 centerPos;
|
|
|
|
Renderer[] renderers;
|
|
public List<Material> HoverMaterials = new List<Material>();
|
|
Dictionary<Renderer, Material[]> originMaterials = new();
|
|
|
|
public Material translucentMat;
|
|
Material hoverRedMat;
|
|
|
|
public override void AfterAwake()
|
|
{
|
|
if (!gameObject.activeSelf)
|
|
return;
|
|
GetComponentInParent<Floor>().machines.Add(this);
|
|
centerPos = transform.GetMeshGroupCenter();
|
|
renderers = GetComponentsInChildren<Renderer>();
|
|
|
|
translucentMat = Resources.Load<Material>("MAT_Translucent");
|
|
hoverRedMat = Resources.Load<Material>("Mat_HoverColor");
|
|
CachingOriginMat();
|
|
}
|
|
|
|
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 StartFlash()
|
|
{
|
|
StartCoroutine(Flash());
|
|
}
|
|
|
|
public void StopFlash()
|
|
{
|
|
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 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.5f)
|
|
{
|
|
alpha = 0.5f;
|
|
coeff = -coeff;
|
|
}
|
|
else if (alpha < 0f)
|
|
{
|
|
alpha = 0f;
|
|
coeff = -coeff;
|
|
}
|
|
else
|
|
{
|
|
alpha += coeff;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |