The scriptableobject type is often used to store some unity3d objects that cannot be packaged, such as strings and some class objects. With this type of child type, you can use buildpipeline to package it into an assetbundle package for future use, which is very convenient. In this way, apart from reading playerpref and C # files, the other method is to access some data objects.
Using unityengine; public class xmlcontains: scriptableobject {Public String thexml ;}
By making your class enherit from scriptableobject, it becomes an "asset type" to unity, in the sense that you can store them on disk, and use the unity ide interface to drag it from the assetsfolder to a behaviour or whatever. it also automatically plays nice
With unity's dependency tracking system that decides what does and what does not go into a webplayer. going a bit further with this example, you cocould have let's say a tiefighter class, actually get some of its information (max health, max speed, acceleration ),
From your xml file, by making the tiefighter class look like this:
Using unityengine; public class tiefighter: monobehaviour {public xmlcontainer myxmlsettings; void awake () {// parse myxmlsettings. thexml into reasonable data} void Update () {// use the reasonable data }}
You coshould then attach the tiefighter script to a gameobject, and then drag your XML-asset from your assetsfolder onto the myxmlsettings public field. please note that this example is much better at showing how you cocould use a scriptableobject than how
Properly implement a tiefighter, as I wocould never really have that parse an actual XML file on runtime. tiefighters are too cool for XML anyway.
The above is not made into a bundle package. The following provides a package into a bundle resource for loading through www:
Generally, you can use buildpipeline. buildassetbundle to package resources, including models, textures, and textures. In fact, scriptableobject can also be packaged, while scriptableobject can store any data type.
The following is a complete example.
1. Create a class first:
Using unityengine; using system. Collections. Generic; public class sysdata: scriptableobject {public list <vector3> content ;}
This class has only one attribute content. You can also declare more attributes, events, and methods.
Later, the instances of this class will be packaged as resources.
2. Create an editor script:
This script is used to instantiate sysdata, set some data, and then package.
Using unityengine; using unityeditor; using system. collections. generic; public class export {[menuitem ("assets/export")] public static void execute () {// instantiate sysdata SD = scriptableobject. createinstance <sysdata> (); // set some data to the content SD. content = new list <vector3> (); SD. content. add (New vector3 (1, 2, 3); SD. content. add (New vector3 (, 6); // sysdata is created as an object, which is displayed on the project panel. String P = "assets/sysdata. asset "; assetdatabase. createasset (SD, P); object o = assetdatabase. loadassetatpath (p, typeof (sysdata); // package it as sysdata. assetbundle file. Buildpipeline. buildassetbundle (O, null, "sysdata. assetbundle"); // Delete the temporary object assetdatabase. deleteasset (p) on the panel );}}
3. Load the data resource at runtime.
Ienumerator start () {WWW = new WWW ("file: //" + application. datapath + "/.. /sysdata. assetbundle "); yield return WWW; // convert the resource to sysdata. This SD object will have the data originally set in the editor. Sysdata SD = www. assetbundle. mainasset as sysdata; // print SD. content [0], vector3 (, 3); print (SD. content [0]);}
4. This is to package the entire object instance. If the scriptableobject attribute includes other class instances, the classes must be serialized using [serializable. In this way, you can package the data required by the system (such as role classification and matrix4x4 data) and directly convert the data into a predetermined type after loading, which is more efficient.