cancel
Showing results for 
Search instead for 
Did you mean: 

Unity API Helper

Justin_Baur
Star Contributor
Star Contributor

Hi All,

Just wanted to share something I have been using in my Unity API's for a little bit now.  This little snippet of code is a part of a library I have created for little addons to the normal Unity API.  

0144fbfa64c4470ca431c470913547d0

 

What this method does is allows you to send in a WorkView Object and get back an instance of your own custom type. All you have to do is use an Attribute on your public properties and with one line of code your item will be transformed from a workview object to something that can be used and passed around more simply in your code.  This utilizes reflection so if speed is your main concern I would recommend against it but it is still very quick and very reliable.  I have open sourced the entire project but I am not sure if posting personal links is allowed if anyone wants to check out the repository feel free to reach out to me.  

 

Let me know if you have any questions.

 

Thanks!

2 REPLIES 2

Alex_French
Elite Collaborator
Elite Collaborator

I like it.  If it were me, I would go ahead and make that (and many reusable things built on top of the Unity API) an Extension Method, and be able to use it very naturally as "var foo = myWVObject.ToClass<myClass>();".

Justin_Baur
Star Contributor
Star Contributor

@Alex French I really like that.  Within this same library I have a few other things written as extension methods but I had been using Newtonsoft's JSON deserializer and made my like that but I will definitely add in a ToClass method that just calls this behind the scenes for you.

You can actually even see in line 31 I am using an extension method on the WorkView.Object class that gets an attribute value by the string address. This means I can setup a class of mine that actually grabs values from more than just the WorkView object given. It can get the values from any string address (period delimited).  That extension method is just a wrapping around the .GetRelatedObject() method and the .AttributeValues.Find() methods.