NLDClient-yudde/ProjectNLD/Assets/Editor/Scene/TreeUtil.cs

82 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using System.Collections.Generic;
using SRF;
using UnityEditor;
using UnityEngine;
public static class TreeUtil
{
private const string TREE_OPAQUE_SHADER = "Assets/Code/Shaders/Scene/NLD_Scene_Opaque.shader";
[MenuItem("Assets/*Custom/资源处理/处理树资源")]
private static void PostProcessTree()
{
var selectedObj = Selection.activeGameObject;
if (selectedObj != null && EditorUtility.IsPersistent(selectedObj))
{
PostProcessTree(selectedObj);
}
}
//树是从Tree Creator生成的目前Unity已经不支持了需要将其中无用的资源删除并且新建材质
public static void PostProcessTree(GameObject tree)
{
if (tree.GetComponent<Tree>() == null)
{
Debug.LogError($"当前对象上没有Tree组件请确认{tree.name}是树!");
EditorGUIUtility.PingObject(tree);
return;
}
CreateOpaqueMaterial(tree);
RemoveUnUseComponent(tree);
RemoveTreeUselessSubAsset(tree);
EditorUtility.SetDirty(tree);
AssetDatabase.Refresh();
Debug.Log("处理成功!");
}
//为树创建不透明材质
private static void CreateOpaqueMaterial(GameObject tree)
{
var opaqueShader = AssetDatabase.LoadAssetAtPath<Shader>(TREE_OPAQUE_SHADER);
if (opaqueShader == null)
{
Debug.LogError($"找不到shader:{TREE_OPAQUE_SHADER}");
return;
}
var mats = MaterialUtil.ReplaceMaterialOnGameObject(tree, opaqueShader);
mats[0].name = "tree_branch";
mats[1].name = "tree_leaves";
mats[0].enableInstancing = true;
mats[1].enableInstancing = true;
AssetDatabase.AddObjectToAsset(mats[0],tree);
AssetDatabase.AddObjectToAsset(mats[1],tree);
}
//移除tree组件
private static void RemoveUnUseComponent(GameObject tree)
{
var treeComponent = tree.GetComponent<Tree>();
Object.DestroyImmediate(treeComponent, true);
}
//删除无用资源
private static void RemoveTreeUselessSubAsset(GameObject tree)
{
var assetPath = AssetDatabase.GetAssetPath(tree);
if (string.IsNullOrEmpty(assetPath))
return;
var allAssets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
for (int i = allAssets.Length - 1; i >= 0; i--)
{
var asset = allAssets[i];
var assetName = asset.name.ToLower();
if (AssetDatabase.IsSubAsset(asset) && (assetName.Contains("optimized") || assetName.Contains("tree data")))
{
AssetDatabase.RemoveObjectFromAsset(asset);
}
}
}
}