我正在创建fbx处理脚本来更改某些网格参数,创建预制件并自动添加碰撞网格。现在,我被困在尝试向我的游戏对象的某些子级添加碰撞网格物体组件。 但是到目前为止还没有运气。我有错误讯息 “不能将组件'MeshCollider'添加到UCX_SM_bench_1_LOD0,因为游戏对象是生成的预制件,只能通过AssetPostprocessor进行修改。” 我正在使用Unity 2017.1。
我是3D艺术家,这是我第一次编写脚本。
using System;
using System.IO;
using UnityEngine;
using UnityEditor;
public class ModelAssetPostprocessor : AssetPostprocessor
{
public void OnPreprocessModel()
{
ModelImporter modelImporter = assetImporter as ModelImporter;
modelImporter.importAnimation = false;
modelImporter.importBlendShapes = false;
modelImporter.importCameras = false;
modelImporter.importVisibility = false;
modelImporter.importLights = false;
modelImporter.importTangents = ModelImporterTangents.Import;
}
private const string PREFAB_DESTINATION_DIRECTORY = "Assets/Prefabs/";//setting up constant for prefabs destination directory
private static void OnPostprocessAllAssets(
string[] importedAssets,
string[] deletedAssets,
string[] movedAssets,
string[] movedFromAssetPaths)
{
foreach (string path in importedAssets)
{
if (!path.EndsWith(".fbx", StringComparison.OrdinalIgnoreCase)) //skipping all the files, which are not .fbx
continue;
GameObject modelAsset = AssetDatabase.LoadAssetAtPath<GameObject>(path); //As far as I understand here we declare local variable "modelAsset"
//It will become GameObject, which took from AssetDatabase using path we saved before. So
//Now we have a GameObject.
CreatePrefabFromModel(path, modelAsset);
//And now we declare a function to create prefab from our game object
}
}
private static void CreatePrefabFromModel(string path, GameObject modelAsset)
{
EnsureDirectoryExists(PREFAB_DESTINATION_DIRECTORY);
string modelFileName = Path.GetFileNameWithoutExtension(path);
string destinationPath = PREFAB_DESTINATION_DIRECTORY + modelFileName + ".prefab";
if (!File.Exists(destinationPath))
{
GameObjectUtility.SetStaticEditorFlags(modelAsset, StaticEditorFlags.LightmapStatic | StaticEditorFlags.NavigationStatic | StaticEditorFlags.BatchingStatic | StaticEditorFlags.OccludeeStatic | StaticEditorFlags.OccluderStatic | StaticEditorFlags.OffMeshLinkGeneration | StaticEditorFlags.ReflectionProbeStatic);
foreach (Transform child in modelAsset.transform)
{
if (child.transform.name.Contains("UCX_"))
{
Renderer rend;
rend = child.GetComponent<Renderer>();
rend.enabled = !rend.enabled;
string childname = child.transform.name;
GameObject childobject = modelAsset.transform.Find(childname).gameObject;
childobject.AddComponent<MeshCollider>();
}
}
PrefabUtility.CreatePrefab(
destinationPath,
modelAsset);
}
else
{
PrefabUtility.ReplacePrefab(
modelAsset,
AssetDatabase.LoadAssetAtPath<GameObject>(destinationPath),
ReplacePrefabOptions.ReplaceNameBased);
}
}
private static void EnsureDirectoryExists(string directory)
{
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
}
}
答案 0 :(得分:0)
我不知道您要达到什么目的,但是首先,请尝试在模型导入(https://docs.unity3d.com/Manual/FBXImporter-Model.html)中选中Generate Colliders
复选框,也许它将解决您所有的问题。
第二条建议-更新统一性(不,您的脚本将无法工作,但始终使用新版本是更好的方法)
根据您的最初要求。我只能建议(统一代码2018.3):
var go = GameObject.Instantiate(modelAsset);
GameObjectUtility.SetStaticEditorFlags(modelAsset, (StaticEditorFlags)(-1));
foreach (Transform child in go.transform)
{
if (child.name.Contains("UCX_"))
{
Renderer rend;
rend = child.GetComponent<Renderer>();
rend.enabled = !rend.enabled;
child.gameObject.AddComponent<MeshCollider>();
}
}
PrefabUtility.SaveAsPrefabAsset(go, destinationPath);
GameObject.DestroyImmediate(go);
我稍微修改了您的脚本。现在将首先实例化网格物体,并从实例化的对象而不是从资产创建预制件。
出现错误的原因是:您试图修改fbx
文件,就像它是一个场景对象一样。您应该首先实例化它,然后根据需要进行更改,然后将其另存为预制件。