{"id":133,"date":"2020-06-12T02:14:41","date_gmt":"2020-06-12T02:14:41","guid":{"rendered":"https:\/\/lucyestela.com\/dev\/?p=133"},"modified":"2021-03-02T03:00:12","modified_gmt":"2021-03-02T03:00:12","slug":"scriptable-objects","status":"publish","type":"post","link":"https:\/\/lucyestela.com\/dev\/unity\/scriptable-objects\/","title":{"rendered":"Scriptable Objects &#8211; Unity"},"content":{"rendered":"<div class=\"boldgrid-section\">\n<div class=\"container\">\n<div class=\"row\" style=\"padding-bottom: 0px;\">\n<div class=\"col-md-12 col-xs-12 col-sm-12\">\n<p class=\"\"><strong>Scriptable Objects have 2 major uses<\/strong><\/p>\n<ol class=\"\">\n<li>Saving and storing data during an editor session<\/li>\n<li>Saving data as an asset in our project for use at runtime<\/li>\n<\/ol>\n<p><strong>Things to Note<\/strong><\/p>\n<ol class=\"\">\n<li>In the editor we can save data to the SO during edit and runtime<\/li>\n<li>In a deployed build we can only read the data from the SO, we cannot save to it. &#8211; so no good for saving game state data.<\/li>\n<\/ol>\n<p class=\"\"><strong>So what&#8217;s good about Scriptable Objects<\/strong><\/p>\n<p class=\"\">So you can make your scriptable object an Asset and create one in your Project panel with the &#8220;Create&#8221; menu. You can also define the name of your scriptable object as it appears in the create menu.<\/p>\n<p class=\"\">Adding your Scriptable Object to the Unity &#8220;Create&#8221; menu<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"> <span style=\"color: #222222;\">[<\/span><span style=\"color: #3363a4;\">CreateAssetMenu<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">menuName<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #db7100;\">\"Scriptable Objects\/YourObjectName\"<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">fileName<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #db7100;\">\"YourObjectName.asset\"<\/span><span style=\"color: #222222;\">)]<\/span><\/span><\/pre>\n<p class=\"\">Now once you have created your scriptable object asset, it&#8217;s public properties will be available in the inspector simply by selecting it in your project panel. You don&#8217;t need to attach it to a scene gameobject in order to be able to view and edit the data.<\/p>\n<p class=\"\"><strong>Why not just use a class?<\/strong><\/p>\n<p class=\"\">Well yes you could, but using a scriptable object to store unchanging game data is probably the most efficient way. It creates just one set of data that remains constant, and everything in your file can access this one copy, rather than creating an instance of the data for every gameobject that uses it.<\/p>\n<p>&nbsp;<\/p>\n<p class=\"\"><strong>Serialization of Scriptable Objects<\/strong><\/p>\n<p class=\"\">Two important attributes to know about<\/p>\n<pre class=\"\">System.Serializable\r\n\r\nSystem.SerializeField<\/pre>\n<p class=\"\">Classes and struct need to be [Serializable]. All <strong>public<\/strong> fields are then serialized. Add it above the class definition.<\/p>\n<pre class=\"\"><span style=\"font-family: Menlo;\"><span style=\"color: #222222;\">[<\/span><span style=\"color: #222222;\">System<\/span><span style=\"color: #222222;\">.<\/span><span style=\"color: #222222;\">Serializable<\/span><span style=\"color: #222222;\">]<\/span>\r\n<span style=\"color: #009695;\">public<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">class<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">MyScriptableObject<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">:<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #3363a4;\">ScriptableObject<\/span><\/span><\/pre>\n<p class=\"\">In Unity, the <code>[SerializeField]<\/code>&nbsp; attribute allows you to have <strong>private<\/strong> script variables that are exposed in the Inspector. This will let you set the values in the editor without giving access to the variable from other scripts.<\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Scriptable Objects have 2 major uses Saving and storing data during an editor session Saving data as an asset in our project for use at runtime Things to Note In the editor we can save data to the SO during edit and runtime In a deployed build we can only read the data from the &#8230; <a title=\"Scriptable Objects &#8211; Unity\" class=\"read-more\" href=\"https:\/\/lucyestela.com\/dev\/unity\/scriptable-objects\/\" aria-label=\"More on Scriptable Objects &#8211; Unity\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"","bgseo_robots_follow":"","footnotes":""},"categories":[58],"tags":[38,37,4],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-unity","tag-object","tag-scriptable","tag-unity"],"_links":{"self":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":2,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/133\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}