|
|
Line 1: |
Line 1: |
| | | |
− | UPDATE: This other one is better: [[CreateScriptableObjectAsset]]
| + | OBSOLETED: This other one is better: [[CreateScriptableObjectAsset]] |
− | | + | |
− | ----
| + | |
− | | + | |
− | This is a confusing thing to illustrate, so hopefully this code example can speak for itself.
| + | |
− | | + | |
− | Save this script as ExampleSavedScriptableObject.cs in your Editor folder:
| + | |
− | | + | |
− | <syntaxhighlight lang="csharp">using System.Collections.Generic;
| + | |
− | using UnityEditor;
| + | |
− | | + | |
− | class ExampleSavedScriptableObject
| + | |
− | {
| + | |
− | [MenuItem("Custom/Create a MyExampleScriptableObjectClass ScriptableObject")]
| + | |
− | static void Execute()
| + | |
− | {
| + | |
− | MyExampleScriptableObjectClass tmp = new MyExampleScriptableObjectClass();
| + | |
− | tmp.size = "{512, 64}";
| + | |
− | tmp.sizeWidth = 512;
| + | |
− | tmp.sizeHeight = 64;
| + | |
− | List<MyExampleClass> l = new List<MyExampleClass>();
| + | |
− | | + | |
− | l.Add(new MyExampleClass("icon_OF_logo.png", "{64, 64}", "{0, -0}", 0, -0, false));
| + | |
− | l.Add(new MyExampleClass("icon_trophy.png", "{64, 64}", "{0, -1}", 0, -1, true ));
| + | |
− | l.Add(new MyExampleClass("notification_tab.png", "{480, 71}", "{-4, 12}", -4, 12, true ));
| + | |
− | tmp.list = l;
| + | |
− | | + | |
− | AssetDatabase.CreateAsset(tmp, "Assets/MyExampleScriptableObjectClassPrefab_renameme.asset");
| + | |
− | }
| + | |
− | }
| + | |
− | </syntaxhighlight>
| + | |
− | | + | |
− | and this one elsewhere in your project folder as MyExampleScriptableObjectClass.cs:
| + | |
− | | + | |
− | <syntaxhighlight lang="csharp">using UnityEngine;
| + | |
− | using System.Collections.Generic;
| + | |
− | | + | |
− | [System.Serializable]
| + | |
− | public class MyExampleScriptableObjectClass : ScriptableObject {
| + | |
− | | + | |
− | public string size;
| + | |
− | public float sizeWidth;
| + | |
− | public float sizeHeight;
| + | |
− | | + | |
− | public List<MyExampleClass> list;
| + | |
− | | + | |
− | }</syntaxhighlight>
| + | |
− | | + | |
− | and this one elsewhere in your project folder as MyExampleClass.cs:
| + | |
− | | + | |
− | <syntaxhighlight lang="csharp">using UnityEngine;
| + | |
− | using System.Collections.Generic;
| + | |
− | | + | |
− | [System.Serializable]
| + | |
− | public class MyExampleClass {
| + | |
− | | + | |
− | //some example data
| + | |
− | public string name;
| + | |
− | public string size;
| + | |
− |
| + | |
− | public string offset;
| + | |
− | public float offsetX;
| + | |
− | public float offsetY;
| + | |
− | public bool isTrimmed;
| + | |
− |
| + | |
− | //constructor
| + | |
− | public MyExampleClass(string name, string size, string offset, float offsetX, float offsetY, bool isTrimmed) {
| + | |
− | this.name = name;
| + | |
− | this.size = size;
| + | |
− | this.offset = offset;
| + | |
− | this.offsetX = offsetX;
| + | |
− | this.offsetY = offsetY;
| + | |
− | this.isTrimmed = isTrimmed;
| + | |
− | }
| + | |
− | | + | |
− | }
| + | |
− | </syntaxhighlight>
| + | |
− | | + | |
− | now use the menu command (Custom|Create a MyExampleScriptableObjectClass ScriptableObject). Click on the prefab that got created, and observe that you can see (and edit) its saved data in the Inspector. Neat eh? Sorry if this doesn't have a nice article describing why it's useful, but I find myself short on words. Feel free to edit this article!
| + | |