How to move a MonoBehaviour to an external assembly and don't get stuck in the “Missing (mono script)”?

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

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.

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)".

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

Does anyone knows a way to solve this problem?

回答1:

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:

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.

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.



回答2:

I created a tool called “Missing script resolver” inside the project Giacomelli.Unity.EditorToolbox.

The tool was created to try to solve this problem. It searchs for prefabs with “missing scripts” problem and fix them.

More details in this post: Fixing "Missing (Mono Script)".



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!