Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AnnemarieWittig · Mar 23 at 05:08 PM · scriptableobjecttestingbestpracticesunittest

Add a ScriptableObject to my Unit Test from inspector

Hi! I have a part in my code with a lot of mathematical calculations. The used equations are ScriptableObjects and I'd like to test the calculations with the same scripted objects. Is there a way to do that? I am aware that I can just use

ScriptableObject.CreateInstance<>()

to create a new object of the type and give it the same values. However, that does invite errors and every time I change a value for one of the equations, I'd have to go through all the tests (or just one if I create a static method to return an object with needed values) to adjust the values, too.

Is there a way to use the objects that I created already within my testclass? Maybe similar to how I just drag/drop them via the Inspector while editing a scene?

If that is not possible, what would be the best alternative? Remove the (ScriptableObject-) equation-objects and create static methods to retrieve objects with the same values within my tests and other MonoBehavior-classes within my scenes?

I hope my issue is understandable, any help is appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hellium · Mar 23 at 05:21 PM

You can load your scriptable object from code

https://docs.unity3d.com/ScriptReference/AssetDatabase.FindAssets.html

CODE NOT TESTED

 private ScriptableObjectType LoadScriptableObject()
 {
     string scriptableObjectName = "ScriptableObjectName";
     string[] guids = AssetDatabase.FindAssets($"t:{nameof(ScriptableObjectType)} {scriptableObjectName }");
     if(guids.Length == 0)
         Assert.Fail($"No {nameof(ScriptableObjectType)} found named {scriptableObjectName}");

     if(guids.Length > 0)
         Debug.LogWarning($"More than one {nameof(ScriptableObjectType)} found named {scriptableObjectName}, taking first one");
 
      return (ScriptableObjectType) AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[0]), typeof(ScriptableObjectType));
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AnnemarieWittig · Mar 23 at 05:42 PM 0
Share

Thank you so much! I only found solutions that needed the objects to be moved to a Resources folder which I didn't wanna use so this really helped so much! I added the path to the folder the objects are located in to (maybe?) increase preformance and be sure the correct object is picked.

avatar image Hellium AnnemarieWittig · Mar 23 at 05:43 PM 0
Share

If you already know the path of the object, then yes, you can skip the whole FindAssets method, and just call return (ScriptableObjectType) AssetDatabase.LoadAssetAtPath(assetPath, typeof(ScriptableObjectType));. Just keep in mind that moving the Scriptable Object will break your tests.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unit Testing Rigidbody2D without polluting scene with gameobjects 1 Answer

MSTools instead of NUnit 0 Answers

Unity 5.6 Unit Testing 1 Answer

Load scene/prefab when running a test through the test runner 1 Answer

Monobehaviour.Start() is called when Playmode tests are executed 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges