68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
|
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);
|
|||
|
}
|
|||
|
}
|