- Home /
Load and Instantiate prefab so that it can be tested in Play Mode Test
Can someone help me achieve the correct code I need in order to test my prefab:
Unity Version: 2018.1.0f2
Load a prefab from disc somehow that isn't using Resources.Load. Create an instance of the prefab (I imagine once I have the correct code to load the thing its a simple GameObject.Instantiate() call but i am likely to be wrong)
I want to be able to instantiate a prefab in a play mode test in c# code so that I can attempt testing my class. I thought this would be trivial to do so, I am finding that it is not. Reading best practices had advised me not to use Resources.Load ( https://unity3d.com/learn/tutorials/topics/best-practices/resources-folder 'Don't Use it.') and that I should use this asset bundle "thing" that I am having some difficulty using for my test.
This is as close as I have gotten which needless to say, is not close at all:
the assetBundles variable contains a list with one entry. "game" (now two "game.game") the paths variable contains a list with one entry "Assets/Spessman/Resources/Prefabs/Player.prefab" which is the correct asset I want, but I do not know how to load it so that I can instantiate based off it, which I am guessing what I need to do.
The error I am getting is:
WhenCreatingAnActorController (0.000s) --- OneTimeSetUp: Unhandled log message: '[Error] Unable to read header from archive file: Assets/Spessman/Resources/Prefabs/Player.prefab'. Use UnityEngine.TestTools.LogAssert.Expect
which I imagine is because I am meant to point at an Asset Bundle file and not a thing i want to load (guessing completely)
From some part of my mucking around I have magically conjured another of these "asset bundles" that i can't seem to find anywhere in the project called game.game that is erroneous.
Thank you for your time.
Answer by Oliver1135 · May 19, 2018 at 11:49 AM
I did not find a way to do this. ugly but I have just created a test class to test the manual construction of what my prefab would be and i'm just using that, it just really sucks. if the prefab gets a lot more complex that i want to test it might be very prone to error/maintenance.
I am still kind of happy i persevered though in this dirty fix, because in part of writing my tests I have discovered some bugs and have quickly fixed them so I'm a lot happier about future refactorings being possible. Even if its very far from ideal. If I'm doing this from code anyway i might abandon the use of prefabs and instantiate from code. Not sure.
Your answer
Follow this Question
Related Questions
Is it a valid approach to check Prefab connections in OnValidate()? 1 Answer
How to modify "resources.assets" (or alternative?) 2 Answers
How do you associate a Material Picker to prefab placed in scene from AssetBundles 0 Answers
how can I export a prefab modified from fbx model? 2 Answers
PlayMode Monobehavior test 1 Answer