PrefabScriptableObjects

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m
Line 22: Line 22:
 
         tmp.list = l;
 
         tmp.list = l;
  
         AssetDatabase.CreateAsset(tmp, "Assets/MyExampleClassPrefab_renameme.asset");
+
         AssetDatabase.CreateAsset(tmp, "Assets/MyExampleScriptableObjectClassPrefab_renameme.asset");
 
     }
 
     }
 
}
 
}

Revision as of 02:33, 30 January 2011

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:

<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");
   }

} </csharp>

and this one elsewhere in your project folder as MyExampleScriptableObjectClass.cs:

<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;

}</csharp>

and this one elsewhere in your project folder as MyExampleClass.cs:

<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; }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox