115 lines
3.0 KiB
C#
115 lines
3.0 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
[ExecuteInEditMode]
|
||
public class MipmapLevelController : MonoBehaviour
|
||
{
|
||
[Range(-3, 3)]
|
||
public float mipMapBias = 0f;
|
||
|
||
private float lastMipMapBias = 0f;
|
||
private List<Renderer> allRenderers = new List<Renderer>();
|
||
|
||
void OnEnable()
|
||
{
|
||
CollectAllRenderers();
|
||
ApplyMipMapBias();
|
||
}
|
||
|
||
void OnDisable()
|
||
{
|
||
// 恢复原始设置
|
||
ResetMipMapBias();
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
// 检查MipMap偏移是否发生变化
|
||
if (mipMapBias != lastMipMapBias)
|
||
{
|
||
ApplyMipMapBias();
|
||
lastMipMapBias = mipMapBias;
|
||
}
|
||
}
|
||
|
||
void CollectAllRenderers()
|
||
{
|
||
allRenderers.Clear();
|
||
|
||
// 获取场景中所有的渲染器
|
||
Renderer[] renderers = FindObjectsOfType<Renderer>(true);
|
||
|
||
foreach (Renderer renderer in renderers)
|
||
{
|
||
allRenderers.Add(renderer);
|
||
}
|
||
|
||
//Debug.Log($"收集到 {allRenderers.Count} 个渲染器");
|
||
}
|
||
|
||
void ApplyMipMapBias()
|
||
{
|
||
foreach (Renderer renderer in allRenderers)
|
||
{
|
||
if (renderer == null) continue;
|
||
|
||
Material[] materials = renderer.sharedMaterials;
|
||
|
||
for (int i = 0; i < materials.Length; i++)
|
||
{
|
||
if (materials[i] == null) continue;
|
||
|
||
foreach (string texName in materials[i].GetTexturePropertyNames())
|
||
{
|
||
if (materials[i].HasProperty(texName))
|
||
{
|
||
Texture texture = materials[i].GetTexture(texName);
|
||
if (texture != null)
|
||
{
|
||
texture.mipMapBias = mipMapBias;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//Debug.Log($"已应用MipMap偏移:{mipMapBias}");
|
||
}
|
||
|
||
void ResetMipMapBias()
|
||
{
|
||
foreach (Renderer renderer in allRenderers)
|
||
{
|
||
if (renderer == null) continue;
|
||
|
||
Material[] materials = renderer.sharedMaterials;
|
||
|
||
for (int i = 0; i < materials.Length; i++)
|
||
{
|
||
if (materials[i] == null) continue;
|
||
|
||
foreach (string texName in materials[i].GetTexturePropertyNames())
|
||
{
|
||
if (materials[i].HasProperty(texName))
|
||
{
|
||
Texture texture = materials[i].GetTexture(texName);
|
||
if (texture != null)
|
||
{
|
||
texture.mipMapBias = 0f;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//Debug.Log("已重置所有MipMap偏移");
|
||
}
|
||
|
||
[ContextMenu("刷新渲染器列表")]
|
||
public void RefreshRenderers()
|
||
{
|
||
CollectAllRenderers();
|
||
ApplyMipMapBias();
|
||
}
|
||
} |