美文网首页
Lod批量修改器设置好的

Lod批量修改器设置好的

作者: Rayson | 来源:发表于2023-11-01 13:40 被阅读0次
1.  using System;

2.  using System.Collections.Generic;

3.  using System.IO;

4.  using UnityEditor;

5.  using UnityEngine;

7.  public class BatchSetLodGroupWindow : EditorWindow{

9.  bool includeSubdirectories = true;

10.  LODFadeMode fadeMode = LODFadeMode.None;

12.  float cullRatio = 0.01f;

14.  public float[] lodWeights = [new](http://www.google.com/search?q=new+msdn.microsoft.com) float[] { 0.6f, 0.3f, 0.1f };

16.  string path = string.Empty;

18.  [MenuItem("Tools/BatchSetLodGroups")]

19.  public static void ShowWindow() {

20.  //Show existing window instance. If one doesn't exist, make one.

21.  EditorWindow.GetWindow([typeof](http://www.google.com/search?q=typeof+msdn.microsoft.com)(BatchSetLodGroupWindow));

22.  }

24.  void OnGUI() {

26.  GUILayout.Label("LODGroup Settings", EditorStyles.boldLabel);

28.  EditorGUILayout.BeginHorizontal();

29.  EditorGUILayout.PrefixLabel("Select Directory");

30.  if (GUILayout.Button("Browse")) {

31.  path = EditorUtility.OpenFolderPanel("Select Directory", "", "");

32.  }

33.  EditorGUILayout.EndHorizontal();

35.  EditorGUILayout.BeginHorizontal();

36.  EditorGUILayout.PrefixLabel("Current Path");

37.  EditorGUILayout.LabelField(path);

38.  EditorGUILayout.EndHorizontal();

40.  includeSubdirectories = EditorGUILayout.Toggle("Include Subdirectories", includeSubdirectories);

42.  EditorGUILayout.BeginHorizontal();

43.  EditorGUILayout.PrefixLabel("Fade Mode");

44.  fadeMode = (LODFadeMode)EditorGUILayout.EnumPopup((Enum)fadeMode);

45.  EditorGUILayout.EndHorizontal();

47.  EditorGUILayout.BeginHorizontal();

48.  EditorGUILayout.PrefixLabel("Cull Ratio (0.01 = 1%)");

49.  cullRatio = EditorGUILayout.FloatField(cullRatio);

50.  EditorGUILayout.EndHorizontal();

52.  EditorGUILayout.LabelField("LOD Level Weights (will be normalized to 1)");

54.  ScriptableObject target = this;

55.  SerializedObject so = [new](http://www.google.com/search?q=new+msdn.microsoft.com) SerializedObject(target);

56.  SerializedProperty prop = so.FindProperty([nameof](http://www.google.com/search?q=nameof+msdn.microsoft.com)(lodWeights));

57.  EditorGUILayout.PropertyField(prop, true); // True means show children

58.  so.ApplyModifiedProperties(); // Remember to apply modified properties

60.  if (GUILayout.Button("Apply")) {

61.  if (!string.IsNullOrWhiteSpace(path)) {

63.  PerformActionOnPrefab(path, this.includeSubdirectories, GetSetLoadGroup(fadeMode,cullRatio, this.lodWeights));

64.  }

65.  }

67.  Func<GameObject, bool> GetSetLoadGroup(LODFadeMode fadeMode,float cullRatio, float[] lodWeights) {

68.  return x => SetLodGroupInner(x, fadeMode,cullRatio, lodWeights);

69.  }

71.  bool SetLodGroupInner(GameObject prefab, LODFadeMode fadeMode,float cullRatio, float[] lodWeights) {

73.  if (lodWeights == null || lodWeights.Length == 0) return false;

74.  LODGroup[] lodGroups = prefab.GetComponentsInChildren<LODGroup>(true);

76.  if (lodGroups == null || lodGroups.Length <= 0) {

77.  return false;

78.  }

80.  for (int i = 0; i < lodGroups.Length; i++) {

81.  LODGroup lodGroup = lodGroups[i];

83.  lodGroup.fadeMode = fadeMode;

84.  LOD[] lods = lodGroup.GetLODs();

86.  float weightSum = 0;

87.  for (int k = 0; k < lods.Length; k++) {

89.  if (k >= lodWeights.Length) {

90.  weightSum += lodWeights[lodWeights.Length - 1];

91.  } else {

92.  weightSum += lodWeights[k];

93.  }

94.  }

97.  float maxLength = 1 - cullRatio;

98.  float curLodPos = 1;

99.  for (int j = 0; j < lods.Length; j++) {

101.  float weight = j < lodWeights.Length ? lodWeights[j] : lodWeights[lodWeights.Length - 1];

103.  float lengthRatio = weightSum != 0 ? weight / weightSum : 1;

105.  float lodLength = maxLength * lengthRatio;

106.  curLodPos = curLodPos - lodLength;

108.  lods[j].screenRelativeTransitionHeight = curLodPos;

109.  }

112.  lodGroup.SetLODs(lods);

113.  }

115.  return true;

116.  }

117.  }

120.  //action: input prefab output should save to prefab

121.  void  PerformActionOnPrefab(string path, bool includeSubdirectories,Func<GameObject,bool> action) {

122.  string[] files = Directory.GetFiles(path,"*.prefab", includeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);

124.  foreach (var file in files) {

125.  GameObject prefabGO = PrefabUtility.LoadPrefabContents(file);

127.  if (prefabGO != null) {

128.  action(prefabGO);

130.  PrefabUtility.SaveAsPrefabAsset(prefabGO, file);

131.  PrefabUtility.UnloadPrefabContents(prefabGO);

132.  }

133.  }

135.  }

137.  }

相关文章

  • 2018-04-13

    AD中批量修改器件的封装: 1选中其中任意一个元件的封装,右击——Find Similar Objects——Fo...

  • mybatis 实例

    增 批量 删 批量 改 批量 批量修改需设置db.properties新增 &allowMultiQueries=...

  • 3.3 结构-RenderSetup

    LOD LOD(Level of Detail),如LOD 200,即指定了其为200(其实这是Unity的内建D...

  • 3dmax教程,如何利用切片修改器生成建筑生长动画

    先打开一个建筑模型。 在修改器列表中添加一个【切片】修改器,将切片类型设置为【移除顶部】,如果你这里模型没有通过移...

  • 批量渲染场景

    此脚本可以设置批量渲染场景物体。 大致思路如下: 设置渲染输出路径 批量渲染 设置渲染器(VRay 或Scanli...

  • Unity Lod和Occlusion Culling

    Lod和Occlusion Culling Lod和遮挡剔除。  Occlusion Culling:Occlus...

  • Zhes Zhit Doul(舍不得)

    Zhes zhit doul! Nongs lax ndouk nongs lod, nongs lod yuad...

  • Redis常用命令

    字符串 设置字符串键值对 规则 示例 批量设置字符串键值对 规则 示例 获取字符串 规则 示例 批量设置字符串 规...

  • 2020-09-13blender导出v3d加载卡住,控制台报错

    一个物体有多个骨架修改器,去掉多余的骨架修改器只保留一个骨架修改器

  • Unity LOD 学习

    LOD指的就是Level of Detail。是一个设置细节等级的功能,大概就是说离物体越近,对应的细节等级...

网友评论

      本文标题:Lod批量修改器设置好的

      本文链接:https://www.haomeiwen.com/subject/qhzwidtx.html