Files
JJBB/Assets/Project/Script/GUI/Base/UICameraMaskable.cs
2024-08-23 15:49:34 +08:00

68 lines
2.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.Collections.Generic;
using Module.Log;
using UnityEngine;
public class UICameraMaskable : MonoBehaviour
{
public static int clipAreaPropertyId;
public UICameraMask mask { get; private set; }
private List<Material> materialList;
private bool started;
private void OnEnable()
{
if (started)
{
mask = GetParentMask(transform);
if (mask == null)
{
LogModule.WarningLog(string.Format("物体{0}上的UICameraMaskable无法找到Mask", gameObject.name));
UICameraMask.RemoveClipForOne(this);
}
else
{
mask.AddClippable(this);
}
}
}
private void OnDisable()
{
if (mask != null)
mask.RemoveClippable(this);
}
private void OnDestroy()
{
for (int i = 0; i < materialList.Count; i++)
Destroy(materialList[i]);
}
private void Start()
{
if (clipAreaPropertyId == 0)
clipAreaPropertyId = Shader.PropertyToID("_ClipArea");
materialList = new List<Material>();
var renderers = GetComponentsInChildren<Renderer>();
for (int i = 0; i < renderers.Length; i++)
for (int j = 0; j < renderers[i].materials.Length; j++)
materialList.Add(renderers[i].materials[j]);
started = true;
OnEnable();
}
private static UICameraMask GetParentMask(Transform child)
{
Transform parent = child.parent;
if (parent == null)
return null;
else
{
var uiCameraMask = parent.GetComponent<UICameraMask>();
if (uiCameraMask)
return uiCameraMask;
else
return GetParentMask(parent);
}
}
public void SetClipRect(Vector4 area)
{
for (int i = 0; i < materialList.Count; i++)
materialList[i].SetVector(clipAreaPropertyId, area);
}
}