重写地面物品shader 和 摆放工具

This commit is contained in:
2026-06-28 19:21:25 +08:00
parent 8cd8aa31da
commit 02ac67d338
61 changed files with 2602 additions and 2878 deletions

View File

@@ -0,0 +1,103 @@
using UnityEngine;
using UnityEditor;
/// <summary>
/// 地面裁剪工具 - 给Sprite应用带地面裁剪的Shader
/// </summary>
public class GroundClipTool : Editor
{
[MenuItem("Tools/Shader/应用地面裁剪Shader")]
public static void ApplyGroundClipShader()
{
if (Selection.gameObjects.Length == 0)
{
EditorUtility.DisplayDialog("提示", "请先选择物体", "确定");
return;
}
// 加载Shader
Shader shader = Shader.Find("Custom/SpriteWithGroundClip");
if (shader == null)
{
EditorUtility.DisplayDialog("错误", "找不到Shader: Custom/SpriteWithGroundClip\n请检查Shader文件是否存在", "确定");
return;
}
int count = 0;
foreach (GameObject go in Selection.gameObjects)
{
SpriteRenderer sr = go.GetComponentInChildren<SpriteRenderer>();
if (sr == null)
{
Debug.LogWarning($"跳过 {go.name} - 没有SpriteRenderer");
continue;
}
// 创建Material
Material mat = new Material(shader);
mat.name = $"{go.name}_GroundClipMat";
// 设置默认值
mat.SetFloat("_GroundHeight", 0f);
mat.SetFloat("_EnableClip", 1f);
mat.SetFloat("_ClipSoftness", 0.05f);
mat.SetColor("_Color", Color.white);
// 应用Material
sr.material = mat;
// 保存Material为Asset
string path = $"Assets/Materials/{mat.name}.mat";
if (!System.IO.Directory.Exists("Assets/Materials"))
{
System.IO.Directory.CreateDirectory("Assets/Materials");
}
AssetDatabase.CreateAsset(mat, path);
EditorUtility.SetDirty(go);
count++;
Debug.Log($"✓ 已应用地面裁剪Shader到 {go.name}");
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
if (count > 0)
{
EditorUtility.DisplayDialog("完成", $"已给 {count} 个物体应用地面裁剪Shader\n\n请在Inspector中调整Ground Height参数", "确定");
}
}
[MenuItem("Tools/Shader/设置地面高度(批量)")]
public static void SetGroundHeightBatch()
{
string input = EditorInputDialog.Show("设置地面高度", "请输入地面Y坐标:", "0");
if (input == null) return;
if (!float.TryParse(input, out float groundHeight))
{
EditorUtility.DisplayDialog("错误", "请输入有效的数字", "确定");
return;
}
int count = 0;
foreach (GameObject go in Selection.gameObjects)
{
SpriteRenderer sr = go.GetComponentInChildren<SpriteRenderer>();
if (sr == null || sr.material == null) continue;
if (sr.material.shader.name == "Custom/SpriteWithGroundClip")
{
sr.material.SetFloat("_GroundHeight", groundHeight);
EditorUtility.SetDirty(go);
count++;
}
}
if (count > 0)
{
EditorUtility.DisplayDialog("完成", $"已设置 {count} 个物体的地面高度为 {groundHeight}", "确定");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: DXoZvHmsUXKE+4nFTDw9nDfspZw/YDFLuPttPP+jrG1uozSGHZC+XR4=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: