如何将MonoBehaviour移至外部程序集并且不卡在“缺少(单脚本)"中? [英] How to move a MonoBehaviour to an external assembly and don't get stuck in the "Missing (mono script)"?

查看:316
本文介绍了如何将MonoBehaviour移至外部程序集并且不卡在“缺少(单脚本)"中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unity3D的游戏项目中有很多脚本(MonoBehaviour),其中很多脚本是我场景中的gameObjects引用的.

I have many scripts (MonoBehaviour) inside a game project in Unity3D, many of them are referenced by gameObjects in my scenes.

现在,我想将其中一些脚本移到单独的程序集(类库)中,因为我们需要在其他项目中使用它们,并且希望改善代码组织.

Now I want to move some of these scripts to a separated assembly (class library), because we need to use them in other projects and we wish to improve our code organization.

因此,当我将脚本移动到该外部类库项目并将.dll放入de 文件夹中时,所有引用已移动脚本的游戏对象都会发出有关缺少(单脚本)"的警告.

So, when I move my scripts to this external class library project and put the .dll inside de Unity3D assets folder, all my gameobjects that reference the moved scripts are warning about "Missing (mono script)".

我想象Unity3D会跟踪按脚本名称和程序集名称查找的脚本引用.

I imagine Unity3D keeps track about script references looking by script name and assembly name.

有人知道解决这个问题的方法吗?

Does anyone knows a way to solve this problem?

推荐答案

不幸的是,Unity中没有自动的方法.您是正确的,因为Unity维护了对MonoBehaviour的引用.您有几种选择,但都不是理想选择:

Unfortunately there is no automated way of doing this in Unity. You are correct in that Unity maintains a reference to the MonoBehaviour. You have a few options, none of them are ideal:

选项1:将项目中当前的MonoBehaviours重命名为"MB_XXX",其中XXX是脚本名称.通过Unity编辑器执行此操作,Unity将维护引用.然后导入带有MonoBehaviours的已编译DLL,并带有您实际要使用的名称.遍历每个对象,从DLL中添加适当的MonoBehaviour,然后删除MB_XXX行为.使用资产商店中的Unity资产之一,将数据从一个MonoBehaviour复制到另一个,从而不必重新输入设置.

Option 1: Rename your MonoBehaviours currently in the project to something like "MB_XXX" where XXX is the name of the script. Do this through the Unity editor and Unity will maintain the reference. Then import your compiled DLL with the MonoBehaviours in it with the names you actually want to use. Go through each object adding in the appropriate MonoBehaviour from the DLL and removing the MB_XXX behaviour. Use one of the Unity assets from the Asset Store to copy data from one MonoBehaviour to another so that you don't have to re-enter your settings.

选项2:创建一个Shell脚本,该脚本将从当前使用的MonoBehaviour更改为DLL中的资产(以文本格式保存资产)中的参考ID.幸运的是,Unity允许您拥有相同名称的MonoBehaviours,至少在尝试运行游戏之前.您将需要获取DLL中每个MonoBehaviours的参考ID-您可以通过将DLL中的所有MonoBehaviours添加到一个空的GameObject中,然后查看生成的资产文件中的参考ID来实现此目的.

Option 2: Create a shell script that will change the reference ID in the assets (save your assets in text format) from the MonoBehaviour you currently use to the one in the DLL. Fortunately for you Unity permits you to have MonoBehaviours of the same name, at least until you try to run your game. You will need to obtain the reference IDs for each of the MonoBehaviours in the DLL - you can achieve this by just adding all of the MonoBehaviours in the DLL to an empty GameObject and then looking in the generated asset file to see what the reference IDs are.

不幸的是,这些都不是理想的解决方案,您只需使用它即可.

These are not ideal solutions unfortunately and you just have to wing it.

这篇关于如何将MonoBehaviour移至外部程序集并且不卡在“缺少(单脚本)"中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆